poj 1350 Cabric Number Problem
http://poj.org/problem?id=1350
题意:6174问题。。。一个四位数。。四个数字重排。。。最大的减去最小的得到新的数字。最后一定能得到6174或者0.除非这个四位数的四个数字都一样。写出变化的过程。
思路:。。。可能不是不四位数。。略坑。然后写了下字符串和数字相互转化的两个函数。嗯。
1/* ***********************************************
2Author :111qqz
3Created Time :2016年01月20日 星期三 12时51分41秒
4File Name :code/poj/1350.cpp
5************************************************ */
6
7#include <cstdio>
8#include <cstring>
9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33int n;
34int cnt;
35
36int get_next( int x)
37{
38 int a,b,len;
39 char st[10];
40 sprintf(st,"%d",x);
41 len = strlen(st);
42// if (len<4) return -1;
43 if (st[0]==st[1]&&st[1]==st[2]&&st[2]==st[3]&&st[3]==st[0]) return -1;
44 for ( int i = 0 ; i < len ; i++)
45 for ( int j = i+1 ; j < len ; j++)
46 if (st[i]<st[j]) swap(st[i],st[j]);
47
48 sscanf(st,"%d",&a);
49// cout<<st<<endl;
50
51 for ( int i = 0 ;i < len/2 ; i++) swap(st[i],st[len-1-i]);
52 // cout<<st<<endl;
53
54 sscanf(st,"%d",&b);
55
56 printf("%d-%d=%d\n",a,b,a-b);
57 return a-b;
58
59
60}
61int main()
62{
63 #ifndef ONLINE_JUDGE
64 freopen("code/in.txt","r",stdin);
65 #endif
66
67 while (scanf("%d",&n)!=EOF&&n!=-1)
68 {
69 cnt = 0 ;
70 printf("N=%d:\n",n);
71 if (n<1000||n>9999)
72 {
73 puts("No!!");
74 continue;
75 }
76 while (1)
77 {
78 cnt++;
79 n = get_next(n);
80 // cout<<"n:"<<n<<endl;
81 if (n==-1)
82 {
83 puts("No!!");
84 break;
85 }
86 if (n==0||n==6174)
87 {
88 printf("Ok!! %d times\n",cnt);
89 break;
90 }
91 }
92 }
93
94 #ifndef ONLINE_JUDGE
95 fclose(stdin);
96 #endif
97 return 0;
98}