zoukankan      html  css  js  c++  java
  • hdu 5969 最大的位或

    最大的位或

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 655    Accepted Submission(s): 293


    Problem Description

    B君和G君聊天的时候想到了如下的问题。
    给定自然数l和r ,选取2个整数x,y满足l <= x <= y <= r ,使得x|y最大。
    其中|表示按位或,即C、 C++、 Java中的|运算。

     

    Input

    包含至多10001组测试数据。
    第一行有一个正整数,表示数据的组数。
    接下来每一行表示一组数据,包含两个整数l,r。
    保证 0 <= l <= r <= 1018

     

    Output

    对于每组数据输出一行,表示最大的位或。

     

    Sample Input

    5
    1 10
    0 1
    1023 1024
    233 322
    1000000000000000000 1000000000000000000

    Sample Output

    15
    1
    2047
    511
    1000000000000000000
     
     
     
    //第一次做位运算有关的题目,贪心方案想了好久。。。
      1 #include <stdio.h>
      2 #include <string.h>
      3 
      4 typedef long long LL;
      5 
      6 LL l,r,Max;
      7 char bit_l[300];
      8 char bit_r[300];
      9 char temp[300];
     10 
     11 LL StrToNum(char s[])//二进制字符变数字
     12 {
     13     int len=strlen(s);
     14     LL ans=0,res=1;
     15     for (int i=len-1;i>=0;i--)
     16     {
     17         if (s[i]=='1')
     18             ans+=res;
     19         res*=2;
     20     }
     21     return ans;
     22 }
     23 
     24 void NumToStr(LL x)//数字变二进制字符串
     25 {
     26     if (x==0)
     27     {
     28         temp[0]='0';
     29         temp[1]='';
     30         return ;
     31     }
     32     char s[300];
     33     int pos=0;
     34     while (x!=0)
     35     {
     36         int tt=x%2;
     37         if (tt==1)
     38             s[pos++]='1';
     39         else if (tt==0)
     40             s[pos++]='0';
     41         x/=2;
     42     }
     43     s[pos]='';
     44     int i;
     45     for (i=0;i<pos;i++)
     46         temp[i]=s[pos-1-i];
     47     temp[i]='';
     48 }
     49 
     50 int main()
     51 {
     52     int i,j,t;
     53     char test[300];
     54     scanf("%d",&t);
     55     while (t--)
     56     {
     57         Max=-100;
     58         scanf("%I64d%I64d",&l,&r);
     59         if (l==r)
     60         {
     61             printf("%lld
    ",l|r);
     62             continue;
     63         }
     64         NumToStr(l);
     65         strcpy(bit_l,temp);
     66         NumToStr(r);
     67         strcpy(bit_r,temp);
     68 
     69         strcpy(test,bit_r);
     70         int len_l=strlen(bit_l);
     71         int len_r=strlen(bit_r);
     72         for (i=0;i<len_r-len_l;i++)
     73         {
     74             test[i]='0';
     75         }
     76         for (j=0;j<=len_l;j++)
     77         {
     78             test[i++]=bit_l[j];
     79         }
     80         strcpy(bit_l,test);
     81         for (i=0;i<len_r;i++)
     82         {
     83             if (bit_l[i]=='1'||bit_r[i]=='1')
     84                 test[i]='1';
     85             else
     86                 test[i]='0';
     87         }
     88         test[i]='';
     89         LL res;
     90         res=StrToNum(test);
     91         if (res>Max) Max=res;
     92         //printf("l : %s
    ",bit_l);
     93         //printf("r : %s
    ",bit_r);
     94         //printf("t : %s
    ",test);
     95         int k=0;
     96         while (bit_l[k]==bit_r[k]) k++;
     97         k++;
     98         for (i=k;i<len_r;i++)
     99         {
    100             res=StrToNum(test);
    101             if (res>Max) Max=res;
    102             if (bit_l[i]=='0'&&bit_r[i]=='0')
    103             {
    104                 test[i]='1';
    105                 res=StrToNum(test);
    106                 if (res>Max)
    107                     Max=res;
    108             }
    109         }
    110         printf("%I64d
    ",Max);
    111     }
    112     return 0;
    113 }
    View Code
  • 相关阅读:
    解决 Mac launchpad 启动台 Gitter 图标无法删除的问题
    React 与 React-Native 使用同一个 meteor 后台
    解决 React-Native mac 运行报错 error Failed to build iOS project. We ran "xcodebuild" command but it exited with error code 65. To debug build logs further, consider building your app with Xcode.app, by ope
    一行命令更新所有 npm 依赖包
    swift学习笔记
    IOS语言总结
    focusSNS学习笔记
    别小看锤子,老罗真的很认真
    windowsphone开发页面跳转到另一个dll中的页面
    【令人振奋】【转】微软潘正磊谈DevOps、Visual Studio 2013新功能、.NET未来
  • 原文地址:https://www.cnblogs.com/haoabcd2010/p/6105396.html
Copyright © 2011-2022 走看看