#include <iostream>
using namespace std;
int main()
{
int k;
cin >> k;//输入k个数
int a1=1,a2=1,a3;//a1、a2为第1、2个数,a3用于存储第i个数
for(int i =3;i<=k;i++)
{
a3 = a1+a2;//前两个数之和
a1=a2;
a2=a3;
}
cout << a3 << endl;
return 0;
}
鸡尾酒疗法
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;//输入n个方法
int a,b;
cin >> a >> b;
double x = b*1.0/a; //鸡尾酒疗法有效率
for( int i = 2 ; i <= n ; i++ )
{
cin >> a >> b;
double y = b*1.0/a;//其他疗法有效率
if( x - y >= 0.05)//做差比较
{
cout << "worse"<<endl;
}
else if( y - x >= 0.05)
{
cout << "better"<<endl;
}
else
{
cout << "same"<<endl;
}
}
return 0;
}
救援
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double n;
cin >> n ;//输入屋顶数
double time=0;//每一次的时间,路程
double l;//注意,这里要用全局变量,为取证做铺垫
for(int i = 1; i <= n;i++)//遍历每一个屋顶
{
double x,y;
cin >> x >> y;//x,y是循环量,每一次坐标
double people;
cin >> people;//people是循环量,每一次的人数
l = sqrt(1.0*x*x+1.0*y*y);//计算路程
time += 2*l*1.0/50+people*1.5;//计算时间
}
cout << ceil(time)<<endl;//将时间向上取整
return 0;
}
一元二次方程及判别式
#include<iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
double a, b, c;
cin >> a >> b>> c ;//随机输入三个数
double delt = b * b - 4 * a * c;//判别式
double x1 = (-b + sqrt( delt)) / (2 * a);//求根公式
double x2 = (-b - sqrt(delt )) / (2 * a);//求根公式
if( delt < 0 )
{
cout << "No answer!" << endl;
}
else if( delt ==0 )
{
cout << fixed << setprecision(5) << "x1=x2=" << x1 <<endl;
}
else
{
if (x1 > x2)
swap(x1,x2);
cout << fixed << setprecision(5) << "x1=" << x1 << ";"<< "x2=" << x2 <<endl;
}
return 0;
}
人口增长
#include <iostream>
#include <iomanip>
using namespace std;
double f(double a,double b)
{
for(int i =1 ;i <= b;i++)
{
a= a * 0.001 + a;
}
return a;
}
int main()
{
double x,n;
cin >> x >> n;
double ans = f(x,n);
cout << fixed <<setprecision(4)<<ans;
return 0;
}