A+B Coming

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/han_hhh/article/details/79956409

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. ^_^ 
InputInput may contain multiple test cases. Each case contains A and B in one line. 
A, B are hexadecimal number. 
Input terminates by EOF. 
OutputOutput A+B in decimal number in one line.Sample Input
1 9
A B
a b
Sample Output
10
21
21

不认识题目里边的英文单词就很尴尬了。还有这个竟然只需要用%x,%d就可以.......,我转的转的已经把自己给转晕了,

等我有空再解决这个问题

正解

#include <stdio.h>  
  
int main(void)  
{  
    int a, b;  
  
    while(scanf("%x%x", &a, &b) != EOF) {  
        printf("%d\n", a + b);  
    }  
  
    return 0;  
}  
错解
#include<stdio.h>
#include<ctype.h>
int convert(char x){
    if(isdigit(x))
        return x-'0';
    else if(islower(x))
        toupper(x);
        return x-'A'+10;
}
/*int convert(char x){
    if(islower(x-'0'))
        toupper(x-'0');
    if(x>='0'&&x<='9')
        return x-'0';
    else if(x>='A'&&x<='F')
        return x-'A'+10;
*/
int main(void){
    char a,b;

    while(scanf("%c%c",&a,&b)!=EOF){
        printf("%d\n",convert(a)+convert(b));
    }

}
#include<stdio.h>
#include<ctype.h>
int convert(char x){
    if(islower(x-'0'))
        toupper(x-'0');
    if(x>='0'&&x<='9')
        return x-'0';
    else if(x>='A'&&x<='F')
        return x-'A'+10;
}



阅读更多

没有更多推荐了,返回首页