实验3

1.题目描述

计算:s=x-x2+x3-x5+x8-……(0.1<x<0.5),要求精度10-8

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iomanip>
using namespace std;
int main(){
    double x,n=1.0,s=0,t=1.0;
    int p=1,q=1,cnt=0;
    cin>>x;
    if(x<=0.1 || x>=0.5)    return 0;
    while(fabs(t)>1e-8){
        cnt++;
        t=1.0;
        for(int i=1;i<=q;i++)    t*=x;
        if(cnt&1)    s+=t;
        else s-=t;
        int c=p+q;
        p=q,q=c;
    }
    cout<<setprecision(8)<<s<<endl;
    getchar();
    getchar();
}

 

2.题目描述

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
int main(){
    int n,a=2,b=1;
    double x,s=0.0;
    cin>>n>>x;
    for(int i=1;i<=n;i++){
        double t=1.0;
        for(int j=1;j<=a;j++)    t*=x;
        s+=double(t/b);
        int c=a+b;
        b=a,a=c;
    }
    cout<<s<<endl;
    getchar();
    getchar();
}

 

3.题目描述

 编写程序,s = 123x + + 123 + 12 + 1  (123x 表示 各位数字由最高位1递增到个位x )

由键盘输入 x的值(在 1 9 之间),例如 x = 6, 则以上表达式为:

         s = 123456 + 12345 + 1234 + 123 + 12 + 1

其和值是: 137171

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
int main(){
    int n,t=1,s=0;
    cin>>n;
    if(n<1 || n>9)    return 0;
    for(int i=1;i<=n;i++){
        s+=t;
        t*=10;
        t+=(i+1);
    }
    cout<<s<<endl;
    getchar();
    getchar();
}
posted @ 2021-09-29 21:21  LittleOrange  阅读(85)  评论(0编辑  收藏  举报