九度[1080]进制转换
1 # include<iostream> 2 # include<string> 3 # include<cstring> 4 using namespace std; 5 struct bigint{ //定义一个大整数类 6 int a[500]; 7 int remain; 8 int i; 9 bigint & operator*(int & value){ //重载乘法 10 for(i=0;i<500;i++) 11 a[i]=a[i]*value; 12 int c=0; 13 for(i=0;i<500;i++){ 14 a[i]=a[i]+c; 15 c=a[i]/100; 16 a[i]=a[i]%100; 17 } 18 return *this; 19 } 20 bool operator !=(const int & value) //判断整数是否为0 21 { 22 for(i=0;i<500;i++) 23 if(a[i]!=value) break; 24 if(i<500) return true; 25 return false; 26 } 27 bigint & operator/(int &value){//重载除法 28 int c=0; 29 for(i=499;i>=0;i--) 30 if(a[i]!=0) break; 31 //cout<<"i"<<i<<endl; 32 for(;i>=0;i--) 33 { 34 a[i]=(a[i]+c*100); 35 c=a[i]%value; 36 a[i]=a[i]/value; 37 } 38 remain=c; 39 return *this; 40 } 41 }; 42 int main(){ 43 int m=0,n=0; 44 int i=0; 45 while(cin>>m>>n){ 46 string s; 47 cin>>s; 48 if(s=="0") {cout<<0<<endl;continue;} 49 int l=s.size(); 50 bigint bida; 51 memset(bida.a,0,sizeof(bida.a)); 52 for(i=0;i<l;i++) 53 { 54 int k=m; 55 if(i!=0) bida=bida*k; 56 if(s[i]<='9') { 57 int k=s[i]-'0'; 58 bida.a[0]=bida.a[0]+k; 59 } 60 else { 61 int k=s[i]-'A'+10; 62 bida.a[0]=bida.a[0]+k; 63 } 64 } //转换为十进制数bida; 65 //cout<<bida.a[0]; 66 int b[2000]; 67 i=0; 68 memset(b,0,sizeof(b)); 69 while(bida!=0){ 70 bida=bida/n; 71 b[i]=bida.remain; 72 i++; 73 } 74 for(i=i-1;i>=0;i--) 75 { 76 if(b[i]>9) {char c=b[i]-10+'a';cout<<c;} 77 else cout<<b[i]; 78 } 79 cout<<endl; 80 } 81 return 0; 82 }
写得有点儿乱,先传上。回头再整理
转载请注明出处及链接 谢谢