题解

188 条题解

  • 4
    @ 2017-05-08 12:42:39
    /*
    好水的模拟题OTZ
    我们可以直接用位运算来做
    于是有了高大上写法
    而我这种弱弱只能模拟进制转换来做了
    唉我好弱啊
    注意这里要用unsigned int而不是int
    不然会爆负
    */
    /*
    高大上写法Orz
    #include <iostream>
    using namespace std;
    unsigned long int n;
    int main()
    {
        cin>>n;
        cout<<((n << 16) | (n >> 16))<<endl;
    }
    */
    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    
    int a[33];
    unsigned int ans;
    int k;
    int l;
    
    int main()
    {
        cin>>k;
        while(k)
        {
            a[++l]=k%2;
            k/=2;
        }
        for(int i=1;i<=16;i++)
            swap(a[i],a[i+16]);
        for(int i=1;i<=32;i++)
            ans+=(1<<(i-1))*a[i];
        cout<<ans<<endl;
        return 0;
    }
         
    
  • 2
    @ 2019-01-05 20:08:43

    还行c++

    #include <iostream>
    
    using namespace std ;
    
    unsigned int n , a , b ; 
    
    int main()
    {
        cin >> n ;
        a = n / 65536 ;
        b = n % 65536 ;
        cout << b * 65536 + a ;
        return 0 ;
    }
    
  • 1
    @ 2023-05-19 23:55:55

    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
    int k[32]={},t,j,w,pf=31;
    long long int a;
    unsigned long long kk=1,b=0;
    cin>>a;
    for(j=31;j>=0;j--){
    k[j]=a%2;
    a-=k[j];
    a/=2;
    }
    for(j=0;j<16;j++){
    t=k[j];
    k[j]=k[j+16];
    k[j+16]=t;
    }
    for(j=0;j<32;j++){
    if(k[j]!=0){
    for(w=0;w<pf;w++){
    kk*=2;
    }
    b+=kk;
    kk=1;
    }
    pf--;
    }
    cout<<b;
    }

  • 1
    @ 2021-01-08 10:34:20
    #include <cstdio>
    using namespace std;
    int main()
    {
        unsigned int a,b;
        scanf("%u",&a);
        b=(a<<16)|(a>>16);
        printf("%u\n",b);
    }
    
  • 1
    @ 2018-03-30 10:55:10

    不会位操作,只能模拟咯
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>

    int main()
    {
    unsigned int n,ans=0;
    scanf("%u",&n);
    // printf("This is unsigned int n: %u\n",n);
    int i,j;
    char p[32],q[16];
    for(i=0;i<32;i++)
    p[i]=0;
    for(i=0;i<32;i++)
    {
    p[i]=n%2;
    n=n/2;
    //printf("%d",p[i]);
    if(n==0) break;
    }//printf("\n");
    // for(i=0;i<32;i++) printf("%d",p[i]); printf("\n");
    for(i=0;i<16;i++)
    {
    q[i]=p[i];
    p[i]=p[i+16];
    p[i+16]=q[i];
    }
    // for(i=0;i<32;i++) printf("%d",p[i]); printf("\n");
    for(i=0;i<32;i++)
    ans+=pow(2,i)*p[i];
    printf("%u",ans);
    }

  • 1
    @ 2016-12-21 17:36:31

    楼下有人用这个,强大。一下子忽略了位运算,学习下
    ```c++
    #include <iostream>
    using namespace std;
    unsigned long int n;
    int main(){
    cin>>n;
    cout<<((n << 16) | (n >> 16))<<endl;
    }

  • 1
    @ 2016-11-17 17:33:03

    #include<cmath>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    int n,sum=0,m=32;
    int a[35]={0};
    int main()
    {
    cin>>n;
    while(n>0)
    {
    if(n<=0)break;
    if(n%2==1)
    {
    a[m]=1;m--;n=(n-1)/2;
    }
    else
    {
    m--;n=n/2;
    }
    }
    int c[35]={0};
    for(int i=1;i<=16;i++)
    { c[i]=a[i+16];}
    for(int i=17;i<=32;i++)
    {c[i]=a[i-16];}
    for(int i=32;i>0;i--)
    {
    sum+=c[i]*pow(2,32-i);//乘方函数
    }
    cout<<sum;
    return 0;

    }

  • 1
    @ 2015-08-02 20:52:23

    #include <iostream>
    #include <stdio.h>
    using namespace std;
    int num2[33];
    int num_2[33];
    int main()
    {
    int n;
    scanf("%d",&n);
    for(int i=1;;i++)
    {
    if(n==0)break;
    if(n%2==1)
    num2[i]=1;
    n/=2;
    }
    for(int i=1;i<=16;i++)
    {
    num_2[i]=num2[i+16];
    num_2[i+16]=num2[i];
    }
    long long ans=0;
    long long n_2=1;
    for(int i=1;i<=32;i++)
    {
    if(num_2[i])ans+=n_2;
    n_2*=2;
    }
    printf("%lld",ans);
    }

  • 0
    #include <bits/stdc++.h>
    using namespace std;
    int main(){
            long long n;
            cin>>n;
            cout<<(((n&65535)<<16)|((n&4294901760)>>16));//前16位与后16位交换
            /*65535=0x0000ffff
              4294901760=0xffff0000*/
    }
    
    • “>>”与“<<”
      表示二进制移位
      eg:1100>>2=0011
      110011<<2=1100

    • “|”
      表示二进制或
      eg:1|0=1
      0|1=1
      1|1=1
      0|0=0

  • 0
    @ 2016-12-21 17:32:05

    感觉测试数据不强,输入用int就够,条件说输入在2^32内,我试了2^32-1我的程序并不可以啊。建议以后做题遇见2^32输入用long long。
    ```c++
    #include<cstdio>
    #include<cmath>
    #include<cstring>

    int main()
    {

    int n,i=0;
    int q;
    int sh[32]= {0};
    int ch[32]= {0};
    scanf("%d",&n);
    //将数字转换为二进制
    while(n != 0)
    {
    sh[i++] = n%2;
    n = n/2;
    }
    //颠倒过来
    for(int j=0; j < 32; j++)
    {
    ch[31-j] = sh[j];
    }
    //换下位置
    for(int j=0; j < 16; j++)
    {
    q = ch[j];
    ch[j] = ch[16+j];
    ch[16+j]=q;
    }
    //输出数字,注意使用long long,因为long不够2^32-1
    long long sum = 0;
    for(int j=0; j < 32; j++)
    {
    if(ch[31-j] == 1)
    sum += (long long)pow(2,j);//注意使用long long,因为long不够2^32-1
    }
    printf("%lld\n",sum);

    return 0;
    }

    • @ 2021-01-08 10:35:26
      unsigned int
      

      就可以

  • 0
    @ 2016-08-02 11:18:22
    #include <iostream>
    #include <cmath>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cstdlib>
    #include <vector>
    #include <queue>
    #include <bitset>
    #define min(a,b) (a>b?b:a)
    #define max(a,b) (a>b?a:b)
    #pragma comment(linker, "/STACK:1024000000,1024000000")
    
    using namespace std;
    typedef long long lg;
    #define INF 1<<30 
    
    int a;
    
    
    int main()
    {
        ios::sync_with_stdio(0);
        cin>>a;
        bitset<32>x(a),y;
        for(int i=0;i<16;i++)
            y[i]=x[i+16];
        for(int i=16;i<32;i++)
            y[i]=x[i-16];
        //cout<<x<<endl<<y;
        cout<<y.to_ulong();
        return 0;
    }
    
  • 0
    @ 2016-07-30 08:59:24
    #include <iostream>
    using namespace std;
    
    long long N = 0,nFront16 = 0,nLast16 = 0;
    int main(){
        cin >> N;
        //取前16位
        nFront16 = N & 4294901760LL;
        nFront16 >>= 16;
        //取后16位
        nLast16 = N & 65535;
        //左移16位
        nLast16 <<= 16;
        //合成新数
        nLast16 += nFront16;
        
        cout << nLast16 << endl;
        return 0;
    }
    
  • 0
    @ 2016-06-19 11:45:55

    位运算
    注意:
    1.unsgined long int
    2.对此题来说写|和+是一样的,因为其余位都是0

    #include <iostream>
    using namespace std;
    unsigned long int n;
    int main(){
    cin>>n;
    cout<<((n << 16) | (n >> 16))<<endl;
    }

    • @ 2016-06-19 11:46:35
      #include <iostream>
      using namespace std;
      unsigned long int n;
      int main(){
          cin>>n;
          cout<<((n << 16) | (n >> 16))<<endl;
      }
      
  • 0
    @ 2016-03-20 19:45:58

    var n:int64;
    begin
    read(n);
    writeln((n mod 65536)*65536+n div 65536);
    end.

  • 0
    @ 2016-03-19 12:06:09

    太坑了。
    #include <iostream>
    #include <string>

    using namespace std;

    int zhuang2_10(string a1)
    {
    int Ia, Idang, ITemp, Iwei,Ians;
    string Sa;
    Sa = a1;
    Iwei = Sa.size();
    Ia = 0;
    Ians = 0;
    for (ITemp = 0; ITemp < Iwei; ITemp++)
    {
    Idang = Sa[ITemp] - '0';
    Ians = Ians * 2 + Idang;
    }
    return Ians;
    }
    string zhuang10_2(int a)
    {
    int yu,Ia;
    string Sa,Syu;
    Ia = a;
    Sa = "";
    while (Ia!= 0)
    {
    yu = Ia % 2;
    Ia = Ia / 2;
    Syu = yu + '0';
    Sa = Syu+Sa;
    }
    return(Sa);
    }
    string bu(string Sa)
    {
    int Ichang, Temp,Ibu;
    string Sb;
    Sb = Sa;
    Ichang = Sb.size();
    if (Ichang >= 32)
    return Sb;
    else
    {
    Ibu = 32 - Ichang;
    for (Temp = 1; Temp <= Ibu; Temp++)
    Sb = '0' + Sb;
    return Sb;
    }
    }
    string Swap(string a)
    {
    string Sa;
    char Ca;
    int i;
    Sa = a;
    for (i = 0; i < 16; i++)
    {
    Ca = Sa[i];
    Sa[i] = Sa[16+i];
    Sa[16+i] = Ca;
    }
    return Sa;
    }
    int main()
    {
    unsigned a;
    string Sa;
    cin >> a;
    Sa = zhuang10_2(a);
    Sa = bu(Sa);
    Sa = Swap(Sa);
    a = zhuang2_10(Sa);
    cout << a;
    return 0;
    }

  • 0
    @ 2015-11-03 09:42:37

    Program P1201;

    Var i:longint;n,sum:int64;
    a,b:array[1..33]of longint;
    c:array[1..33]of int64;

    Begin
    readln(n);i:=32;
    while n>0 do begin
    a[i]:=n mod 2;
    dec(i);
    n:=n div 2;
    end;
    for i:=1 to 16 do b[i]:=a[16+i];
    for i:=17 to 32 do b[i]:=a[i-16];
    c[32]:=1;
    for i:=31 downto 1 do c[i]:=c[i+1]*2;
    sum:=0;
    for i:=1 to 32 do sum:=c[i]*b[i]+sum;
    writeln(sum);
    end.

  • 0
    @ 2015-10-25 21:08:48

    很简单的一道题目,就是个二进制运算,注意是unsigned int不是int就好了
    测试数据 #0: Accepted, time = 0 ms, mem = 524 KiB, score = 10
    测试数据 #1: Accepted, time = 0 ms, mem = 524 KiB, score = 10
    测试数据 #2: Accepted, time = 15 ms, mem = 528 KiB, score = 10
    测试数据 #3: Accepted, time = 0 ms, mem = 520 KiB, score = 10
    测试数据 #4: Accepted, time = 15 ms, mem = 520 KiB, score = 10
    测试数据 #5: Accepted, time = 0 ms, mem = 524 KiB, score = 10
    测试数据 #6: Accepted, time = 0 ms, mem = 520 KiB, score = 10
    测试数据 #7: Accepted, time = 0 ms, mem = 524 KiB, score = 10
    测试数据 #8: Accepted, time = 1 ms, mem = 520 KiB, score = 10
    测试数据 #9: Accepted, time = 15 ms, mem = 520 KiB, score = 10
    Accepted, time = 46 ms, mem = 528 KiB, score = 100
    代码
    #include <iostream>
    using namespace std;
    int main()
    {
    unsigned int x;
    cin>>x;
    cout<<(unsigned int)(((x<<16)&0xFFFF0000)|((x>>16)&0x0000FFFF))<<endl;
    return 0;
    }

  • 0
    @ 2015-10-18 15:49:29

    测试数据 #0: Accepted, time = 1 ms, mem = 520 KiB, score = 10
    测试数据 #1: Accepted, time = 0 ms, mem = 524 KiB, score = 10
    测试数据 #2: Accepted, time = 15 ms, mem = 524 KiB, score = 10
    测试数据 #3: Accepted, time = 0 ms, mem = 520 KiB, score = 10
    测试数据 #4: Accepted, time = 0 ms, mem = 524 KiB, score = 10
    测试数据 #5: Accepted, time = 0 ms, mem = 524 KiB, score = 10
    测试数据 #6: Accepted, time = 1 ms, mem = 524 KiB, score = 10
    测试数据 #7: Accepted, time = 15 ms, mem = 524 KiB, score = 10
    测试数据 #8: Accepted, time = 0 ms, mem = 524 KiB, score = 10
    测试数据 #9: Accepted, time = 1 ms, mem = 524 KiB, score = 10
    Accepted, time = 33 ms, mem = 524 KiB, score = 100

  • 0
    @ 2015-10-13 00:08:45

    var m,n:int64;
    begin
    readln(n);
    m:=1 shl 16;
    n:=(n mod m)shl 16+n shr 16;
    writeln(n);
    end.
    {________________________________________}
    var s:longword;
    begin
    read(s);
    write(swap(s));
    end.

  • 0
    @ 2015-08-13 22:00:15

    var
    s:longword;
    begin
    readln(s);
    writeln(swap(s));
    end.

信息

ID
1201
难度
3
分类
模拟 点击显示
标签
(无)
递交数
3713
已通过
1828
通过率
49%
被复制
13
上传者