用c++解决这个问题,求各路大神指点,最好能写出每条代码的作用,感激不尽!

2025-02-25 18:58:41
推荐回答(2个)
回答1:

#include
using namespace std;
void main()
{
int num,a[5],n=0;                   //num是输入的数,a[5]是用来存放美味树上的值。
cout << "请输入一个不多于5位的正整数:" << endl;
cin >> num;
int num1 = num;                     //num1=num,因为第一个while中最后num会变成0,没放进行后面的运算。
int counter = 0;
while (num != 0) 
{
a[n] = num % 10;                  //这儿是为将每位数存在a[]数组中,比如478%10=8,即a[0]=8.然后num=47,a[1]=7,......
n++;
counter++;
num = num / 10;            //用num/10. 比如num=23,23/10=2,继续,2/10=0.进行两次所以位数为2.
}
cout << "位数为:" << counter<< endl;
cout << "由个位起,每位上数字为:" << endl;
for (int i = 0; i < n; i++)
{
cout << a[i] << "  ";
}
cout << endl;
cout << "逆序排列:" << endl;
for (int i = 0; i < n; i++)
{
cout << a[i] ;
}
cout << endl;
cout << "平方值小于它的数:" << endl;
int j=0;
int m = 0;
while (j*j <= num1)              //用while,当j*j<=num1,输出j.
{
j++;
cout << j<<",";           
}
}

自己做的,可能有点繁琐!

回答2:

很有趣的样子,暂时不会啊。建议你找本有答案的书,每次这样问也不是办法。