HDU 1719 Friend 规律题

解题报告:规定数字1和数字2是Friend number,然后规定,若a和b是Friend number,那么a*b+a+b也是Friend number,输入一个数,让你判断这个数是否是Friend number。

Friend number = (2^x)*(3^y)-1;如果判断出某个数能满足这个关系,则这个数就是Friend number。

 1 #include<cstdio>
 2 const int MAX = 2<<30-1;
 3 int main() {
 4     int n;
 5     while(scanf("%d",&n)!=EOF) {
 6         if(n == 0) {
 7             printf("NO!
");
 8             continue;
 9         }
10         int c1 = 1,c2,f = 0;
11         for(int i = 0;i<30;++i) {
12             c2 = 1;
13             if(c1 > MAX || f)
14             break;
15             for(int j = 0;j<30;++j) {
16                 if(c2 >= MAX || c1*c2 >= MAX)
17                 break;
18                 if(c1*c2-1==n) {
19                     f = 1;
20                     break;
21                 }
22                 c2 *= 3;
23             }
24             c1 *= 2;
25         }
26         printf(f? "YES!
":"NO!
");
27     }
28     return 0;
29 }
View Code
原文地址:https://www.cnblogs.com/xiaxiaosheng/p/3257459.html