现在的位置: 首页 > 综合 > 正文

hdu 1720(ACM steps 1.2.2)A+B coming

2013年11月04日 ⁄ 综合 ⁄ 共 1075字 ⁄ 字号 评论关闭

题目描述

A+B Coming

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4554    Accepted Submission(s): 2917

Problem Description
Many classmates said to me that A+B is must needs.
If you can’t AC this problem, you would invite me for night meal. ^_^
 

Input
Input may contain multiple test cases. Each case contains A and B in one line.
A, B are hexadecimal number.
Input terminates by EOF.
 

Output
Output A+B in decimal number in one line.
 

Sample Input
1 9 A B a b
 

Sample Output
10 21 21
 

Author
威士忌
 

Source
 

Recommend
lcy
总结:
1.求八进制数的和,需要注意的是输入不止一位数,所以需要用%s
2.scanf()以Space、Enter、Tab结束一次输入
   gets()以Enter结束输入(空格不结束),接受空格,会舍弃最后的回车符!
第二:为了避免出现上述问题,必须要清空缓冲区的残留数据,可以用以下的方法解决:
   方法1:C语言里提供了函数清空缓冲区,只要在读数据之前先清空缓冲区就没问题了!  
     这个函数是fflush(stdin)。
#include<stdlib.h>

#include<stdio.h>


int solve(char a)

{

   if(a>='0'&&a<='9')

         return a-'0';

       else if(a>='a'&&a<='z')

        return a-'a'+10;

       else if(a>='A'&&a<='Z')

         return a-'A'+10;

}

int main()

{

    int i,x,y;

    char sa[1005],sb[1005];

    while(scanf("%s %s",sa,sb)!=EOF)

    {

       x=0;y=0;

       for(i=0;sa[i]!='\0';i++)

       {

         x=x*16+solve(sa[i]);

       }

       

       for(i=0;sb[i]!='\0';i++)

       {

         y=y*16+solve(sb[i]);

       }

         

       printf("%d\n",x+y);

    }

    return 0;

   

}

抱歉!评论已关闭.