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("\n");
31 
32     return 0;
33 }

 

 
 
 

 

转载于:https://www.cnblogs.com/0xiaoyu/p/11345596.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值