北大ACM 2389 Bull Math

Bull Math
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11709   Accepted: 6089

Description

Bulls are so much better at math than the cows. They can multiply huge integers together and get perfectly precise answers ... or so they say. Farmer John wonders if their answers are correct. Help him check the bulls' answers. Read in two positive integers (no more than 40 digits each) and compute their product. Output it as a normal number (with no extra leading zeros). 

FJ asks that you do this yourself; don't use a special library function for the multiplication.

Input

* Lines 1..2: Each line contains a single decimal number.

Output

* Line 1: The exact product of the two input lines

Sample Input

11111111111111
1111111111

Sample Output

12345679011110987654321
 
 
一道简单了水题,贡献了5次WA,没注意结果为0 的情况和数组开小了
 
 
#include <stdio.h>
#include <string.h>
int result[100]={0};
int a[45];
int b[45];
int length_a = 0, length_b = 0;
void Cal(int a[], int b[])
{
	void Show();
	int jinwei = 0, digit = 0,x;
	int hinder = 0;
	int temp[50]={0};
	int jinwei_2 = 0;
	int k,i,j,nn;
	for(i = length_b-1; i >= 0; i--, hinder++)
	{
		for(j = length_a-1, nn = 0; j >= 0 || jinwei > 0; j--, nn++)
		{
			x = a[j] * b[i] + jinwei;
			jinwei = x / 10;
			temp[nn] = x % 10;
		}
		for(j = 0,k = hinder; j < nn || jinwei_2 > 0; k++,j++)
		{
			x = result[k] + temp[j] + jinwei_2;
			jinwei_2 = x / 10;
			result[k] = x % 10;
		}
		jinwei = jinwei_2 = 0;
		memset(temp,0,sizeof(temp));
	}
}
void Convert(char *ptr_a, char *ptr_b)
{
	char *temp = ptr_a;
	int *pt = a;
	int i;
	unsigned int j;
	for(i = 0; i < 2; i++)
	{
		for(j = 0; j < strlen(temp); j++)
			pt[j] = temp[j] - '0';
		temp = ptr_b;
		pt = b;
	}
}
void Show()
{
	int i = 99,j;
	while(result[i]==0){
		if(i==0) break;
		i--;		
	}
	if(i==0) 
	{
		printf("0\n");
		return;
	}
	for(j = i; j >= 0; j-- )
		printf("%d",result[j]);
		
	printf("\n");
}
int main()
{

	char ptr_a[45]; 
	char ptr_b[45];
	scanf("%s", ptr_a);
	scanf("%s", ptr_b);
	length_a = strlen(ptr_a);
	length_b = strlen(ptr_b);
	Convert(ptr_a, ptr_b);

	Cal(a,b);	
	Show();
	return 1;
}


你可能感兴趣的:(算法,ACM)