在一行中输入一个整数
![]()
。
输出一个字符串,表示将
转换为千分位格式后的结果。
请不要输出多余的空格或换行。
980364535
980,364,535
6
6
#include <stdio.h>
#include <string.h>
int main()
{
char n[1000000]={0};//初始化,全为0
scanf("%s",n);
for(int i=0;i<strlen(n);i++)//strlen函数是在<string.h>下的一个计算字符串长度的函数
{
printf("%c",n[i]);
if((strlen(n)-(i+1))%3==0 && (i+1) != strlen(n))//相当于i从1开始每三个,小于n
{
printf(",");
}
}
return 0;
} #include <stdio.h>
int main(){
long n;
int arr[20] = { 0 }, i = 0;
scanf("%ld", &n);
while(n){
arr[i] = n % 10;
n /= 10;
i++;
}
for(int j = i - 1, count = 0; j >= 0; j--, count++){
printf("%d", arr[j]);
if((i-1 - count) % 3 == 0 && count < i - 1)
printf(",");
}
return 0;
} #include <iostream>
#include <string>
using namespace std;
int main() {
string N, A;
cin >> N;
int t = N.size();
for (int i = t - 1; i >= 0; i--) {
A[t - i - 1] = N[i];
}
for (int i = t - 1; i >= 0; i--) {
cout << A[i];
if ((i % 3 == 0 && i != 0))
cout << ',';
}
return 0;
} int main() { int n; char str[20];//用来逆序存放最终输出的数字n和字符',' int i = 0;//作为str字符数组的下标 int count = 0;//用来计数 scanf("%d", &n); while (n)//结束条件 { if (count == 3)//count每到3就放一个逗号字符到字符数组里 { str[i] = ','; count = 0;//count重新开始计数 goto qu; } int num= n % 10; sprintf(&str[i], "%d", num);//sprintf是格式转换函数,把数字转换成对应的字符 n /= 10; count++; qu: i++;//每放进去一个字符,下标就++一下 } for (int j = i - 1; j >= 0; j--)//逆序打印出字符数组即可 { printf("%c", str[j]); } return 0; }