NYOJ 303
View Code
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 #include<stack> 6 using namespace std; 7 8 char ch[1000]; 9 stack <int> q; 10 11 void slovenum() 12 { 13 int i,len; 14 while(!q.empty())q.pop(); 15 int x=0; 16 len=strlen(ch); 17 for(i=0;i<len;++i) 18 x=x*10+ch[i]-'0'; 19 while(x) 20 { 21 q.push(x%26); 22 if(x==26)break; 23 if(x%26==0)x-=26;//注意这里:当 x = 52时即余数为 0 由于只有26个字母,不能当27进制处理 24 else x-=x%26; 25 x/=26; 26 } 27 while(!q.empty()) 28 { 29 int temp=q.top(); 30 q.pop(); 31 if(!temp)printf("Z"); 32 else 33 printf("%c",temp-1+'A'); 34 } 35 printf("\n"); 36 } 37 38 void slovechar() 39 { 40 int i,len; 41 len=strlen(ch); 42 int sum=0; 43 for(i=0;i<len;++i) 44 sum=sum*26+ch[i]-'A'+1; 45 printf("%d\n",sum); 46 } 47 48 int main() 49 { 50 int iscase,i; 51 scanf("%d",&iscase); 52 while(iscase--) 53 { 54 scanf("%s",ch); 55 if(ch[0]>='0'&&ch[0]<='9') 56 slovenum(); 57 else slovechar(); 58 } 59 return 0; 60 }