排序 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空 ...
随机推荐
- centos编译安装php7
环境说明 VMware 12 中搭建的CentOS 7 x64 4核 2G内存 环境中已经安装了http://blog.csdn.net/u014595668/article/details/5016 ...
- [mutt] Configure mutt to receive email via IMAP and send via SMTP
“All mail clients suck. This one [mutt] just sucks less.” Michael Elkins, ca. 1995 Actually I have t ...
- GO语言 切片的缩短和增长原理
package main import "fmt" //import OS "os" //import "strings" //import ...
- 结构类型:Struct
一.概述: 结构类似于类,但结构为值类型,存储于栈中. 结构不能继承和被继承,但可实现接口. 结构成员访问级别有public,private(默认) ,internal. 1.简单结构 可以将无方法, ...
- 寒假关于计算机课程的学习计划(第二次作业<二>)
由于自己刚从紧张的高中学习中解脱出来,进入大学学习意识不够重视,导致学业与别人相差较大,特别是C语言,所以打算寒假主要先补一补C语言.自己在网上查找了很多资料,包括浙江大学翁凯老师在网易云课堂讲的&l ...
- memcached源码剖析4:并发模型
memcached是一个典型的单进程系统.虽然是单进程,但是memcached内部通过多线程实现了master-worker模型,这也是服务端最常见的一种并发模型.实际上,除了master线程和wor ...
- Java的日期时间
Date类 Date d = new Date() 使用SimpleDateFormat格式化日期. SimpleDateFormat允许用户自定义日期时间格式来运行. package cuiyuee ...
- JavaScript的DOM_动态加载脚本和样式
一.动态加载脚本 当网站需求变大,脚本的需求也逐步变大.我们就不得不引入太多的 JS 脚本而降低了整站的性能,所以就出现了动态脚本的概念,在适时的时候加载相应的脚本. 1.动态加载js文件 比如:我们 ...
- PhoneGap 白名单安全机制 navigator.app 加载外部页面返回以及退出介绍
一. Phonegap 白名单安全机制 Phonegap应用的页面大多存在于本地,但有时需要加载外部的Web页面到应用内置的浏览器 视图中已完成特定的应用功能,出于安全性考虑,PhoneGap 设立了 ...
- BZOJ2004:[HNOI2010]Bus 公交线路(状压DP,矩阵乘法)
Description 小Z所在的城市有N个公交车站,排列在一条长(N-1)km的直线上,从左到右依次编号为1到N,相邻公交车站间的距离均为1km. 作为公交车线路的规划者,小Z调查了市民的需求,决定 ...