# Peking University poj # 1001Exponentiation power,

Source: Internet
Author: User

# Peking University poj # 1001Exponentiation power,

Attached to the original question address: http://poj.org/problem? Id = 1001

First, I analyzed the questions and listed several important points I think:

Next, we will break through them one by one;

  • Enter two integers to calculate the product

Because it is a large number, it has exceeded the range that can be calculated by int or long double, so I thought of using the array method for processing.

Save the multiplier to an array, and then use the column-vertical method to calculate and import a single digit. The simulation is as follows:

A [0] a [1] a [2] a [3] a [4]

X B [0] B [1] B [2] B [3] B [4]

----------------------------------------------------------------------------

[0] [4] [1] [4] [2] [4] [3] [4] [4] [4]

[0] [3] [1] [3] [2] [3] [3] [3] [4] [3]

[0] [2] [1] [2] [2] [2] [3] [2] [4] [2]

[0] [1] [1] [1] [2] [1] [3] [1] [4] [1]

[0] [0] [1] [0] [2] [0] [3] [0] [4] [0]

----------------------------------------------------------------------------

C [0] c [1] c [2] c [3] c [4] c [5] c [6] c [7] c [8]

Then, before writing this code, I listed an instance to clarify the purpose of each step:

9 9

X 9 9 // two multiplier arrays are constructed. The array length is the number of digits.

---------------------

81

81

---------------------

81 162 81 // multiplication and Addition before carry

---------------------

81 170 1

---------------------

98 0 1 // carry complete

The following code implements "input two integers, calculate the product:

1 # include <stdio. h> 2 int main () {3 int la, lb, l, I, j; 4 char aa [50], bb [50]; 5 scanf ("% s", aa, bb); 6 la = strlen (aa); 7 lb = strlen (bb); 8 l = la + lb-1; 9 int a [la], B [lb], AB [l], c [l]; 10 for (I = 0; I <la; I ++) {11 a [I] = (int) aa [I]-48; 12} 13 for (I = 0; I <lb; I ++) {14 B [I] = (int) bb [I]-48; 15} // get two multiplier arrays 16 for (I = L-1; I> = 0; I --) {17 AB [I] = 0; 18 for (j = la-1; j> I-lb; j --) {19 if (I-j> = 0 & j> = 0) {20 AB [I] = AB [I] + a [j] * B [I-j]; 21} 22} 23} 24 for (I = 0; I <l; I ++) {25 c [I] = AB [I]; 26} // get 27 printf ("\ n") before carrying AB "); 28 for (I = L-1; I> = 1; I --) {29 c [I] = AB [I] % 10; 30 AB [I-1] = AB [I-1] + (AB [I]-c [I])/10; 31} 32 c [0] = AB [0]; 33 for (I = 0; I <l; I ++) {34 printf ("% d", c [I]); 35} 36 return 0; 37}
  • After multiple multiplier values are input, the branch outputs the result.

Here we use a statement: while (scanf ("% s % d", s, & n) = 2) or while (scanf (% s % d ", s, & n )! = EOF)

The function is to jump out of the loop after the input data is complete.

The following code implements "output result of branch after multiple multiplier inputs:

 1 #include <stdio.h> 2 int main() { 3     int x[100],y[100],z[100],i=0,j; 4     while(scanf("%d %d",&x[i],&y[i])==2){ 5         z[i]=x[i]*y[i]; 6         i++; 7     } 8     for(j=0;j<i;j++){ 9         printf("%d\n",z[j]);10     }11     return 0;12 }

Orz not finished yet

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.