各位大哥大姐!两个很简单C++程序!救命啊!!!

来源:百度知道 编辑:UC知道 时间:2024/06/08 14:54:02
1.编制程序计算:1-1/2!+1/4!-1/6!+ … 计算到1/n!<0.000001为止。
2.请编写一个函数,函数的参数为一个3位整数,函数的功能是将该整数的各个位数重新排列,得到可能的
最大整数。编写主函数调用此函数,提示用户通过键盘输入3位整数,判断是否为有效输入并输出最后结果。
例如:某次运行的结果如下:
Input a three-digit integer: 10
Sorry, your input is not valid
Input a three-digit integer: 132
Possible max integer: 321

先写第一个:

#include"iostream.h"
#include "cmath"//调用里面的乘方函数pow()
int JC(int n);
main()
{
int m=1;
double sum=0;
while(JC(m)<=1000000)
{
sum+=pow(-1,m+1)/JC(m);
m++;
}
cout<<sum<<endl;
}

/*阶乘函数*/
int JC(int n)
{
int value=1,i;
for(i=1;i<=n;i++)
{
value*=i;
}
return value;
}

第二个:

#include"iostream.h"
int max(int k);
void main()
{
int n;
loop:
cout<<"Input a three-digit integer: "<<endl;
cin>>n;
if(n>999||n<100)
{
cout<<"Sorry, your input is not valid!"<<endl;
goto loop;
}

cout<<"Possible max integer:"<<endl<<max(n)<<endl;

}

/*max函数*/
int max(int k)
{
int