HDU 2274 Magic WisKey
Posted 风中的簌雨
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章