打卡3

1.6 牛顿迭代法求根

 

 

   

 

 

不断迭代就可以了

#include<bits/stdc++.h>
using namespace std;

double f(double a,double b,double c,double d)
{
double x0,x=1.5,f,fd,h;
do
{
x0=x;
f=a*x0*x0*x0+b*x0*x0+c*x0+d;
fd=3*a*x0*x0+2*b*x0+c;
x=x0-f/fd;
}while(fabs(x-x0)>=1e-5);
return x;
}

int main()
{
double a,b,c,d,x;
cin>>a>>b>>c>>d;
cout<<"答案是:"<<f(a,b,c,d)<<endl;
}

 

 

1.7最佳存款方案

这道题非常简单就代码就行

#include<bits/stdc++.h>
using namespace std;

int main()
{
double ans=0;
for(int i=0;i<5;i++)
{
ans+=1000;
ans/=(1+0.0063*12);
}
cout<<ans<<endl;
}

 

 

1.8冒泡排序

问题:对n个数进行升序排序

 

流程图:

 

 

#include<bits/stdc++.h>
using namespace std;

int main()
{
int n,a[100010];
cin>>n;
for(int i=0;i<n;i++)cin>>a[i];
for(int i=0;i<n;i++)
{
for(int j=i;j<n-1;j++)
{
if(a[j]>a[j+1])
{
swap(a[j],a[j+1]);
}
}
}
for(int i=0;i<n;i++)cout<<a[i]<<' ';
}

 

posted on 2023-04-12 21:08  临江柔  阅读(12)  评论(0编辑  收藏  举报