• Double it(水题模拟+逆向思维)


    Alex has two magic machines A and B. Machine A will give you 2x + 1 coins if you insert x coins in it, machine B will give you 2x + 2. Alex has no coins and wants to get exactly n coins in order to buy a new unicorn, but he can't figure out how to do it. Your task is to find a way to use the machines to get exactly n coins.

    Input

    The input consists of a single line containing n(1 ≤ n ≤ 109).

    Output

    For each one output a string of A's and B's giving the order in which the machines are used.

    Examples

    Input
    7
    Output
    AAA

    Input
    10
    Output
    ABB
    这道题写了好长时间才过,最后我只能说读题很重要!
     
     
    题意:
    利用A,B两台机器可以获得硬币,规则如下:
    ya = 2 * x + 1;
    yb = 2 * x + 2; 
    x为投进机器的硬币数
    刚开始没硬币,想获得恰好n枚硬币,问投硬币的顺序如何
    没钱也能钱生钱,你说这气不气人
     
     
    思路:
    可以以n为起点往后推,若当前硬币数为偶数,则一定是从B来的,否则则是A
     
     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 char re[1000000];
     5 
     6 int main()
     7 {
     8     long long n, cur, i;
     9     scanf("%lld", &n);
    10     cur = 0;
    11     
    12     while(n!=0)
    13     {
    14         if(n%2==0)
    15         {
    16             re[cur++] = 'B';
    17             n = (n-2)/2;
    18         }
    19         else
    20         {
    21             re[cur++] = 'A';
    22             n = (n-1)/2;
    23         }
    24     }
    25 
    26     for(i=cur-1;i>=0;i--)
    27     {
    28         printf("%c", re[i]);
    29     }
    30     printf("
    ");
    31 
    32     return 0;
    33 }
     
     
     
  • 相关阅读:
    pkg_resources.DistributionNotFound: The 'catkin-pkg==0.4.9' distribution was not found
    gsl库安装
    json ubuntu下安装
    系统安装情况以及深度学习环境搭建
    ros 编程习惯
    ubuntu系统ftp连接 以及ssh连接
    redmine问题
    maven仓库私服配置
    SVN配置管理(trunk、branches、tags)
    Gitolite配置管理和GIT基本操作
  • 原文地址:https://www.cnblogs.com/0xiaoyu/p/11345596.html
Copyright © 2020-2023  润新知