求助!!c++程序设计设s=1+1/2+1/3+....+1/n,求与八最接近的s的值与其对应的n值。最好用到do。。。while语

供稿:hz-xin.com     日期:2025-01-13
c++程序设计设s=1+1/2+1/3+....+1/n,求与八最接近的s的值与其对应的n值。最好用到do。。。while语

声明一个int型变量n作为分母兼作项数记数变量,声明一个double型变量s记录前n项之和;利用do~while语句,n从1开始到当前项的浮点和小于8时继续循环,否则退出。退出循环后,比较s-8和8-(s-1.0/n),取小者(相等时取前一项)的最后一项的n便是题解。代码如下:
#include "stdio.h"int main(int argc,char *argv[]){int n;double s;s=n=0;do{s+=1.0/++n;}while(s 8-s+1.0/n)s-=1.0/n--;printf("s = %fn = %d
",s,n);return 0;}运行结果如下:


#include
int main()
{ int i=0;
double s=0;
while(s<8)
s+=1.0/++i;
if(s-8<8-(s-1.0/(i-1)))
printf("s=%fi=%d
",s,i);
else printf("s=%fi=%d
",s-1.0/(i-1),i-1);
return 0;
}

#include<iostream>
#include<windows.h>
using std::cout ;
using std::endl ;
int main()
{
int n=1 ;
double s=0,s2 ; //s是当前表达式的和,s2是上一次计算时的和。
do{
s2=s ;
s+=(double)(1.0/n) ;
n++ ;
}while(s<8) ; //退出循环的时候,s大于8,s2小于等于8
if(8-s2>s-8) //算绝对值。。。小的输出
cout<<"s="<<s<<" n="<<n-1<<endl ;
else cout<<"s="<<s2<<" n="<<n-2<<endl ; //输出
return 0 ; //return 0;
}

#include<iostream>
using namespace std;
void main()
{
int n=1;
double s=0;
do
{
s=s+(1.0/n);
n++;
}
while(s<8.0);
cout <<"s="<<s<<endl;
cout <<"n="<<n<<endl;
}

#include<cstdio>
int main()
{
double s1=0,s2;
int t=0;
do {
t++;
s2=s1;
s1+=1.0/t;
}while(s1<8);
if(s1+s2>16) printf("%lf %d\n",s2,t-1);
else printf("%lf %d\n",s1,t);
}
补:
#include<cstdio>
#include<cmath>
int main()
{
int n;
while(~scanf("%d",&n)){
double s1=0,s2;
int t=0;
do {
t++;
s2=s1;
s1+=1.0/t;
}while(s1<8&&t<n);
if(fabs(s1-8)>fabs(8-s2)) printf("%lf %d\n",s2,t-1);
else printf("%lf %d\n",s1,t);
}
}

#include<iostream>
using namespace std;
int main()
{
double sum=0;
double i=1;
do
{
sum+=(1.0/i);
i+=1;
}while(sum<8.0);
i-=1;
double temp = sum-1.0/i;
double a = 8-temp;
double b = sum-8;
if (a<b)
{
sum = temp;
i-=1;
}
cout<<i<<"\t"<<sum<<endl;
}

#include<iostream>
void main()
{
int n=1;
double limit;
double sum=0;
cin>>limit;//输入limit
while(1)
{
sum=sum+(double)(1/n);//求和
if (sum>limit)//满足条件
{
cout<<n;//输出n
return;//跳出循环
}
n++;
}
return;
}

求助!!c++程序设计设s=1+1\/2+1\/3+...+1\/n,求与八最接近的s的值与其对...
do{ s2=s ;s+=(double)(1.0\/n) ;n++ ;}while(s<8) ; \/\/退出循环的时候,s大于8,s2小于等于8 if(8-s2>s-8) \/\/算绝对值。。。小的输出 cout<<"s="<<s<<" n="<<n-1<<endl ;else cout<<"s="<<s2<<" n="<<n-2<<endl ; \/\/输出 return 0 ; \/\/retur...

用c++,求求各位大佬了!设 s=1+1\/2+1\/3+...+1\/n,求与8最接近的s值及与...
C++代码和运行结果如下:可见与8最接近的s为8.00049,与之对应的n为1673 附C++源码:include <iostream> using namespace std;int main() { double s = 0, last_s;int n = 0;while (s < 8) { n++;s += 1.0 \/ n;last_s = s;} cout << "1+1\/2+1\/3+...+1\/";if (8...

C语言编程,s=1+1\/2-1\/3+1\/4-1\/5...+1\/n,
{int i;double j;double s=1;for(i=2;i<=n;i++) \/\/从2开始,s初值是1了{ \/\/多条语句要加括号j=1.0\/i; \/\/有1.0参与,不用加强转了if(i%2!=0) \/\/这里应该是ij=-j;s+=j;}printf("%lf\\n",s); \/\/double用%lf long float}void main(){int n;double s=1;printf("...

c++程序设计设s=1+1\/2+1\/3+...+1\/n,求与八最接近的s的值与其对应的n值...
include "stdio.h"int main(int argc,char *argv[]){int n;double s;s=n=0;do{s+=1.0\/++n;}while(s<8);if(s-8 > 8-s+1.0\/n)s-=1.0\/n--;printf("s = %f\\tn = %d\\n",s,n);return 0;}运行结果如下:

怎么用C语言计算S=1+1\/(1+2)+1\/(1+2+3)+……+1\/(1+2+3+……+100)
include "stdio.h"int main(int argv,char *argc[]){double s;int i,t;for(s=t=0,i=1;i<101;s+=1.0\/(t+=i),i++);printf("The result are %f\\n",s);return 0;}运行结果:

C++输入一个正整数n,编程实现求s=1+1+2+1+2+3+...1+2+3+...n
二楼的给的是c语言的 啊!楼主要的时c++吗?我写个c++的吧!include <iostream> using namespace std;int main(){ int n,i,j,s=0;int a[100]={0};cout<<"请输入小于100的正整数n:"<<endl;cin>>n;for(i=0;i<n;i++){ for(j=0;j<=i;j++){ a[i]=a[i]+j+1;} s+=...

编写程序,计算s=1+1\/(1*2)+1\/(1*2*3)+1\/(1*2*3*4)+。。。+1\/(1*2*3...
getchar();\/\/DEV-C++编译器需要此行等待回车以使输出结果可见 } double njie(int n)\/\/求n! 函数 { int i,njie=1;for(i=1;i<=n;i++)njie=njie*i;return njie;} void ok(int n)\/\/求各项和函数 { int i;double sum=0;for(i=1;i<=n;i++)sum=sum+1.0\/njie(i);printf("...

c++中s+=i+1什么意思
把i先+1,然后再累加到s中,相当于 s=s+i+1

C++编写程序,计算s=1+(1+2)+(1+2+3)+…+(1+2+3+…+n)的值
include usingnamespacestd;intmain(){ intn;staticintsum=1;cout<<"请输入N:"<<endl;cin>>n;for(inti=1;i<=n;i++){ for(intj=1;j<=i;j++){ sum=sum+j;} } cout<<"结果2为:"<<sum;system("pause");return0;

用C++编程1+1=?
include <iostream> using namespace std;int main(){ int i;i=1+1;cout<<"1+1="<<i<<endl;return 0;}