排序 permutation
习题2-6 排序 permutation
用1,2,3……9组成3个三位数abc,def和ghi,每个数字恰好使用一次,要求abc:def:ghi=1:2:3。按照“abc def ghi”的格式输出所有解,每行一个解。提示:不必太动脑筋。
我的思路:
既然每个数字只能出现一次,那么就将1-9这九个数字出现的情况做记录,出现了的赋值为1,没有出现的为0。
然后分别对应着abc,def,ghi的百、十、个三位。
最后进行累加,如果累加器结果为9,那么说明每个数字出现了一次,则可以输出,否则不然。
#include "stdio.h"
int main()
{
for(int abc=;abc<=;abc++)//确定abc最小值和最大值
{//1-9出现的数字为1,没有出现的为0,累加起来,等于9则正确,否则错误
int s[]={}; //定义一个数组记录0~9出现的次数,并且循环前置零,
//防止上一轮出现的1在这一轮没有出现,影响最后累加,导致多加
s[abc/]=;//百位 a
s[abc/%]=;//十位 b
s[abc%]=;//个位 c int def=*abc;//令def为abc的2倍
s[def/]=;//百位 d
s[def/%]=;//十位 e
s[def%]=;//个位 f int ghi=*abc;//令ghi为abc的3倍
s[ghi/]=;//百位 g
s[ghi/%]=;//十位 h
s[ghi%]=;//个位 i int count=;//累加器
for(int i=;i<=;i++)//实现1-9的累加计数
count+=s[i];
if(count==)//如果1-9都出现了,则输出这些组合
printf("%d %d %d\n",abc,def,ghi);
}
return ;
}
排序 permutation的更多相关文章
- 字典排序permutation
理论 C++ 中的next_permutation 一般作为正序全排列的使用规则,其实这个就是正序字典排序的实现. 比如我们要对 列表 [1,2,3] 做full permutation 一般使用递 ...
- pandas中的随机排序和抽样
1.随机重排序 使用take()随机排序 如: df.take([54]) #采取索引为54的数据 可以借助np.random.permutation()函数随机排序 permutation()函 ...
- leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法
Next Permutation Implement next permutation, which rearranges numbers into the lexicographically ne ...
- CF798E. Mike and code of a permutation [拓扑排序 线段树]
CF798E. Mike and code of a permutation 题意: 排列p,编码了一个序列a.对于每个i,找到第一个\(p_j > p_i\)并且未被标记的j,标记这个j并\( ...
- Coursera Algorithms week2 基础排序 练习测验: Permutation
题目原文: Given two integer arrays of size n , design a subquadratic algorithm to determine whether one ...
- [LeetCode] 60. Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- HDU 4917 Permutation(拓扑排序 + 状压DP + 组合数)
题目链接 Permutation 题目大意:给出n,和m个关系,每个关系为ai必须排在bi的前面,求符合要求的n的全排列的个数. 数据规模为n <= 40,m <= 20. 直接状压DP空 ...
随机推荐
- python之内置函数,匿名函数
什么是内置函数? 就是Python给你提供的,拿来直接用的函数,比如print,input等等.其实就是我们在创建.py的时候python解释器所自动生成的内置的函数,就好比我们之前所学的作用空间 内 ...
- 四、python小功能记录——按键转点击事件
import win32api,win32gui,win32confrom pynput.keyboard import Listener def clickLeftCur(): win32api.m ...
- Linux(CentOS)网卡的基本设置
临时设置 修改IP地址: ifconfig eth0 10.60.45.205 mask 255.255.255.0 修改网关地址 route add default gw 10.60.45.1 de ...
- Java代码实现对Azkaban的Flow进行设置定时
代码如下: public class CurlToAzkaban{ public static void scheduleExecute(String flow, String cronExpress ...
- 【2017.10.13 ROS机器人操作系统】ROS系统常用术语及资源
ROS机器人操作系统是一种后操作系统,提供了类似于软件开发中使用到的中间件的功能. ROS: Robot Operating System 机器人操作系统 Package: 功能包 Stack: 功能 ...
- unittest:2 执行多条用例,仅执行一次setUp和tearDown
对象方法setUp()和tearDown() 每个用例执行前后都会被调用.但是有另外一种场景:setUp之后执行完所有用例,最后调用一次tearDown.比如打开网页,多条用例分别验证网页上的元素正确 ...
- 2、Python文件操作工具 xlrd 工具
#打开excel文档workbook = xlrd.open_workbook('..\cye.xls',encoding_override='utf-8') #获取sheet页信息sheet = w ...
- Linux系统下常用的磁盘管理命令——du / df / fdisk / mount / xxd
之前使用虚拟机体验Linux操作系统的使用,一般使用默认的磁盘分区设置,也很少涉及磁盘管理操作,且总有删除重装作为后盾.在安装Ubuntu双系统后,在使用过程中遇到了磁盘分区不合理导致的/boot分区 ...
- Linux环境下GNU, GCC, G++编译器
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u012823258/article/details/36890125 一.GNU GNU是" ...
- unbuntu 14安装 golang
golang目前有两种编译,一种是golang官方提供的,另外一个是gnu提供的gccgo.这里安装的是Golang,从仓库安装(apt-get) sudo apt-get instal ...