1001 A+B Format

题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400

主要思路:用sum不断模10,第n(n%3==0)前输出逗号,递归输出

出错点:第一次没有取abs在sum < 0时,输出类似 -1,-9-9-8。

收获(意外):abs的头文件是stdlib.h 或者 cstdlib;fabs 的头文件是math.h 和 cmath

cmath 与 abs的搭配 abs返回0

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 void Solve(int sum,int n){
 5   if(sum/10 == 0){
 6     printf("%d",sum);
 7     return;
 8   }
 9   n++;
10   int k = sum % 10;
11   Solve(sum/10,n);
12   if(n%3==0) printf(",");
13   printf("%d",abs(k));
14 }
15 int main(){
16   int a,b;
17   //for(int i = 0; i < 100; i++){
18     scanf("%d %d",&a,&b);
19     int sum = a+b;
20     Solve(sum,0);
21     //printf("\n");
22   //}
23 
24   return 0;
25 }

猜你喜欢

转载自www.cnblogs.com/jinjin-2018/p/9147956.html