HDU 6463.超级无敌简单题-卡边界的暴力 (“字节跳动-文远知行杯”广东工业大学第十四届程序设计竞赛)

超级无敌简单题

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 570    Accepted Submission(s): 274


Problem Description
通常来说,题面短的题目一般都比较难,所以我要把题面写得很长很长。
通常来说,题面短的题目一般都比较难,所以我要把题面写得很长很长。
通常来说,题面短的题目一般都比较难,所以我要把题面写得很长很长。
鸽子数字由以下过程定义:从任何正整数开始,将数字替换为其各个数位的平方和,并重复该过程,直到该数字等于1。如果不能,则这个数字不是鸽子数。
例如7是鸽子数,因为7->49->97->130->10->1。(7*7=49,4*4+9*9=97,9*9+7*7=130....如此类推)
显然1是第一个鸽子数。
有Q个询问,每个询问给出一个数k,你需要输出第k个鸽子数。
 
Input
第一行一个Q,代表询问的个数(Q<=100000)
接下来Q行,每行一个数字k(k<150000)
 
Output
每行输出一个数,代表第k个鸽子数
 
Sample Input
2 1 2
 
Sample Output
1 7
 
Source
 
 
代码:
 1 //6463
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 const int maxn=150000+10;
 5 int ans[maxn];
 6 int main()
 7 {
 8     int n=0;
 9     for(int i=1;i<=1e6+4e4+5e3;i++){
10         int t=0,cnt=i;
11         while(1){
12             int a[100];
13             int h=0;
14             while(cnt){
15                 a[++h]=cnt%10;
16                 cnt/=10;
17             }
18             int ret=0;
19             for(int i=1;i<=h;i++)
20                 ret+=a[i]*a[i];
21             if(ret==1){
22                 ans[++n]=i;
23                 //cout<<n<<" "<<i<<endl;
24                 break;
25             }
26             else cnt=ret;
27             t++;
28             if(t==10) break;
29         }
30     }
31     //cout<<n<<endl;
32     int q;
33     scanf("%d",&q);
34     while(q--){
35         int x;
36         scanf("%d",&x);
37         cout<<ans[x]<<endl;
38     }
39 }

猜你喜欢

转载自www.cnblogs.com/ZERO-/p/10580599.html