1489 - 特殊乘法
时间限制 : 1 秒
内存限制 : 32 MB
写个算法,对2个小于1000000000的输入,求结果。特殊乘法举例:123 * 45 = 1*4 +1*5 +2*4 +2*5 +3*4+3*5
题目输入
两个小于1000000000的数
题目输出
输入可能有多组数据,对于每一组数据,输出Input中的两个数按照题目要求的方法进行运算后得到的结果。
输入/输出样例
输入格式
24 65 42 66666 3 67
输出格式
66 180 39
C语言解答
#include<stdio.h> #include<string.h> int maxsize=20; int calc(char a[], char b[]){ int lena, lenb, i, j, result; lena=strlen(a); lenb=strlen(b); result=0; for(i=0; i<lena; i++){ for(j=0; j<lenb; j++){ result+=(a[i]-'0')*(b[j]-'0'); } } return result; } int main(){ char a[maxsize], b[maxsize]; int result; while(scanf("%s%s", a, b)==2){ result=calc(a, b); printf("%d\n", result); } return 0; }
C++解答
#include <stdio.h> int a,b; int run() { int i=0,j=0; while(a!=0) { i+=a%10; a/=10; } while(b!=0) { j+=b%10; b/=10; } printf("%d\n",i*j); } int main() { scanf("%d%d",&a,&b); while((a!=-9999999)||(b!=-4444444)) { run(); a=-9999999; b=-4444444; scanf("%d%d",&a,&b); } return 0; }
Java解答
import java.util.Scanner; public class Main { private static Scanner s = new Scanner(System.in) ; public static void main(String[] args) { while(s.hasNext()){ long a = s.nextLong() ; long b = s.nextLong() ; System.out.println(f(a, b)) ; } } public static long f(long a , long b){ long x = a ; long y = b ; int i = 0 ; int j = 0 ; while(a>0){ a = a/10 ; i++ ; } while(b>0){ b = b/10 ; j++ ; } int c [] = new int[i] ; int d [] = new int[j] ; i = 0 ; j = 0 ; while(x>0){ c[i] = (int) (x%10) ; i++ ; x = x /10 ; } while(y>0){ d[j] = (int) (y%10) ; j++ ; y = y /10 ; } long sum = 0 ; for (int k = 0; k < c.length; k++) { for (int k2 = 0; k2 < d.length; k2++) { sum = sum+c[k]*d[k2] ; } } return sum ; } }