Friend

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2234    Accepted Submission(s): 1121

Problem Description
Friend number are defined recursively as follows.

(1) numbers 1 and 2 are friend number;

(2) if a and b are friend numbers, so is ab+a+b;

(3) only the numbers defined in (1) and (2) are friend number.

Now your task is to judge whether an integer is a friend number.
 
Input
There are several lines in input, each line has a nunnegative integer a, 0<=a<=2^30.
 
Output
For the number a on each line of the input, if a is a friend number, output “YES!”, otherwise output “NO!”.
 
Sample Input
3
13121
12131
 
Sample Output
YES!
YES!
NO!
 
思路: 设c是Friend数,题中给出公式ab+a+b。则c=ab+a+b。c+1=ab+a+b+1=(a+1)*(b+1),所以推断n+1是否满足这个公式就可以。又由于1,2是最小的Friend数。 所以推断n+1=(a+1)^x*(b+1)^y即n+1=2^x*3^y是否成立。
 
 
代码:
 
#include<stdio.h>
int main()
{
int n,x,y;
while(scanf("%d",&n)!=EOF)
{
n+=1;//n先加1;
x=0;y=0;
while(n%2==0)
{
n=n/2;
x++;
}
while(n%3==0)
{
n=n/3;
y++;
}
if(n==1&&(x>0||y>0))//n=0时不是Friend数。
printf("YES!\n");
else
printf("NO!\n");
}
return 0;
}

最新文章

  1. Be Better:遇见更好的自己-2016年记
  2. websocket初探
  3. nodejs渲染模板
  4. 去除utf8文件的bom标记
  5. 项目跑起来之后,一会儿后台就会报错Illegal access: this web application instance has been stopped already. Could not load [com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask]. The following stack trace
  6. ArcGIS API for JavaScript开发环境搭建及第一个实例demo
  7. 为什么一个object_id在dba_objects中为什么查不到记录?
  8. java作业——整数相加
  9. Customize R initiallization in Mac
  10. 【django】django学得好迷茫啊 来个学习规划吧
  11. 使用SQL Server 2008远程链接时SQL数据库不成功的解决方法
  12. IOS-沙盒机制(一 简述)
  13. VSTO 学习笔记(十一)开发Excel 2010 64位自定义公式
  14. temp-重庆农商行二次出差
  15. Swift处理异常
  16. 使用Python批量修改文件名
  17. 简单的线程Thread使用
  18. 系统目录结构/ls命令/文件类型/alias命令
  19. Dubbo调用链(version:2.5.3)
  20. sublime text 显示 typescript高亮

热门文章

  1. 【codeforces 757E】Bash Plays with Functions
  2. SQL SERVER-集合操作
  3. HDU 4313 Contest 2
  4. java之 ------ DAO设计模式的【具体解释】及常见设计模式的【应用】
  5. java之IO处理
  6. MapReduce(十六): 写数据到HDFS的源代码分析
  7. C中操作文件的几种模式
  8. [jQuery] 选择器和事件
  9. 基于对话框的应用程序,点击button打开一个网页
  10. ThinkPHP5.0框架开发--第2章 TP5.0架构