1 /* 2 *Main.c 3 *1022. D进制的A+B Ver.1 4 *Created on : 2014.9.5 5 *****测试通过****** 6 */ 7 8 #include <stdio.h> 9 #include <stdlib.h> 10 11 void cal1(int sum, int D); 12 13 int main(void){ 14 15 int A, B, sum;//题目中的A、B 16 int D; 17 18 scanf_s("%d %d %d", &A, &B, &D); 19 20 sum = A + B; 21 22 cal1(sum, D); 23 printf(" "); 24 25 system("pause"); 26 return 0; 27 28 } 29 30 void cal1(int sum, int D){ 31 32 if (sum / D != 0){ 33 cal1(sum / D, D); 34 } 35 printf("%d", sum%D); 36 }
1 /* 2 *Main.c 3 *1022. D进制的A+B Ver.2 4 *Author : Boomkeeper 5 *Created on : 2014.9.5 6 *****部分通过**** 7 */ 8 9 #include <stdio.h> 10 #include <stdlib.h> 11 12 void cal2(int sum, int D); 13 14 int main(void){ 15 16 int A, B, sum;//题目中的A、B 17 int D; 18 19 scanf("%d %d %d", &A, &B, &D); 20 21 sum = A + B; 22 23 cal2(sum, D); 24 printf(" "); 25 26 system("pause"); 27 return 0; 28 29 } 30 31 void cal2(int sum, int D){ 32 33 int array[100]; 34 int i = 0; 35 36 while (sum != 0){ 37 array[i++] = sum%D; 38 sum /= D; 39 } 40 41 if (array[i - 1] == 0) 42 i = i - 2; 43 else 44 i = i - 1; 45 while (i >= 0){ 46 printf("%d", array[i]); 47 i--; 48 } 49 } 50
明天奔赴考场,今晚最后一题练手,希望顺利!
同祝“PATers”凯旋!
(话说从Eclipse转到VS上真是憋手蹩脚%>_<%)
题目链接:
http://pat.zju.edu.cn/contests/pat-b-practise/1022
参考: