签到

06月21日
尚未签到

共有回帖数 0

    奈何情殇

    等级:
    有这样一个奇怪的天平,它的砝码只有1g, 3g, 9g, 27g......
    并且各只有一个,但它却能够称出所有整数g质量的物体
    例如要称出质量2g,那就一边放一个3g,另一边放物体和1g的
    砝码就可以了。放在物体那边的砝码在前面加负号表示之。

    输入:
    多组测试数据,每行只有一个正整数n,表示要称的物体质量,
    n在32bit有符号整数所能表示的数的范围内,
    最后请使用EOF结束本程序

    输出:
    怎么称质量n出来

    样例输入:
    2
    3
    4
    5

    样例输出:
    2 = 3 - 1
    3 = 3
    4 = 3 + 1
    5 = 9 - 3 - 1
    #include stdio.h

    unsigned three[21] = {1};
    int s[21];

    int main()
    {
       int n;
       for (int i = 1; i  21; ++i)
           three = three[i-1]*3;
       while (scanf("%d", &n) != EOF)
       {
           int carry = 0, i;
           printf("%d = ", n);
           for (i = 0; n; n /= 3, ++i)
               if (carry = (s = n%3+carry)  1)
                   s -= 3;
           if (carry) s[i++] = carry;
           printf("%u", three[--i]);
           while (i)
               if (s[--i])
                   printf(" %c %u", s  0 ? '+' : '-', three);
           putchar('n');
       }
       return 0;
    }


    楼主 2015-12-31 13:00 回复

共有回帖数 0
  • 回 帖
  • 表情 图片 视频
  • 发表

登录直线网账号

Copyright © 2010~2015 直线网 版权所有,All Rights Reserved.沪ICP备10039589号 意见反馈 | 关于直线 | 版权声明 | 会员须知