Multply Problem

Multiply Problem

description:

6*9 = 42 is not true for the base of 10, but is true for the base of 13. The problem asks students to write a program which inputs three integers p, q, and r and determines the base B (2 <=B <=16) for which p*q = r. If there are many candidates for B, output the smallest one.

source:

right:ginjava

--------------------------------------------------------------------------------------------------

import java.io.*;import java.util.*;public class Main{ private static int MAX=100000; private static int change(int num,int r) { int temp=1,i,ans=0; while (num!=0) { if (num%10>=r) return -1; ans+=temp*(num%10); temp=temp*r; num=num/10; } return ans; } public static void main(String args[]) throws Exception { BufferedReader stdin= new BufferedReader(new InputStreamReader(System.in)); String line=stdin.readLine(); StringTokenizer st=new StringTokenizer(line); int t=Integer.parseInt(st.nextToken()); while (t-->0) { boolean flag=false; line=stdin.readLine(); st=new StringTokenizer(line); int a=Integer.parseInt(st.nextToken()); int b=Integer.parseInt(st.nextToken()); int c=Integer.parseInt(st.nextToken()); for (int i=1;i<=MAX;i++) { if (change(a,i)*change(b,i)==change(c,i)) { flag=true; System.out.println(i); break; } } if (flag==false) System.out.println(0); } } }

--------------------------------------------------------------------------------------------------

wrong 1: GDBNU

--------------------------------------------------------------------------------------------------

#include <iostream.h>long change_to_10(long x,const int b){ long xx,number=0; for(int j=1;x!=0;j*=b) { xx=x%10; number+=xx*j; x/=10; } return number;}int main() { int n,b,mark; long p,q,r; cin>>n; for(int i=0;i<n;i++) { mark=0; cin>>p>>q>>r; for(b=2;b<=16;b++) { if( change_to_10(p,b) * change_to_10(q,b) == change_to_10(r,b) ) { mark=1; break; } } if(mark) cout<<b<<endl; else cout<<mark<<endl; } return 0;}

--------------------------------------------------------------------------------------------------

wrong2: skywalker

--------------------------------------------------------------------------------------------------

#include<iostream>#include<cmath>using namespace std;int pw(int x,int y){ int m=1; while(y){ m=m*x; y--; } return m;}int main(){ long n,p,q,r,i,j,k=0,s1=0,s2=0,s3=0; int cp[100],cq[100],cr[100]; cin>>n; n++; while(n--){ cin>>p>>q>>r; k=0; s1=0; s2=0; s3=0; for(i=0;i<100;i++){ cp[i]=p%10;p=p/10;if(cp[i]>k) k=cp[i]; cq[i]=q%10;q=q/10;if(cq[i]>k) k=cq[i]; cr[i]=r%10;r=r/10;if(cr[i]>k) k=cr[i]; } for(i=k+1;i<17;i++){ for(j=0;j<100;j++){ s1=s1+cp[j]*pw(i,j); s2=s2+cq[j]*pw(i,j); s3=s3+cr[j]*pw(i,j); } if(s1*s2==s3){ cout<<i<<endl; break; } s1=0; s2=0; s3=0; } if(i>=17) cout<<"0"<<endl; } return 0;}

--------------------------------------------------------------------------------------------------

wrong3:00648280

--------------------------------------------------------------------------------------------------

#include<iostream>using namespace std;double to_dec( int digit, int base );int main(){ int num, base; int p, q, r; int i; while( cin >> num ) { for( i = 0; i != num; ++ i ) { cin >> p >> q >> r; for( base = 3; base != 17; ++ base ) { if( to_dec( p, base ) * to_dec( q, base ) == to_dec( r, base ) ) { break; } } if( base == 17 ) { cout << '0' << endl; } else { cout << base << endl; } } } return 0;}double to_dec( int digit, int base ){ double dec_form = digit % 10; int x = 1; digit = digit / 10; while( digit != 0 ) { x *= base; dec_form += ( digit % 10 ) * x; digit /= 10; } return dec_form;}

--------------------------------------------------------------------------------------------------

wrong4: ym123456

--------------------------------------------------------------------------------------------------

#include <stdio.h>#include <string.h>int b2ten(int num, int radix){ int result = 0, i; char temp[100]; int len = strlen(temp); sprintf(temp, "%d", num); for (i = 0; i < len; i ++) { if (temp[i] - '0' >= radix) return -1; result = result * radix + (temp[i] - '0'); } return result;} int main(){ int n, i, mul1, mul2, result; long a, b, c; scanf("%d", &n); while (n --) { scanf("%d%d%d", &mul1, &mul2, &result); for (i = 2; i <= 16; i ++) { a = b2ten(mul1, i); b = b2ten(mul2, i); c = b2ten(result, i); if (a == 1 || b == -1 || c == -1) continue; if (a * b == c) { printf("%d\n", i); break; } } if (i == 17) printf("0\n"); } return 0;}

--------------------------------------------------------------------------------------------------

wrong5: 00748311

--------------------------------------------------------------------------------------------------

#include<iostream>using namespace std;int bToTen(char a[],int b){ int result=0; for(int i=0;i<(int)strlen(a);i++){ if((a[i]-'0')>=b) return -1; result*=b; result+=a[i]-'0'; } return result;}void main(){ int n,b; char p[8],q[8],r[8]; int p1,q1,r1; cin>>n; while(n--){ cin>>p>>q>>r; for(b=2;b<16;b++){ p1=bToTen(p,b); q1=bToTen(q,b); r1=bToTen(r,b); if(p1==-1||q1==-1||r1==-1) continue; if(p1*q1==r1){ cout<<b<<endl; break; } } if(b==17) cout<<'0'<<endl; }}

--------------------------------------------------------------------------------------------------