当前位置:编程学习 > C/C++ >>

NOJ 1303 判断a+b是否溢出

[1303] A + B
时间限制: 1000 ms 内存限制: 65535 K
问题描述
As we all know, in the computer science, an integer A is in the range of 32-signed integer, which means the integer A is between -2^31 and (2^31)-1 (inclusive) and A is a 64-signed integer, which means A is between -2^63 and (2^63)-1(inclusive). Now we give the K-signed range, and two K-signed integers A and B, you should check whether the sum of A and B is beyond the range of K-signed integer or not.

输入
There will be many cases to calculate. In each case, there comes the integer K (2<=K<=64) first in a single line.
Then following the line, there is another single line which has two K-signed integers A and B.
输出
For each case, you should estimate whether the sum is beyond the range. If exceeded, print “Yes”, otherwise “I love nbut”.
样例输入
64
1000 1000样例输出
I love nbut提示
无来源
加多宝凉茶操作
    

http://acm.nbut.cn/Problem/view.xhtml?id=1303
题意  :输入k a  b 表示 k位大小的数据  如 int 是32位  long long 是 64位  k最大为64   输入a b  问a+b的和是否溢出
 
思路:
我们用long long 存储a b  对于k<64  a+b不会溢出 直接根据k位数据范围判断 
当 k为64 则可能溢出  我们可以用 a+b=c  判断 c-a是不是等于b c-b是不是等于a  
或者用最下面的方法看代码 2种处理都不错
 
[cpp]
#include<stdio.h>  
#include<math.h>  
int main() 

    int k; 
    __int64 a,b,c,left,right,max,min; 
    while(scanf("%d",&k)!=EOF) 
    { 
        scanf("%I64d %I64d",&a,&b); 
        if(k==64) 
        { 
        c=a+b; 
        if(c-a!=b||c-b!=a) 
        { 
            printf("Yes\n"); 
        } 
        else printf("I love nbut\n"); 
        continue; 
        } 
 
        left=-1;right=1; 
        int i; 
            for(i=1;i<=k-1;i++) 
            { 
                left*=2; 
                right*=2; 
            } 
            right-=1; 
            c=a+b; 
            if(left<=c&&c<=right) 
            { 
                printf("I love nbut\n"); 
               
            }  
            else printf("Yes\n"); 
         
 
    } 
    return 0; 

#include<stdio.h>
#include<math.h>
int main()
{
 int k;
 __int64 a,b,c,left,right,max,min;
 while(scanf("%d",&k)!=EOF)
 {
  scanf("%I64d %I64d",&a,&b);
        if(k==64)
  {
     c=a+b;
  if(c-a!=b||c-b!=a)
  {
   printf("Yes\n");
  }
  else printf("I love nbut\n");
  continue;
  }

  left=-1;right=1;
  int i;
   for(i=1;i<=k-1;i++)
   {
    left*=2;
    right*=2;
   }
   right-=1;
   c=a+b;
   if(left<=c&&c<=right)
   {
    printf("I love nbut\n");
    
   }
   else printf("Yes\n");
  

 }
 return 0;
}
[cpp]
#include<stdio.h>  
#include<math.h>  
int main() 

    int k; 
    __int64 a,b,c,left,right,max,min; 
    while(scanf("%d",&k)!=EOF) 
    { 
        scanf("%I64d %I64d",&a,&b); 
        if(k==64) 
        { 
     
 
            max=(__int64)pow(2.0,k-1.0)-1; 
            min=(__int64)pow(2.0,k-1.0); 
            if(a>=0&&b<=0||a<=0&&b>=0)  
                printf("I love nbut\n"); 
            else 
            { 
                if(a>0&&b>0) 
                { 
                    a=a-max+b; 
                    if(a>0) 
                        printf("Yes\n"); 
                    else 
                        printf("I love nbut\n"); 
                } 
            &

补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,