程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> NOJ 1303 判斷a+b是否溢出

NOJ 1303 判斷a+b是否溢出

編輯:C++入門知識

[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"); 
                } 
                else if(a<0&&b<0) 
                { 
                    a=a-min+b; 
                    if(a<0) 
                        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)
  {
 

      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");
                }
                else if(a<0&&b<0)
                {
                    a=a-min+b;
                    if(a<0)
      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;
}


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved