加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

UVA 10106 (大数相乘 13.08.02)

发布时间:2020-12-14 04:03:19 所属栏目:大数据 来源:网络整理
导读:?Product ? The Problem The problem is to multiply two integers X,Y. ( 0=X,Y10 250 ) 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

?Product?

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

题意: 大数相乘
做法,看AC代码,有注释,自行理解,不难~

AC代码:
#include<stdio.h>
#include<string.h>

int main() {
	char mul1[300],mul2[300];
	char ch;
	int ans[600];
	int len1,len2;
	int num1,num2;
	int k,Sum;
	while(gets(mul1) != NULL && gets(mul2) != NULL) {
		memset(ans,sizeof(ans));
		len1 = strlen(mul1);
		len2 = strlen(mul2);
		//倒序第一个乘数:
		for(int i = 0; i < len1/2; i++) {
			ch = mul1[i];
			mul1[i] = mul1[len1 - 1 - i];
			mul1[len1 - 1 - i] = ch;
		}
		//倒序第二个乘数:
		for(int i = 0; i < len2/2; i++) {
			ch = mul2[i];
			mul2[i] = mul2[len2 - 1 - i];
			mul2[len2 - 1 - i] = ch;
		}
		//开始处理
		for(int i = 0; i < len1; i++) {
			num1 = mul1[i] - '0';
			k = i;
			for(int j = 0; j < len2; j++) {
				num2 = mul2[j] - '0';
				Sum = num1 * num2;
				ans[k] = Sum % 10 + ans[k];
				ans[k+1] = Sum / 10 + ans[k+1];
				if(ans[k] > 9) {
					ans[k+1] = ans[k] / 10 + ans[k+1];
					ans[k] = ans[k] % 10;
				}
				k++;
			}
		}
		//从后面开始查找第一个非零数,然后倒序输出~
		int pos;
		for(int i = 599; i >= 0; i--) {
			if(ans[i] != 0) {
				pos = i;
				for(int i = pos; i >= 0; i--)
					printf("%d",ans[i]);
				printf("n");
				break;
			}
			else if(i == 0 && ans[i] == 0)
				printf("0n");
		}
	}
	return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读