首页 > 代码库 > HDU 2274 Magic WisKey
HDU 2274 Magic WisKey
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2274
Magic WisKey
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 568 Accepted Submission(s): 323
Problem Description
On New Year Festival, Liu Qian’s magic impressed on little WisKey’s heart and he wants to learn some magic to make himself stronger.
One day, he met a cowman named LinLe. Linle is very nice, he told little WisKey the mysteries of magic. Now, little WisKey began to perform the magic to you.
“Hello, Everybody. I have five decimal numbers named a, b, c, d, e, (0 <= a, b, c, d, e <= 9) and I rearranged them, and then combined them into a number, for example <a, b, c, d, e> = a*10000 + b*1000 + c*100 + d*10 + e*1. You know the number of permutations is 5! = 120. So you have 120 numbers in your hands, you can pick a number N from the 120 numbers and calculate the sum S of remain 119 numbers. AHA~, If you tell me the S, I can guess the N~!”
It’s easy? Okay, you can challenge this.
One day, he met a cowman named LinLe. Linle is very nice, he told little WisKey the mysteries of magic. Now, little WisKey began to perform the magic to you.
“Hello, Everybody. I have five decimal numbers named a, b, c, d, e, (0 <= a, b, c, d, e <= 9) and I rearranged them, and then combined them into a number, for example <a, b, c, d, e> = a*10000 + b*1000 + c*100 + d*10 + e*1. You know the number of permutations is 5! = 120. So you have 120 numbers in your hands, you can pick a number N from the 120 numbers and calculate the sum S of remain 119 numbers. AHA~, If you tell me the S, I can guess the N~!”
It’s easy? Okay, you can challenge this.
Input
Each line will contain an integer S. Process to end of file.
Output
For each case, output all possible N, print the number N with 5 digits, including the leading zeros, one line per case.
I promise every case have one solution at least. If have many N, please output them from small to large in one line, separate them with a blank space.
I promise every case have one solution at least. If have many N, please output them from small to large in one line, separate them with a blank space.
Sample Input
2933266
6392217
4245386
Sample Output
00038
07719
21238
题目大意:选定五个数字,他们形成一个五位数N。你事先不知道是哪五个数字,但你知道这五个数字全排列后组成的 120 个五位数的和减去该五位数的结果S,求N。
解题思路:五位数总共的可能就是从00000~99999。所以先打表暴力枚举求出所有五位数的S,然后对于每个输入的S,查找对应的N。
求五位数全排列之和的方法:假设五个数字分别是a,b,c,d,e。首先考虑a,a在万位上会出现4!次,千位上也会出现4!次,百位十位个位同理,所以a出现的每个地方总和为a*4!*(10000+1000+100+10+1)。b,c,d,e与a类似。所以总和为(a+b+c+d+e)*4!*11111。
AC代码:
1 #include<stdio.h> 2 #include<string.h> 3 int a[6],s[100005],sum; 4 int main() 5 { 6 for(int i=0;i<=99999;i++) { //先打表 7 int t=i,k=0,sum; 8 while(t>0) { 9 a[k++]=t%10; 10 t/=10; 11 } 12 int tmp=0; 13 for(int j=0;j<k;j++) 14 tmp+=a[j]; 15 s[i]=tmp*24*11111-i; 16 } 17 while(~scanf("%d",&sum)) { //直接查询 18 for(int i=0;i<=99999;i++) 19 if(sum==s[i]) printf("%05d\n",i); 20 } 21 return 0; 22 }
HDU 2274 Magic WisKey
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。