All Your Base 模拟题

来源:互联网 发布:sql 合计函数 编辑:程序博客网 时间:2024/06/05 08:34

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

a specified operation on them and output the result in ourbase.
The Base:A number system where the right-most digit (digit 1) canbe 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, eachdigit 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 arepossible;any

numbers which go past that digit are invalid. Negative numbersare prefixed with a single “-” sign.
Numbers never have leading zeros, with the exception of zeroitself, which is represented by a single “0”character.
Operations:Addition (+) and subtraction (-): The numbers are addedor subtracted as normal (includingcarrying, borrowing, etc).
Input:
The first line of input is the number (base 10) of operations thatneed to be performed.Each following line will be at most 1000 bytesand 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 benegative.
Output:
For each operation in the input, a single line of output should beproduced containing either the result(a variable-radix number) orthe string

“Invalid”) (without quotes) followed by a newline (LF).
If either of the input numbers or the resulting number is not validin the number system, or an error is  encounteredwhile 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)//检测 输入是否 非法
{
    inti,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'
{
   inti,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'; 
        elses[k-1-ss]=bsum-10+'A';                                
     }
     if(it!=0){s[k-1-ss]='1';s[k-ss]='\0';}
     else s[k-1-ss]='\0';
   }
   else {
   //第二大类 减法,同时 先要比较两数绝对值
     if(len1-flag1>len2-flag2)big=1;
     else if(len1-flag1
     else {
        int tp;
        tp=strcmp(x+flag1,y+flag2);
        if(tp>=0)big=1;
        else big=2;
     }
     if(big==1){at=1;bt=-1;}
     else {at=-1;bt=1;}
     if(((otion==2 || otion==5 )&&big==1)||
       ((otion==8 || otion==3 )&&big==2))
         oprate=0;
     else {oprate=-1;s[0]='-';}
     for(i=len1-1,j=len2-1,k=1;k<=len;k++,i--,j--){       
        if(i>=flag1 &&j>=flag2)bsum=(x[i]-'0')*at+(y[j]-'0')*bt+it;   
        else if(i>=flag1 && j
        else if(i=flag2)bsum=(y[j]-'0')*bt+it;
        if(bsum<0){it=-1;bsum=bsum+(k+1);}
        else it=0;
        if(bsum<10)s[k-1-oprate]=bsum+'0';
        elses[k-1-oprate]=bsum-10+'A';                         
     }
     s[k-1-oprate]='\0';                   
   
}
int main()
{
    intn,i,j;//=+
    chara[1010],op,b[1010],t;
   scanf("%d",&n);
   while(n--){
      scanf("%s%c%c%s",a,&op,&t,b);
      len1=strlen(a);
      len2=strlen(b);
      flag1=flag2=0;
      if(a[0]=='-')flag1=1;
      if(b[0]=='-')flag2=1;
      if(len1-flag1>35 ||len2-flag2 >35){
         printf("Invalid\n");
         continue;
      
      if(is_valid(a,len1,flag1)&&is_valid(b,len2,flag2)){
         ans(a,b,t);
         //输出 一 倒序 二 不能有前导0 三 多余的0 不要输出 四 结果为0时输出 0即可
         int ln=strlen(s);
         int fg=0;
         if(s[0]=='-')fg=1;
         if(ln-fg>35)printf("Invalid\n");
         else {
            for(i=ln-1;i>=fg;i--)
              if(s[i]=='0')continue;
              else break;
            if(i>=fg){
              if(fg)printf("-");
              for(i;i>=fg;i--)
                printf("%c",s[i]);
            }
            else printf("0");
            printf("\n");
         }
      }
      else  printf("Invalid\n");
    }
    return0;
       


 

原创粉丝点击