守形数

 题目截图:

 

思路:

  首先,只有尾数为 0,1,5,6 的数才可能为守形数,所以其他可以直接排除。然后对其他的数进行判断即可。

 

代码如下:

 1 /*
 2     守形数
 3 */
 4 
 5 #include <stdio.h>
 6 #include <string.h>
 7 #include <math.h>
 8 #include <stdlib.h>
 9 #include <time.h>
10 #include <stdbool.h>
11 
12 int main() {
13     int N;
14     while(scanf("%d", &N) != EOF) {
15         int g = N%10;                        // 提取尾数 
16         // 只有尾数为 0,1,5,6 的数才可能为守形数
17         if(g==1 || g==5 || g==6 || g==0) {
18             int t = N*N;            
19             int ans=10;                        // 用来提取低位部分 
20             if(N > 10) {
21                 ans = 100;
22             } 
23             if(t%ans == N) {                // 判断是否是守形数 
24                 printf("Yes!\n");
25             } else {
26                 printf("No!\n");
27             }
28         } else {
29             printf("No!\n");
30         }
31     } 
32 
33     return 0;
34 }

 

posted @ 2018-02-08 17:55  Just_for_Myself  阅读(426)  评论(0编辑  收藏  举报