POJ 1350 Cabric Number Problem(我的水题之路——字符串和数字间的转换)

来源:互联网 发布:mysql 5.1.32.tar.gz 编辑:程序博客网 时间:2024/05/21 13:54
Cabric Number Problem
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 8056 Accepted: 2464

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

536422214444-1

Sample Output

N=5364:6543-3456=30878730-378=83528532-2358=6174Ok!! 3 timesN=2221:2221-1222=999999-999=0Ok!! 2 timesN=4444:No!!

Source

Xi'an 2002

这道题的整体思路比较简单,基本上没有什么特别的算法。

题中给定一个4位数字,需要你对这个4位数中所有的位数进行正序和逆序的排序,求出其可以组成的最大值和最小值。然后对这两个值进行求差,如果差的值等于6174或者0,就结束计算,同时输出“Ok!! %d times”,%d表示计算的次数,否则,继续将这个差值作为一个数,继续之前的运算。如果输入的数字4个数字均相同,则输出“No!!".

看到数字对各个位数的操作,就会首先想到字符串操作,需要先将数字转换成字符串,然后将字符串转换成数字,进行差值计算。所以就写两个函数,分别完成字符串到数字的转换和逆反运算。

注意点:
1)当输入数据本身就是6174的时候,不能不进行计算直接退出。
2)当输入数据不是四位数字的时候,直接输出"No!!",导致1个OLE。
3)当输出运算公式的时候,要使用数字输出,不能让3位数有前导0,导致1个WA。
4)当数字0,转换成字符串的时候,需要进行特判,要返回字符串"0",不能返回空串。

代码(1AC 1WA 1OLE):
#include <cstdio>#include <cstdlib>#include <cstring>#include <string>#include <algorithm>using namespace std;int StrToInt(char str[]){    int num;    int len, tmp;    int i;    len = strlen(str);    for (num = 0, tmp = 1, i = len - 1; i >= 0; i--){        num += tmp * (str[i] - '0');        tmp *= 10;    }    return num;}void IntToStr(int num, char str[]){    char tmp[10];    int i, j;    if (num != 0){        for (i = 0; num; i++){            tmp[i] = num % 10 + '0';            num /= 10;        }        for (j = 0, i --; i >= 0; i--, j++){            str[j] = tmp[i];        }        str[j] = '\0';    }    else{        str[0] = '0';        str[1] = '\0';    }}void swapStr(char str[]){    char tmp[10];    int i, j, len;    len = strlen(str);    for (i = 0, j = len - 1; i < len; i++, j--){        tmp[j] = str[i];    }    for (i = 0; i < len ; i++){        str[i] = tmp[i];    }    str[i] = '\0';}int JudgeSame(int num){    int tmp1, tmp2;    tmp1 = num % 10;    while (num){        tmp2 = num % 10;        if (tmp1 != tmp2){            return 0;        }        num /= 10;    }    return 1;}int main(void){    int num;    int flag;    char max[10], min[10], tmp[10];    while (scanf("%d", &num), num != -1){        printf("N=%d:\n", num);        flag = 0;        if (JudgeSame(num) || num < 1000 || num > 9999){            flag = -1;        }        while ((num != 0 && num != 6174 || flag == 0) && flag != -1){            IntToStr(num, tmp);            strcpy(min, tmp);            sort(min, min + strlen(min));            strcpy(max, min);            swapStr(max);            printf("%d-%d=%d\n", StrToInt(max), StrToInt(min), (num = StrToInt(max) - StrToInt(min)));            flag ++;        }        if (flag && flag != -1){            printf("Ok!! %d times\n", flag);        }        else if (flag == -1 || flag == 0){            printf("No!!\n");        }    }    return 0;}

原创粉丝点击