(sscanf,sprintf)Cabric Number Problem(P1350)
来源:互联网 发布:包装设计 知乎 编辑:程序博客网 时间:2024/06/06 04:19
又学到东东了,哈哈,
注意:要限制其输入的数是四位的,不然不合法,
#include <iostream>#include<algorithm>using namespace std;bool cmp(char a,char b){return a>b;}bool get_next(int &x){int a,b,n;char s[11];sprintf(s,"%d",x);n = strlen(s);sort(s,s+n);sscanf(s,"%d",&a);sort(s,s+n,cmp);sscanf(s,"%d",&b);printf("%d-%d=%d\n",b,a,b-a);x=b-a; if (b-a==6174||b-a==0)return false;return true;}int main(){int i,j,k;int n;while (cin>>n,n!=-1){printf("N=%d:\n",n);int step=1;if (n%1111==0||n>9999||n<1000){printf("No!!\n");continue;}while (get_next(n))step++;printf("Ok!! %d times\n",step);}return 0;}
Cabric Number Problem
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 8209 Accepted: 2519
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.
(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
- (sscanf,sprintf)Cabric Number Problem(P1350)
- 1350 Cabric Number Problem
- poj1350 Cabric Number Problem
- Cabric Number Problem
- poj 1350 Cabric Number Problem
- POJ 1350 Cabric Number Problem
- poj1350(Cabric Number Problem)
- POJ1350 Cabric Number Problem [模拟]
- poj 1350 Cabric Number Problem
- poj 1350 Cabric Number Problem
- poj 1350 Cabric Number Problem
- POJ 1350Cabric Number Problem
- poj-1350Cabric Number Problem
- POJ 1350:Cabric Number Problem
- POJ 1350 Cabric Number Problem
- POJ 1350-Cabric Number Problem
- POJ 1350 Cabric Number Problem(模拟)
- POJ 1350 Cabric Number Problem (6174问题)
- java调用存储过程进行增、删、查、改操作(源代码)
- 同步和互斥有什么联系和区别
- UNIX 系统上的文本操作简介
- 线程的调度与切换
- 一个简单的基于多线程的web server
- (sscanf,sprintf)Cabric Number Problem(P1350)
- windows服务的安装和卸载
- 安卓应用程序加密,签名和发布
- 页面退出不可后退
- 说说JSON和JSONP,也许你会豁然开朗,含jQuery用例
- java测试类增、删、查、改的编写(源代码)
- ArcGIS Server开发篇之一:开发语言介绍
- 如何转换数组 List to ObservableCollection
- Question: STO billing price determination