循环结构程序设计
程序员文章站
2022-07-12 16:56:47
...
输入实数x和整数n,输出x+x2+…+xn
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double x,sum=0;
int n;
cout<<"pls input x and n:";
cin>>x>>n;
for(int i=1;i<=n;i++)
{
sum+=pow(x,i);
}
cout<<"sum="<<sum<<endl;
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double sum=0;
int m,n;
cout<<"pls input m and n:";
cin>>m>>n;
for(int i=1;i<=2*m-1;i+=2)
{
sum+=pow(i,n);
}
cout<<"sum="<<sum<<endl;
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int f1=1,f2=1,f3;
cout<<f1<<"\t"<<f2;
for(int i=3;i<=20;i++)
{
f3 = f1 + f2;
cout<<"\t"<<f3;
f1 = f2;
f2 = f3;
}
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
for(int i=100;i<=999;i++)
{
int a,b,c;
a=i/100;
b=(i-bai*100)/10;
c=i%10;
if(pow(a,3)+pow(b,3)+pow(c,3) == i)
cout<<"找到一个水仙花数:"<<i<<endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int x,p=0,n=0,j=0,o=0;
cin>>x;
while(x!=0)
{
if(x>0)
p++;
if(x<0)
n++;
if(x%2==0)
o++;
else if(x%2!=0)
j++;
cin>>x;
}
cout<<p<<" "<<n<<" "<<j<<" "<<o;
return 0;
}```