标签:
3 29+4823 18be+6784 4ae1-3d6c
44114 100102 6565
本想拿这道题练一下进制转换的,看完最优代码才知道c语言有直接的十六进制,八进制占位符
我的代码
#include <iostream> #include <cstdio> using namespace std; int hd(char c) { if(c >= ‘a‘) return c-‘a‘+10; else return c-‘0‘; } int main() { int n; char c; scanf("%d",&n); getchar(); while(n--) { int a = 0, b = 0, d = 0,sign = -1; while(1) { //a进制转10进制的核心就是d=x*a^n+y*a^n-1+………………+z*a^0 scanf("%c",&c); if(c == ‘+‘ || c ==‘-‘) break; a = a*16 + hd(c); } if(c == ‘+‘) sign = 1; else sign = 0; while(1) { scanf("%c",&c); if(c == ‘\n‘) break; b = b*16 + hd(c); } if(sign) d = a + b; else d = a - b; printf("%o\n",d); } }
最优代码:
01.#include<stdio.h> 02.int main() 03.{ 04.int T; 05.scanf("%d",&T); 06.while(T--) 07.{ 08.int a,b,d; 09.char c; 10.scanf("%x%c%x",&a,&c,&b); 11.if(c==‘+‘) d=a+b; 12.else d=a-b; 13.if(d>=0) 14.printf("%o\n",d); 15.else printf("-%o\n",-d); 16.} 17.}
标签:
原文地址:http://www.cnblogs.com/ekinzhang/p/4321387.html