紫书 UVA679

给下落的深度和小球个数,小球依次下落,结点有个开关,每到一个结点,开关关向左,开向右


一开始想到了简单模拟,结果超时…

[cpp]  view plain  copy
  1. #include <cstdio>  
  2. #include <iostream>  
  3. #include <cstring>  
  4. #define maxn 20   
  5. using namespace std;  
  6.   
  7. int s[1<<maxn];  
  8.   
  9. int main(){  
  10.     int n,D,I,now=1;  
  11.     scanf("%d",&n);  
  12.     while(n--){  
  13.         scanf("%d%d",&D,&I);  
  14.         int last=(1<<D)-1;//最后结点,临界   
  15.     //  cout<<"last="<<last<<endl;  
  16.         memset(s,0,sizeof(s));  
  17.         while(I--){  
  18.             now=1;  
  19.             while(1){     
  20.               
  21.                 if(s[now]==0){  
  22.                     int temp= 2*now;  
  23.                     s[now]=1;  
  24.                     now=temp;  
  25.                 }  
  26.                 else{  
  27.                     int temp= 2*now+1;  
  28.                     s[now]=0;  
  29.                     now=temp;     
  30.                 }  
  31.                       
  32.                 if(now>last) break;            
  33.             }  
  34.               
  35.               
  36.         }   
  37.           
  38.         printf("%d\n",now/2);  
  39.     }  
  40.       
  41.     return 0;  
  42. }  



后来开了刘汝佳老师算法入门,发现缺失需要多读题,注意细节,多思考。

I小球个数的奇偶性很有关系。奇数的话也是往左走的(I+1)/2个小球,偶数是往右走的I/2个小球。


[cpp]  view plain  copy
  1. #include <cstdio>  
  2. #include <iostream>  
  3. #include <cstring>  
  4. #define maxn 20   
  5. using namespace std;  
  6.   
  7. int s[1<<maxn];  
  8.   
  9. int main(){  
  10.     int n,D,I,now=1;  
  11.     scanf("%d",&n);  
  12.     while(n--){  
  13.         scanf("%d%d",&D,&I);  
  14.         int k=1;  
  15.         for(int i =0;i<D-1;i++)  
  16.         {  
  17.             if(I%2!=0){  
  18.                 k=2*k;  
  19.                 I=(I+1)/2;  
  20.             }  
  21.             else{  
  22.                 k=2*k+1;  
  23.                 I=I/2;  
  24.             }  
  25.         }  
  26.         printf("%d\n",k);  
  27.     }  
  28.       
  29.     return 0;  
  30. }  

转载于:https://www.cnblogs.com/dillydally/p/9567826.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值