NOJ 1303 determines whether a + B overflows

Source: Internet
Author: User

[1303] A + B
Time Limit: 1000 MS memory limit: 65535 K
Problem description
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 (random) and A is a 64-signed integer, which means A is between-2 ^ 63 and (2 ^ 63)-1 (random ). now we give the K-signed range, and two K-signed integers A and B, you shoshould check whether the sum of A and B is beyond the range of K-signed integer or not.

Input
There will be used 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.
Output
For each case, you shocould estimate whether the sum is beyond the range. If exceeded, print "Yes", otherwise "I love nbut ".
Sample Input
64
1000 sample output
I love nbut prompt
No source
Jiaduobao herbal tea operation

Bytes
Http://acm.nbut.cn/Problem/view.xhtml? Id = 1303
Enter k a B to indicate k-bit data. For example, if int Is 32-bit long, the maximum value of 64-bit k is 64. Input a B to ask whether the sum of a + B overflows.
 
Ideas:
We use long to store a B. For k <64 a + B, it is determined based on the range of k-bit data.
If k is 64, it may overflow. We can use a + B = c to determine whether c-a is equal to B c-B or not.
Or you can use the following method to check whether the two methods of Code handle well.
 
[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-! = B | c-B! =)
{
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-! = B | c-B! =)
{
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;
}


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.