There must be a "Number" in mind-Common Errors and flaws in C Language beginner code (8)

Source: Internet
Author: User

There is a BUG in the refactoring code I provided. This BUG is found inBirds _ AsukaThe netizen pointed out that "is it time-complex?" and said that "at first glance, I wanted to think of it as a mathematical problem, I found out again after thinking about the problem in mathematics.

#include <stdio.h> main(  i , sum =  ( i =  ; i <= ; i +++= 

#include <stdio.h> main( , ( + ) *  /  


). The "Number of rectangles" problem should be said that it is not so easy to obtain mathematical solutions.
Birds _ AsukaAfter the netizen said that he "first saw me and wanted to think of it as a mathematical problem", I also tried to think about it in a mathematical way. I found that n different points on the same line can constitute (n-1) n/2 different line segments, the rectangle of A * B has A + 1 and B + 1 different points on the adjacent sides, so they can form (A + 1) A/2 and (B + 1) respectively) b/two different line segments. In this way, the number of rectangles is (A + 1) A/2 × (B + 1) B/2. When the maximum value of A and B is 1000, the result is obviously not less than 25 × 1010, and this value is obviously greater than 231-1, even greater than 232-1 (the maximum integer that can be expressed by the unsigned type in most compilers ). In this way, the int type is used as the result type in the original refactoring code, which is obviously wrong.

)
Birds _ AsukaSome netizens pointed out that the problem of "time complexity is relatively large" also exists. However, for the convenience of algorithm description, the count () function was not written as follows:

 count(  A ,  x1 , y1 ;    x2 , y2 ;    num =  ( x1 =  ; x1 < B ; x1 ++ )       ( y1 =  ; y1 < A ; y1 ++ )          ( x2 = x1 +  ; x2 <= B ; x2 ++ )             ( y2 = y1 +  ; y2 <= A ; y2 ++ )                  num ++ ;                   

    ( x1 =  ; x1 < B ; x1 ++ ( x2 = x1 +  ; x2 <= B ; x2 ++ ( y1 =  ; y1 < A ; y1 ++ ( y2 = y1 +  ; y2 <= A ; y2 ++++ ;




typedef  BIG_NUM[] ;

 

 count( BIG_NUM m ,  A , ] = ] =    mul_sum( m , B );}

 mul_sum( BIG_NUM m ,  t1 = n %  ==  ? n / = n %  !=  ? (n + ) /  : n + 

 mul( BIG_NUM m , ] *=] *=] += m[]/;   m[] %= 


 ( m[] ==  , m[ , m[ , m[

<stdio.h>typedef  BIG_NUM[ count( BIG_NUM ,  ,  mul_sum( BIG_NUM ,  mul( BIG_NUM ,  POW 1000000 WID 6 main(  ( printf(  , &A , &B )!=  ( m[] ==  , m[ , m[ , WID , m[ mul( BIG_NUM m , ] *=] *=] += m[]/POW;   m[] %= count( BIG_NUM m ,  A , ] = ] =    mul_sum( m , B ); mul_sum( BIG_NUM m ,  t1 = n %  ==  ? n / = n %  !=  ? (n + ) /  : n + 

Related Article

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.