Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
帮忙看看我怎么错的吧,各位。我高精度写得没问题啊,样例也过了#include<iostream> #include<cstring> using namespace std ; #define unit 1000 typedef struct Number { int d[1000] ; int l ; }Number ; void add ( Number &a , int n ) { a.d[0] += n; int i ,c = a.d[0] / unit ; a.d[0] %= unit; for ( i = 1 ;c || i < a.l ;i ++ ) { a.d[i] += c ; c = a.d[i] / unit ; a.d[i] %= unit ; } a.l = i ; while ( a.l > 0 && a.d[a.l-1] == 0 ) a.l -- ; } int div ( Number &a, int n ) { int i ,c = 0; for ( i = a.l - 1;i >= 0; i -- ) { a.d[i] += c * unit ; c = a.d[i] % n ; a.d[i] /= n ; } while ( a.l > 1 && a.d[a.l-1] == 0 ) a.l -- ; return c; } void mul ( Number &a, int n ) { int i ,c = 0; for ( i = 0 ;i < a.l || c > 0 ;i ++ ) { a.d[i] *= n ; a.d[i] += c ; c = a.d[i] / unit ; a.d[i] %= unit ; } a.l = i ; } int main () { Number a; int b1, b2, len, i, t; char in[unit], out[unit]; freopen ( "d:\\in.txt","r",stdin ); cin >> t; while ( t -- ) { cin >> b1 >> b2 >> in; cout << b1 << ' ' << in << endl << b2 << ' '; memset ( a.d, 0, sizeof ( a.d ) ); a.l = 1; for ( len = 0; in[len] ; len ++ ) if ( in[len] <= '9' ) in[len] -= '0'; else if ( in[len] >= 'a') in[len] -= 'a' - 36; else in[len] -= 'A' - 10; for ( i = 0 ; i < len; i ++ ) { mul ( a, b1 ); add ( a, (int)in[i] ); } len = 0; while ( a.d[0] || a.l - 1 ) out[len++] = div ( a, b2 ) ; len --; while ( len >= 0 ) { if ( out[len] < 10 ) cout << (int)out[len]; else if ( out[len] < 36 ) cout << (char) ( out[len] + 'A' - 10 ) ; else cout << (char) ( out[len] + 'a' - 36 ) ; len --; } cout << endl << endl; } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator