UV 465-overflow (floating point advantage)

Source: Internet
Author: User

This question demonstrates the advantages of floating point numbers. That is, the 64-bit integer is used (note that long requires i64d output). Overflow also turns into a negative number, making it difficult to judge. If the double type is used, even if the number of digits of a string exceeds bits, the sscanf () function is also used for processing, but it will not become a negative number. The reason is that floating point numbers can be expressed exponentially. For example, the positive value of double ranges from 4.94065645841246544e-324 to 1.79769313486231570e + 308. This means that the upper 300-bit string can be converted to a double-precision floating point number, and the precision will be lost at most. After knowing this nature, it is very easy to solve this problem with double-precision floating point numbers.

#include <stdio.h>const int MINT = 0x7fffffff;int main() {    char str1[1000], str2[1000], ch;    while (scanf("%s %c %s", str1, &ch, str2) != EOF) {        printf("%s %c %s\n", str1, ch, str2);        double a, b;        sscanf(str1, "%lf", &a);        sscanf(str2, "%lf", &b);        if (a > MINT)            printf("first number too big\n");        if (b > MINT)            printf("second number too big\n");        if ('+'==ch && a+b>MINT)            printf("result too big\n");        if ('*'==ch && a*b>MINT)            printf("result too big\n");    }    return 0;}

Previously, I used long integer processing, even though various processing would still be wrong. Finally, let's start with the article http://blog.sina.com.cn/s/blog_8ee62ecb0100wpjm.html. Thanks to the concise and elegant code of the original author.

 

 

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.