编程介的小学生 2017-01-24 08:16 采纳率: 20.5%
浏览 889
已采纳

Friend

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!

  • 写回答

1条回答 默认 最新

  • octopusflying 2017-01-24 12:56
    关注

    不一定对,你试试吧。

     #include<stdio.h>
    #include<math.h>
    
    int main(void) {
        long long number;
    
        while(scanf("%lld", &number) != EOF) {
            number++;
            int result = 0;
            for(int i = 0; i <= 30; i++) {
                if(pow(2, i) > number) {
                    break;
                }
                double r = log(number / (double) pow(2, i)) / log(3.0);
                long long llr = (long long) r;
                if(r - llr < 1e-9 && r - llr > -1e-9) {
                    result = 1;
                }
            }
    
            if(result) {
                printf("YES!\n");
            } else {
                printf("NO!\n");
            }
        }
    
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器