All Your Base 模擬題

2011 ACM ICPC SOUTHCENTRALUSA REGIONAL
PROGRAMMINGCONTEST
                          2 - All Your Base
Premise: Given a specification for a “base” (well, actually a mixed radix number system), take in pairs of numbers written in our “base”, perform

a specified operation on them and output the result in our base.
The Base:A number system where the right-most digit (digit 1) can be a counting number between 0 and 1,the second right-most digit (digit 2) can

be a counting number between 0 and 2 and, more generally, each digit n(as labeled from the right) can have values between 0 and n. After 9,

upper case letters are used,starting with A and going through Z. After the highest digit (which can be 0-Z), no further digits are possible;any

numbers which go past that digit are invalid. Negative numbers are prefixed with a single “-” sign.
Numbers never have leading zeros, with the exception of zero itself, which is represented by a single “0”character.
Operations:Addition (+) and subtraction (-): The numbers are added or subtracted as normal (includingcarrying, borrowing, etc).
Input:
The first line of input is the number (base 10) of operations that need to be performed.Each following line will be at most 1000 bytes and will

consist of a variable-radix number, a space, a
single character indicating the operation (+, or -), a space, another variable-radix number, and a
newline (LF).
Either number for any operation (and also the result) may be negative.
Output:
For each operation in the input, a single line of output should be produced containing either the result(a variable-radix number) or the string

“Invalid”) (without quotes) followed by a newline (LF).
If either of the input numbers or the resulting number is not valid in the number system, or an error is  encountered while performing the

operation, the result is invalid.
Input Output Notes
3 Number of operations; no output for this line
3 + 5

Invalid

9987654321 + 1

A000000000
-A000000000 - 1

-A000000001


 

題目大意:

給兩個 操作數 以及 一個操作符 輸出對應的結果

操作數 要求爲 最右邊第一位爲2進制 最右邊第二位爲3 進制 一次遞增 10用A表示 一直到 Z 進制 即35

 

代碼:

#include
#include
int flag1,flag2,len1,len2;
char s[1010];
int is_valid(char t[],int len,int flag)//檢測 輸入是否 非法
{
    int i,j;
    i=flag;
    for(i=len-1,j=1;i>=flag;i--,j++){
       if(j<10){
         if(t[i]-'0'>=0 && t[i]-'0' <=j)continue;
         else return 0;
       }
       else {
         if((t[i]-'0'>=0 && t[i]-'0'<=9) || ((t[i]-'A')>=0&&(t[i]-'A')<=(j-10)))
            continue;
         else return 0;
       }
   }
   return 1;
}
void  ans(char x[],char y[],char op)//計算 操作結果 注意結束賦值'\0'
{
   int i,j,k,otion,len,it,bsum,ss,big,at,bt,oprate;
   //總共分爲 8 中情況 。兩大類。
   if(op=='+'){
     if(flag1==0&&flag2==0)otion=1;
     else if(flag1==0&&flag2==1)otion=2;
     else if(flag1==1&&flag2==0)otion=3;
     else otion=4;
   }
   else {
     if(flag1==0&&flag2==0)otion=5;
     else if(flag1==0&&flag2==1)otion=6;
     else if(flag1==1&&flag2==0)otion=7;
     else otion=8;
   }
   for(i=flag1;i
       if(x[i]>='A'&&x[i]<='Z')x[i]=x[i]-'A'+10+'0';
   for(i=flag2;i
       if(y[i]>='A'&&y[i]<='Z')y[i]=y[i]-'A'+10+'0';  
   len= len1-flag1 > len2-flag2 ? len1-flag1 : len2-flag2;
   it=0;
   //第一大類, 採用加法計算 
   if(otion==1 || otion==4||otion==6 ||otion==7){
      if(otion==4 || otion==7){ss=-1;s[0]='-';}
      else ss=0;
      for(i=len1-1,j=len2-1,k=1;k<=len;k++,i--,j--)
      {
         if(i>=flag1 && j>=flag2)bsum=(x[i]-'0')+(y[j]-'0')+it;   
         else if(i>=flag1 && j
         else if(i=flag2)bsum=(y[j]-'0')+it;
         if(bsum>k){it=1;bsum=bsum-(k+1);}
         else it=0;
         if(bsum<10)s[k-1-ss]=bsum+'0'; 
         else s[k-1-ss]=bsum-10+'A';                     

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章