程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> uva 10106

uva 10106

編輯:C++入門知識

The Problem
The problem is to multiply two integers X, Y. (0<=X,Y<10250)

The Input
The input will consist of a set of pairs of lines. Each line in pair contains one multiplyer.

The Output
For each input pair of lines the output line should consist one integer the product.

Sample Input
12
12
2
222222222222222222222222
Sample Output
144
444444444444444444444444

#include <stdio.h>
#include <string.h>
#define MAXN 260
char str1[MAXN];
char str2[MAXN];
int s1[MAXN];
int s2[MAXN];
int s[MAXN * 2 + 10];

int main()
{
    int i,j,len1,len2;
    while(gets(str1) && gets(str2))
    {
        memset(s1,0,sizeof(s1));
        memset(s2,0,sizeof(s2));
        memset(s,0,sizeof(s));
        len1 = strlen(str1);
        len2 = strlen(str2);
        j = 0;
        for(i = len1 - 1; i >=0; i--)
        {
          s1[j++] = str1[i] - '0';
        }
        j = 0;
        for(i = len2 - 1; i >= 0; i--)
        {
            s2[j++] = str2[i] - '0';
        }

        for(i = 0; i < len2; i++)
        {
            for(j = 0; j < len1; j++)
            {
                s[i+j] += s2[i] * s1[j];
            }
        }
        for(i = 0; i < MAXN * 2 +2; i++)
        {
            if(s[i] >= 10)
            {
                s[i + 1] += s[i]/10;
                s[i] %= 10;
            }
        }
        int flag = 0;
        for(i = MAXN * 2 + 5; i >= 0; i--)
        {
            if(flag)
            {
                printf("%d", s[i]);
            }
            else if(s[i])
            {
                printf("%d", s[i]);
                flag = 1;
            }
        }
        if(!flag)
        {
            printf("0");
        }
        printf("\n");
    }
    return 0;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved