1015 水仙花数 1080 两个数的平方和 1082 与7无关的数 1083 矩阵取数问题 1087 1 10 100 1000

1015 水仙花数
水仙花数是指一个 n 位数 ( n >= 3 ),它的每个位上的数字的 n 次幂之和等于它本身。(例如:1^3 + 5^3 + 3^3 = 153)
给出一个整数M,求 >= M的最小的水仙花数。
 
Input
一个整数M(10 <= M <= 1000)
Output
输出>= M的最小的水仙花数
Input示例
99
Output示例
153
暴力求解,模拟都可以。
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<cstdio>
 6 using namespace std;
 7 
 8 int m;
 9 
10 int mi(int a,int b)
11 {
12     int res=1;
13     for (int i=1;i<=b;i++)
14         res*=a;
15     return res;    
16 }
17 int main()
18 {
19     scanf("%d",&m);
20     int x=m;
21     while(1)
22     {
23         int y=x,ans=0;
24         if (y>=100)
25         {
26             if (y>=1000)
27             for (int i=1;i<=4;i++)
28             {
29                 ans+=mi(y%10,4);
30                 y/=10;
31             }
32             else
33             for (int i=1;i<=3;i++)
34             {
35                 ans+=mi(y%10,3);
36                 y/=10;
37             }
38         }
39         else for (int i=1;i<=2;i++)
40              {
41                  ans+=mi(y%10,2);
42                  y/=10;
43              }
44         if (ans==x) break;
45         x++;
46     }
47     printf("%d\n",x);
48 }

1080 两个数的平方和


给出一个整数N,将N表示为2个整数i j的平方和(i <= j),如果有多种表示,按照i的递增序输出。
 
例如:N = 130,130 = 3^2 + 11^2 = 7^2 + 9^2 (注:3 11同11 3算1种)

Input
一个数N(1 <= N <= 10^9)
Output
共K行:每行2个数,i j,表示N = i^2 + j^2(0 <= i <= j)。
如果无法分解为2个数的平方和,则输出No Solution
Input示例
130
Output示例
3 11
7 9
根号以内枚举,找最小那个,然后找出大的那个,复杂度O(√n)
 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cmath>
 5 #include<cstring>
 6 using namespace std;
 7 
 8 int n;
 9 int boo=0;
10 
11 int main()
12 {
13     scanf("%d",&n);
14     int up=(int)sqrt(n);
15     for (int i=0;i<=up;i++)
16     {
17         int x=n-i*i;
18         x=(int)sqrt(x);
19         if (x*x+i*i==n&&x>=i) 
20         {
21             printf("%d %d\n",i,x);
22             boo=1;
23         }
24     }
25     if(!boo) printf("No Solution\n");
26 }

1082 与7无关的数


一个正整数,如果它能被7整除,或者它的十进制表示法中某个位数上的数字为7,则称其为与7相关的数。求所有小于等于N的与7无关的正整数的平方和。
 
例如:N = 8,<= 8与7无关的数包括:1 2 3 4 5 6 8,平方和为:155。

Input
第1行:一个数T,表示后面用作输入测试的数的数量。(1 <= T <= 1000)
第2 - T + 1行:每行1个数N。(1 <= N <= 10^6)
Output
共T行,每行一个数,对应T个测试的计算结果。
Input示例
5
4
5
6
7
8
Output示例
30
55
91
91
155
预处理1e6及以内的数的个数,然后一次线扫就可以了。
 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cmath>
 5 #include<cstring>
 6 using namespace std;
 7 
 8 typedef long long LL;
 9 const int NN=1e6+7;
10 
11 int T;
12 LL ans[NN]={0};
13 
14 int main()
15 {
16     scanf("%d",&T);
17     int x=1,y,z;
18     for (int i=1;i<=1000000;i++)
19     {
20         bool boo=0;
21         y=i;
22         while (y!=0)
23             if (y%10==7)
24             {
25                 boo=1;
26                 break;
27             }
28             else y/=10;
29         if (i%7==0) boo=1;
30         if (boo) ans[i]=ans[i-1];
31         else ans[i]=ans[i-1]+(LL)i*i;        
32     }
33     for (int i=1;i<=T;i++)
34     {
35         scanf("%d",&x);
36         printf("%lld\n",ans[x]);
37     }
38 }

1083 矩阵取数问题


一个N*N矩阵中有不同的正整数,经过这个格子,就能获得相应价值的奖励,从左上走到右下,只能向下向右走,求能够获得的最大价值。
 
例如:3 * 3的方格。
 
1 3 3
2 1 3
2 2 1
 
能够获得的最大价值为:11。

Input
第1行:N,N为矩阵的大小。(2 <= N <= 500)
第2 - N + 1行:每行N个数,中间用空格隔开,对应格子中奖励的价值。(1 <= N[i] <= 10000)
Output
输出能够获得的最大价值。
Input示例
3
1 3 3
2 1 3
2 2 1
Output示例
11
简单dp吧。

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cmath>
 5 #include<cstring>
 6 using namespace std;
 7 
 8 const int NN=507;
 9 int n;
10 int f[NN][NN];
11 
12 int main()
13 {
14     scanf("%d",&n);
15     for (int i=1;i<=n;i++)
16         for (int j=1;j<=n;j++)
17             scanf("%d",&f[i][j]);
18     for (int i=1;i<=n;i++)
19         for (int j=1;j<=n;j++)
20             f[i][j]=max(f[i][j]+f[i-1][j],f[i][j-1]+f[i][j]);
21     printf("%d\n",f[n][n]);                
22 }

1087 1 10 100 1000


1,10,100,1000...组成序列1101001000...,求这个序列的第N位是0还是1。
 

Input
第1行:一个数T,表示后面用作输入测试的数的数量。(1 <= T <= 10000)
第2 - T + 1行:每行1个数N。(1 <= N <= 10^9)
Output
共T行,如果该位是0,输出0,如果该位是1,输出1。
Input示例
3
1
2
3
Output示例
1
1
0
发现1的位置规律,然后大约o(√n)的复杂度,累加的过程。
 1 #include<cstdio>
 2 #include<map>
 3 using namespace std;
 4 
 5 int T;
 6 map<int,int>p;
 7 
 8 int main()
 9 {
10     int x=2,y=1;
11     p[1]=1;
12     for (;x<=1e9;x+=y)
13     {
14         p[x]=1;
15         y++;
16     }
17     scanf("%d",&T);
18     while (T--)
19     {
20         scanf("%d",&x);
21         printf("%d\n",p[x]);
22     }
23 }
posted @ 2017-07-31 00:53  Kaiser-  阅读(429)  评论(0编辑  收藏  举报