sicily 6484. *the easiest problem

Description
 Have you passed the problem 1000(A - B)? Yeah,it's very easy!
 Now,given two integers N and M(0 < N,M <= 1050 ,N > M),calculate N + M and N * M. I think it's also very easy for you!

Input
 There are several test cases, one line for each case. For each line, there are only two numbers N and M,seperated by space.The input is end by EOF.

Output
Output N + M and N * M in a single line without leading zeros respectively.There is a blank line between two test cases.

 

用字符串存储数字,运算的时候利用ASCII码改成数字,然后按照小学教的加法和乘法算法来做,先在纸上模拟一遍,然后转化成一堆选择和循环就变成代码了。注意进位的处理。

查了喂鸡才知道乘法原来有那么多奇葩的算法………

 输出格式要求比较奇葩,是下面这样,红色画起来的是输入

View Code
  1 #include <stdio.h>
  2 #include <string.h>
  3 
  4 void add( const char n[], const char m[] );
  5 void multiply( const char n[], const char m[] );
  6 void printArray( const int array[], int length );
  7 int max( int a, int b);
  8 
  9 int main()
 10 {
 11     char n[55] = {0};
 12     char m[55] = {0};
 13     
 14     if( scanf("%s%s", &n, &m) != EOF )
 15     {
 16         add( n, m );
 17         multiply( n, m );
 18         
 19         while( scanf("%s%s", &n, &m) != EOF )
 20         {
 21             printf("\n");
 22             
 23             add( n, m );
 24             multiply( n, m );
 25         }
 26     }
 27     
 28     return 0;
 29 }
 30 
 31 void add( const char n[], const char m[] )
 32 {
 33     int temp;
 34     int carry;
 35     int i;
 36      int ni, mi;
 37     int nlen, mlen, sumlen;    
 38     int sum[55] = {0};
 39 
 40     nlen = strlen(n);
 41     mlen = strlen(m);
 42     
 43     for( i = 0; i < max( nlen, mlen ); i++ )
 44     {
 45 
 46         (nlen - i - 1 >= 0) ? (ni = n[nlen - i - 1] - '0') : (ni = 0);
 47         (mlen - i - 1 >= 0) ? (mi = m[mlen - i - 1] - '0') : (mi = 0);
 48         
 49         temp = sum[i] + ni + mi;
 50         sum[i] = temp % 10;
 51         carry = temp / 10; 
 52         sumlen = i;
 53 
 54         if( carry > 0 )
 55         {
 56             sum[i+1] = sum[i+1] + carry;
 57             sumlen = i+1;
 58         } 
 59     }
 60     
 61     printArray( sum, sumlen );
 62     
 63     return;
 64 }
 65 
 66 void multiply( const char n[], const char m[] )
 67 {
 68     int temp;
 69     int carry;
 70     int i1, i2;    
 71      int i; 
 72     int j, k;
 73     int nlen, mlen, prdtlen;
 74     
 75     int prdt[110] = {0};
 76     
 77     nlen = strlen(n);
 78     mlen = strlen(m);
 79     carry = 0;
 80     
 81     for( i1 = 0, k = nlen - 1; i1 < nlen; i1++, k-- )
 82     {
 83         for( i2 = 0, j = mlen - 1; i2 < mlen; i2++, j-- )
 84         {
 85             i = i1 + i2;
 86             temp = prdt[i] + ( n[k] - '0' ) * ( m[j] - '0' ) + carry;
 87             prdt[i] = temp % 10;
 88             carry = temp / 10; 
 89         }
 90         
 91         while( carry > 0 )
 92         {
 93             i++;
 94             prdt[i] = prdt[i] + carry % 10;
 95             carry = carry / 10;
 96         }
 97         
 98         prdtlen = i;
 99     }
100     
101     printArray( prdt, prdtlen );
102     
103     return;
104 }
105 
106 void printArray( const int array[], int length )
107 {
108     int i;
109     
110     for( i = length; i >= 0; i-- )
111     {
112         printf( "%d", array[i] );
113     }
114     
115     printf("\n");
116 }
117 
118 
119 int max( int a, int b)
120 {
121     if ( a > b )
122     {
123         return a;
124     }
125     else
126     {
127         return b;
128     }
129 }

 

posted @ 2012-12-01 22:19  Joyee  阅读(250)  评论(0编辑  收藏  举报