排序 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的框架基础学习
框架基础 框架实质: 所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端 B/S结构的响应: import socket def handle_requ ...
- 乘风破浪:LeetCode真题_020_Valid Parentheses
乘风破浪:LeetCode真题_020_Valid Parentheses 一.前言 下面开始堆栈方面的问题了,堆栈的操作基本上有压栈,出栈,判断栈空等等,虽然很简单,但是非常有意义. 二.Valid ...
- January 03 2017 Week 1st Tuesday
It is always morning somewhere in the world. 世界上总是有一个地方可以看到阳光. There may be always someone who can e ...
- 低版本兼容使用Fragment (转)
Fragment框架开发东西确实很方便,但是恼人的是从4.0才开始支持.以前的版本必须用兼容模式开发,本人在网上找了大量资料,终于找到些线索正常运行于2.1版本的安卓系统.现在浅说一下兼容版本使用Fr ...
- 如果要遍历除了for循环,你还知道什么?——JavaScript的各种遍历方式
如果要遍历除了for循环,你还知道什么?——JavaScript的各种遍历方式 这是曾经面试中的一道题目,当时的我用现在很潮的话讲,整个人是懵比的,我呆呆的说了句,我好像只知道for循环啊.后来回过神 ...
- 【转】多线程Core Data
原文地址:http://www.cocoanetics.com/2012/07/multi-context-coredata/ Multi-Context CoreData When you star ...
- 死磕salt系列-salt grains pillar 配置
grains 和 pillar 对比: Grains:存放静态数据,主要存储客户端的主机信息,重启grains会刷新. Pillar: 处理敏感数据, 处理差异性的文件. Grains数据系统 sal ...
- canvas二三事之签名板与视频绘制
今天,不知道怎么的就点开了语雀,然后就看到了<HTML5 Canvas 教程>,开始了canvas的研究(学习)之旅. 首先,想到的第一个东西就是签名板,上代码: <canvas i ...
- 20155328 2016-2017-2 《Java程序设计》第九周学习总结
20155328 2016-2017-2 <Java程序设计>第九周学习总结 教材学习内容总结 16.1 JDBC入门 JDBC是Java联机数据库的标准规范,定义一组标准类与接口,应用程 ...
- ethereumjs/ethereumjs-blockchain-1-简介和API
https://github.com/ethereumjs/ethereumjs-blockchain SYNOPSIS概要 A module to store and interact with b ...