猿问

帮忙讲下程序中ps( )每一行的意思?

#include <iostream>
using namespace std;

int ps(int n)
{
if(n<1000){cout<<n;return 0;}
ps(n/1000);
cout<<','<<n%1000/100<<n%100/10<<n%10;
}
int main()
{
int n;
cout<<"请输入:";
while(cin>>n,cin.good())
{
//if(n<0){n=-n;cout<<'-';}
ps(n);
cout<<endl;
}
return 0;
}

守着一只汪
浏览 146回答 1
1回答

慕桂英4014372

#include <iostream> // 预处理命令using namespace std; // 命名空间int ps(int n) //定义函数 返回为整形{if(n<1000){cout<<n; //若n小于1000输出nreturn 0; //返回0}ps(n/1000); //递归调用函数自身,n除以1000cout<<','<<n%1000/100<<n%100/10<<n%10; // 输出"," 逗号后3位}int main() //主函数{int n; //定义整形ncout<<"请输入:"; // 输出 请输入:while(cin>>n,cin.good()) // 当输入正确时开始,也就是当输入不是整形时停止{ps(n); //调用前面定义的ps函数cout<<endl; // 输出换行}return 0; // 返回0}整个程序就是要输入一个数字 若小于1000 则按输入的输出若大于1000 则将后面3位与之前的数字用逗号隔开
随时随地看视频慕课网APP
我要回答