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 |
高精度模板题留个模板给大家,扩展精度把数组的50改大就好~ #include <iostream> #include <stdio.h> #include <string.h> using namespace std; struct gjd{ int digits[50]; gjd(){ for(int i = 0; i < 50; i++) digits[i] = 0; } gjd(char *str){ int len = strlen(str); for(int i = 0; i < len; i++){ digits[i] = str[len-1-i]-'0'; } for(int i = len; i < 50; i++){ digits[i] = 0; } } void normalize(){ int carry = 0; for(int i = 0; i < 50; i++){ digits[i] += carry; if(digits[i] >= 0){ carry = digits[i]/10; digits[i]%=10; } else{ if(digits[i]%10 == 0){ carry = digits[i]/10; digits[i] = 0; } else{ carry = digits[i]/10-1; digits[i] -= carry*10; } } } } }p26[24], bfh[25]; gjd add(gjd g1, gjd g2){ gjd temp; for(int i = 0; i < 50; i++){ temp.digits[i] = g1.digits[i] + g2.digits[i]; } temp.normalize(); return temp; } gjd sbt(gjd g1, gjd g2){ gjd temp; for(int i = 0; i < 50; i++){ temp.digits[i] = g1.digits[i] - g2.digits[i]; } temp.normalize(); return temp; } gjd mlt(gjd g, int n){ gjd temp; for(int i = 0; i < 50; i++){ temp.digits[i] = g.digits[i] * n; } temp.normalize(); return temp; } bool operator<(const gjd &g1, const gjd &g2){ for(int i = 49; i >= 0; i--){ if(g1.digits[i] < g2.digits[i]) return 1; if(g1.digits[i] > g2.digits[i]) return 0; } return 0; } void init(){ p26[0].digits[0] = 1; for(int i = 1; i < 50; i++) p26[0].digits[i] = 0; for(int i = 1; i < 24; i++){ p26[i] = mlt(p26[i-1], 26); } for(int i = 0; i < 50; i++) bfh[0].digits[i] = 0; for(int i = 1; i < 24; i++){ bfh[i] = add(bfh[i-1], p26[i]); } } void print(gjd &g){ int tag; for(int i = 49; i >= 0; i--){ if(g.digits[i] != 0){ tag = i; break; } } for(int i = tag; i >= 0; i--){ printf("%d", g.digits[i]); if(i > 0 && i%3==0) printf(","); } } int main() { init(); char str[100]; while(1){ scanf("%s", str); if(str[0] == '*') break; int len = strlen(str); if(str[0] >= 'a'){ //字呣转数字 gjd res = bfh[0]; for(int i = len-1; i>=0; i--){ int I = len-1-i; int dg = str[i]-'a'+1; res = add(res, mlt(p26[I], dg)); } printf("%s", str); for(int i = 0; i < 22-len; i++) printf(" "); print(res); printf("\n"); } else{ //数字转字呣 gjd tar(str); gjd Tar = tar; int ws; for(int i = 1; i < 24; i++){ if(!(bfh[i] < tar)){ ws = i; break; } } tar = sbt(tar, bfh[ws-1]); char res[30] = {'\0'}; for(int w = ws; w > 0; w--){ int sw; for(int i = 1; i <= 26; i++){ gjd gtemp = mlt(p26[w-1], i); if(!(gtemp < tar)){ sw = i; break; } } res[ws-w] = sw + 'a' - 1; tar = sbt(tar, mlt(p26[w-1], sw-1)); } int len = strlen(res); printf("%s", res); for(int i = 0; i < 22-len; i++) printf(" "); print(Tar); printf("\n"); } } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator