寒假Day50:51nod3047位移运算

用n条直线,划分平面,最多能够划分为多少块? (n+1)*n/2+1  3->7

 

3047 位移运算

题目链接:http://www.51nod.com/Challenge/Problem.html#problemId=3047

题意:

给出两个数a,b。问a能否只通过位移运算( >>和 << 可以多次使用)变成b。如果可以输出"Yes",否则输出"No"。

思路:

把a和b分别转换成二进制,去掉reverse过的b对应的数组的后导零,我的代码没有reverse,所以去前导零即可,最后判断b是否是a的字串即可(简单strstr就可以)

注意事项:

数字1->字符串'1'     +‘0’

串b是否是串a的字串  strstr(a,b)是的话为if为真

带有修改的代码(AC的):(需要反思)

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 char a[1500],b[1500];
 5 
 6 int main()
 7 {
 8     int t,x,y;
 9     scanf("%d",&t);
10     while(t--)
11     {
12         memset(a,'\0',sizeof(a));
13         memset(b,'\0',sizeof(b));
14         scanf("%d %d",&x,&y);
15         int p=0,q=0;
16         while(x)
17         {
18             // a[p++]=(char)(x%2);
19             a[p++]=x%2+'0';
20             x/=2;
21         }
22         while(y)
23         {
24             b[q++]=y%2+'0';
25             y/=2;
26         }
27 //        for(int i=0;i<q;i++)
28 //        {
29 //            if(b[i]=='0')
30 //            {
31 
32 //                for(int j=0;j<q;j++)
33 //                    b[j]=b[j+1];
34 //                b[q-1]='\0';
35 //                q--;
36 //            }
37 //            else
38 //                break;
39 //        }
40         while(b[0]=='0')
41         {
42             for(int i=0; i<q; i++)
43                 b[i]=b[i+1];
44             b[q-1]='\0';
45             q--;
46         }
47         //printf("%s %s\n",a,b);
48         if(strstr(a,b))
49             cout<<"Yes"<<endl;
50         else
51             cout<<"No"<<endl;
52     }
53     return 0;
54 }
View Code

最后代码:

#include <bits/stdc++.h>
using namespace std;

char a[1500],b[1500];

int main()
{
    int t,x,y;
    scanf("%d",&t);
    while(t--)
    {
        memset(a,'\0',sizeof(a));
        memset(b,'\0',sizeof(b));
        scanf("%d %d",&x,&y);
        int p=0,q=0;
        while(x)
        {
            a[p++]=x%2+'0';
            x/=2;
        }
        while(y)
        {
            b[q++]=y%2+'0';
            y/=2;
        }

        while(b[0]=='0')
        {
            for(int i=0; i<q; i++)
                b[i]=b[i+1];
            b[q-1]='\0';
            q--;
        }
        if(strstr(a,b))
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/OFSHK/p/12494821.html