习题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的更多相关文章

  1. 字典排序permutation

    理论 C++ 中的next_permutation 一般作为正序全排列的使用规则,其实这个就是正序字典排序的实现. 比如我们要对 列表 [1,2,3]  做full permutation 一般使用递 ...

  2. pandas中的随机排序和抽样

    1.随机重排序 使用take()随机排序 如: df.take([54])   #采取索引为54的数据 可以借助np.random.permutation()函数随机排序 permutation()函 ...

  3. leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法

    Next Permutation  Implement next permutation, which rearranges numbers into the lexicographically ne ...

  4. CF798E. Mike and code of a permutation [拓扑排序 线段树]

    CF798E. Mike and code of a permutation 题意: 排列p,编码了一个序列a.对于每个i,找到第一个\(p_j > p_i\)并且未被标记的j,标记这个j并\( ...

  5. Coursera Algorithms week2 基础排序 练习测验: Permutation

    题目原文: Given two integer arrays of size n , design a subquadratic algorithm to determine whether one ...

  6. [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 ...

  7. [LeetCode] Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  8. Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  9. HDU 4917 Permutation(拓扑排序 + 状压DP + 组合数)

    题目链接 Permutation 题目大意:给出n,和m个关系,每个关系为ai必须排在bi的前面,求符合要求的n的全排列的个数. 数据规模为n <= 40,m <= 20. 直接状压DP空 ...

随机推荐

  1. Selenium2学习(一)-pip降级selenium3.0

    selenium版本安装后启动Firefox出现异常:'geckodriver' executable needs to be in PATH selenium默默的升级到了3.0,然而网上的教程都是 ...

  2. Linux Reboot And Poweroff Command

    1.Environment:RedHat Linux Enterprise 6+Vmware Workstation 12 Pro 2.Linux shutdown and restart comma ...

  3. find bugs设置

  4. scala当中的类型参数

    类型参数主要就是研究scala当中的类或者scala当中的方法的泛型 1.scala当中的类的泛型         object Demo8 {          def main(args: Arr ...

  5. #Alpha Scrum4

    Alpha Scrum3 牛肉面不要牛肉不要面 Alpha项目冲刺(团队作业5) 各个成员在 Alpha 阶段认领的任务 林志松:音乐网页前端页面编写,博客发布 林书浩.陈远军:界面设计.美化 吴沂章 ...

  6. Cloudera Manager大数据集群环境搭建

    笔者安装CDH集群是参照官方文档:https://www.cloudera.com/documentation/enterprise/latest/topics/cm_ig_install_path_ ...

  7. 【转】Faster RCNN 原理

    看过好几篇讲Faster RCNN的文章,有一些基础以后,看这个文章是最好的. https://www.cnblogs.com/wangyong/p/8513563.html

  8. P2668 斗地主

    题目描述 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的AAA到KKK加上大小王的共545454张牌来进行的扑克牌游戏.在斗地主中,牌的大小关 系根据牌的数码表示如下: ...

  9. 【JavaScript】particle

    这是js实现的粒子动画,有两种模式,分别是zoom和line,它们对应的效果不同,但是原理都相同,具体分析如下: 部分程序如下: var p = this; p.originParams = orig ...

  10. linux 下 chkconfig安装与使用详解

    chkconfig 安装 开始的时候因为Raspbian的原因,系统是不自带chkconfig这个命令的, root@raspberrypi:~# chkconfig-bash: chkconfig: ...