POJ 1350 Cabric Number Problem

题目链接:http://poj.org/problem?id=1350
Cabric Number Problem
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11123 Accepted: 3422
Description

If we input a number formed by 4 digits and these digits are not all of one same value, then it obeys the following law. Let us operate the number in the following way:
(1) Arrange the digits in the way from bigger to smaller, such that it forms the biggest number that could be made from these 4 digits;
(2) Arrange the digits in the way from smaller to bigger, such that it forms the smallest number that could be made from these 4 digits (If there is 0 among these 4 digits, the number obtained may be less than four digits);
(3) Find the difference of these two numbers that is a new four digital number.
Repeat the above process, we can finally always get the result 6174 or 0.
Please write the program to realize the above algorithm.
Input

Each case is a line of an integer.-1 denotes the end of input.
Output

If the integer is formed exactly by 4 digits and these digits are not all of one same value, then output from the program should show the procedure for finding this number and the number of repetition times. Otherwise output “No!!”.
Sample Input

5364
2221
4444
-1
Sample Output

N=5364:
6543-3456=3087
8730-378=8352
8532-2358=6174
Ok!! 3 times
N=2221:
2221-1222=999
999-999=0
Ok!! 2 times
N=4444:
No!!
Source

Xi’an 2002

模拟题

题意:就是首先给你一个四位数,如果这个四位数的每一位都相等,直接输出No!!
否则的话,就用这四位数字组成的最大的数减去最小的数,直到差为6174或者是为0的时候才结束,最后按照特定的格式并且将变换的次数输出。

我的代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[8];
void fenjie(int n)
{
    a[0]=n%10;
    n=n/10;
    a[1]=n%10;
    n/=10;
    a[2]=n%10;
    n/=10;
    a[3]=n%10;
}
int main()
{
    int n;
    //fenjie(6543);
    //for(int i=0;i<4;i++)
        //printf("%d ",a[i]);
    while(~scanf("%d",&n))
    {
        if(n==-1)
            break;
        printf("N=%d:\n",n);
        fenjie(n);
        if(a[0]==a[1]&&a[1]==a[2]&&a[2]==a[3]||n<1000||n>9999)
        {
            printf("No!!\n");
            continue;
        }
        int s=-1,d=0;
        while(s!=0&&s!=6174)
        {
            fenjie(n);
            int len=0,flag=0;
            for(int i=3;i>=0;i--)
                if(a[i]!=0)
                len++;
            for(int i=0; i<len-1; i++)
                if(a[i]!=a[i+1])
                {
                    flag=1;
                }
            if(flag==0)
            {
                d++;
                int tt=a[3]*1000+a[2]*100+a[1]*10+a[0];
                printf("%d-%d=0\n",tt,tt);
                break;
            }
            sort(a,a+4);
            int s1,s2;
            if(a[2]==0)
            {
                s1=a[3];
                s2=a[3]*1000+a[2]*100+a[1]*10+a[0];
            }
            else if(a[1]==0)
            {
                s1=a[2]*10+a[3];
                s2=a[3]*1000+a[2]*100+a[1]*10+a[0];
            }
            else if(a[0]==0)
            {
                s1=a[1]*100+a[2]*10+a[3];
                s2=a[3]*1000+a[2]*100+a[1]*10+a[0];
            }
            else
            {
                s1=a[0]*1000+a[1]*100+a[2]*10+a[3];
                s2=a[3]*1000+a[2]*100+a[1]*10+a[0];
            }
            s=s2-s1;
            n=s;
            printf("%d-%d=%d\n",s2,s1,s2-s1);
            d++;
        }
        printf("Ok!! %d times\n",d);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值