Algorithm to improve P1001

Source: Internet
Author: User

When two larger integers are multiplied, a data overflow scenario may Occur. To avoid overflow, you can use a string method to achieve multiplication between two large numbers. specifically, first enter two integers as strings, each integer will not exceed 8 bits in length, and then the result of multiplying them is stored in another string (no more than 16 bits in length), and finally the string is printed Out. For example, Suppose the user input is: 62773417 and 12345678, the output is: 774980393241726.

Input:
62773417 12345678

Output:
774980393241726

The main thing to note is the question of rounding ....

#include <stdio.h>#include<string.h>voidMultiply (Char*s,Char*s1,Char*S2) {    intlen,len1,i,j,k,count,tmp;//Count: RoundingLen = strlen (s), len1 =strlen (s1);  for(i =0; I < -; i + +) s2[i]='0';//you can't use memset .s2[ -] =' /';  for(j = len1-1; J >=0; j--) {count=0; K= len1-j-1;//note where each round starts to be placed         for(i = len-1; I >=0; i--) {tmp= (s1[j]-'0') * (s[i]-'0') +count; if(tmp + s2[k]-'0'>9){                //count and s2[k] are mutually affected, solution I sets a variable, II calculates the count into TMP before doing the If Judgment//still note that count is calculated before s2[k]//count = (tmp + count + s2[k]-' 0 ')/10;//s2[k] = (tmp + count + s2[k]-' 0 ')%10 + ' 0 ';Count = (tmp + s2[k]-'0')/Ten; s2[k]= (tmp + s2[k]-'0')%Ten+'0'; }            Else{s2[k]= tmp +s2[k]; Count=0; } k++; }        if(count >0) s2[k]= Count +'0'; }    //the following method can only avoid the last 0, the final output is to start at non-0 o'clock output//the simpler way is to define the global variables in k, so it is not necessary to strlen for length at output time.    if(! (s2[k] >'0'|| s2[k] = =' /')) s2[k]=' /'; Else if(s2[k] >'0') {s2[k+1] =' /'; }    }intmain () {Chars[9],s1[9],s2[ -]; inti,j; scanf ("%s%s", s,s1);    Multiply (s,s1,s2); //Reverse Output     for(i = strlen (s2)-1; I >=0; i-- )        if(s2[i]! ='0')              break; if(i = =-1) Putchar ('0');//0 to consider separately     for(j = i; J >=0; j--) Putchar (s2[j]); Puts (""); return 0;}

Algorithm to improve P1001

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.