现在的位置: 首页 > 综合 > 正文

121

2013年03月15日 ⁄ 综合 ⁄ 共 1156字 ⁄ 字号 评论关闭

另类乘法

时间限制:3000 ms  |  内存限制:65535 KB
难度:1
描述

Bessie is tired of multiplying pairs of numbers the usual way, so she invented her own style of multiplication. In her style, A*B is equal to the
sum of all possible pairwise products between the digits of A and B. For example, the product 123*45 is equal to 1*4 + 1*5 + 2*4 + 2*5 + 3*4 + 3*5 = 54. Given two integers A and B (1 ≤ A, B ≤ 1,000,000,000), determine A*B in Bessie's style
of multiplication.

输入
The first Line of the input is a positive integer T,indicates the number of the test cases;
In every case,the input is in one line,contains two positive interger A,B
输出
For every case,output the multiplication in Bessie's style.
样例输入
1
123 45
样例输出
54
#include<stdio.h>
#include<string.h>
int main(){
	int n;
	int i,j,length1,length2,account;
	char char2[100],char1[100];
	scanf("%d",&n);
	while(n--){
		account=0;
		scanf("%s %s",char1,char2);
		length1=strlen(char1);
		length2=strlen(char2);
		for(i=0;i<length1;i++){
			char1[i]=char1[i]-48;
			for(j=0;j<length2;j++){
			account+=char1[i]*(char2[j]-48);
			}
			
		}
		printf("%d\n",account);
	}
}

优化代码

 

#include<stdio.h>
int main(){
	int n,sum;
	char a[20],b[20];
	scanf("%d",&n);
	while(n--){
		sum=0;
		scanf("%s%s",a,b);
		for(char *p=a;*p;p++)
			for(char *q=b;*q;q++){
			sum+=(*p-'0')*(*q-'0');
			}
			printf("%d\n",sum);
	}
}

*q,*p.为什么可以作为判断条件,因为 字符串最后是'\0',也就是零 ,循环就结束了。

指针的应用、

抱歉!评论已关闭.