题解 | 添加逗号
添加逗号
https://www.nowcoder.com/practice/f51c317e745649c0900996fd3f683aed
#include <stdio.h>
#include <string.h>
int main ()
{
char str[1000];
scanf("%s",str);
int len = strlen(str);
int m = len % 3;
char str1[1000];
int count1 = 0,count2 = 0;
if(len<3){
printf("%s\n",str);
}else
{
if(m>0){
for(int i = 0;i<m;i++){
str1[count1++] = str[i];
}
str1[count1++] = ',';
}
for(int i = m;str[i] != '\0';i++){
str1[count1++] = str[i];
count2++;
if(count2 == 3&&str[i+1] != '\0'){
str1[count1++] = ',';
count2 = 0;
}
}
printf("%s\n",str1);
}
return 0;
}
新手写的很老实的写法望指正
查看27道真题和解析