poj 1350 Cabric Number Problem

http://poj.org/problem?id=1350

题意:6174问题。。。一个四位数。。四个数字重排。。。最大的减去最小的得到新的数字。最后一定能得到6174或者0.除非这个四位数的四个数字都一样。写出变化的过程。

思路:。。。可能不是不四位数。。略坑。然后写了下字符串和数字相互转化的两个函数。嗯。

/* ***********************************************
Author :111qqz
Created Time :2016年01月20日 星期三 12时51分41秒
File Name :code/poj/1350.cpp
************************************************ */
 1#include <cstdio>
 2#include <cstring>
 3#include <iostream>
 4#include <algorithm>
 5#include <vector>
 6#include <queue>
 7#include <set>
 8#include <map>
 9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
1using namespace std;
2const double eps = 1E-8;
3const int dx4[4]={1,0,0,-1};
4const int dy4[4]={0,-1,1,0};
5const int inf = 0x3f3f3f3f;
6int n;
7int cnt;
 1int get_next( int x)
 2{
 3    int a,b,len;
 4    char st[10];
 5    sprintf(st,"%d",x);
 6    len = strlen(st);
 7//    if (len<4) return -1;
 8    if (st[0]==st[1]&&st[1]==st[2]&&st[2]==st[3]&&st[3]==st[0]) return -1;
 9    for ( int i = 0 ; i < len ; i++)
10	for ( int j =  i+1 ;  j < len ; j++)
11	    if (st[i]<st[j]) swap(st[i],st[j]);
    sscanf(st,"%d",&a);
//    cout<<st<<endl;

    for ( int i = 0 ;i < len/2 ;  i++) swap(st[i],st[len-1-i]);
  //  cout<<st<<endl;

    sscanf(st,"%d",&b);

    printf("%d-%d=%d\n",a,b,a-b);
    return a-b;
1}
2int main()
3{
4	#ifndef  ONLINE_JUDGE 
5	freopen("code/in.txt","r",stdin);
6  #endif
 1	while (scanf("%d",&n)!=EOF&&n!=-1)
 2	{
 3	    cnt =  0 ;
 4	    printf("N=%d:\n",n);
 5	    if (n<1000||n>9999) 
 6	    {
 7		puts("No!!");
 8		continue;
 9	    }
10	    while (1)
11	    {
12		cnt++;
13		n = get_next(n);
14	//	cout<<"n:"<<n<<endl;
15		if (n==-1)
16		{
17		    puts("No!!");
18		    break;
19		}
20		if (n==0||n==6174)
21		{
22		    printf("Ok!! %d times\n",cnt);
23		    break;
24		}
25	    }
26	}
1  #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4    return 0;
5}