SWUST OJ 有趣的三位数(0319)

有趣的三位数(0319)

Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 158 Accepted: 62
 
Description
有这样一组数,每组由三个三位数组成,并且这三个数中1~9有且仅有一次,更特殊的是这三个数中,两个较小的数相加等于另外一个数(如124 659 783)。 你的任务就是查找出所有满足上述条件的组(以组出现,并且按照升序排列(每组之间以按首元素升序。对于其中的一组,三个数也按照升序排列))。
 
Input
no input
 
Output
输出所有满足题意的组(每个组的三个数之间以一个空格)。
 
Sample Input

Sample Output
124 659 783
125 739 864
  |
  | -------省略
  |
216 738 954
218 349 567
  |
  |
  |
 
Hint
 
Source
计算机科学与技术专业2008级程序设计竞赛
 
 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4 int main()
 5 {
 6     int a[9]={1,2,3,4,5,6,7,8,9},b,c,d;
 7     while(next_permutation(a,a+9))
 8     {
 9         b=a[0]*100+a[1]*10+a[2];
10         c=a[3]*100+a[4]*10+a[5];
11         d=a[6]*100+a[7]*10+a[8];
12         if(b+c==d&&b<=c)
13         {
14             cout<<b<<' '<<c<<' '<<d<<endl;
15         }
16     }
17 return 0;
18 }

 

注:b>c时 break 并不正确,231 987 654 的下一个排列 234 156 789 就终止循环了,显然不正确。

posted on 2015-05-02 12:04  _飛  阅读(907)  评论(0编辑  收藏  举报

导航