1. 温度转换
题目链接:http://csp.magu.ltd/problem/J0604
【问题描述】
摄氏温度和华氏温度的转化公式为,
C
=
5
∗
(
F
−
32
)
/
9
C=5*(F-32)/9
C=5∗(F−32)/9(其中C表示摄氏温度,F表示华氏温度)。
编写程序,输入华氏温度F,输出摄氏温度,结果保留到小数点后5位。
【题解代码】
#include <iostream>
using namespace std;
int main()
{
double F;
scanf("%lf", &F);
printf("%.5f", 5 * (F - 32) / 9.0);
return 0;
}
2. 计算人口数量
题目链接:http://csp.magu.ltd/problem/J0605
【问题描述】
某国现有x亿人口,每年的增长率为千分之a,编写程序,计算n年后的人口数。
【题解代码】
#include <iostream>
using namespace std;
int main()
{
double x;
int a, n;
scanf("%lf%d%d", &x, &a, &n);
while (n--)
{
x = x + x * (0.001 * a);
}
printf("%.4f", x);
return 0;
}
3. 求e的值
题目链接:http://csp.magu.ltd/problem/J0606
【问题描述】
数学中有许多重要的常数,如圆周率π和虚数单位i、自然对数的底数e。e是一个无限不循环小数,且为超越数,其值约为2.718281828459045。
e的计算公式为: e = 1 + 1 / 1 ! + 1 / 2 ! + 1 / 3 ! + . . . + 1 / n ! e = 1 + 1/1! + 1/2! + 1/3! + ... + 1/n! e=1+1/1!+1/2!+1/3!+...+1/n!
编写程序,输入整数n,计算e的值,结果保留到小数点后12位。
【题解代码】
#include <iostream>
using namespace std;
int main()
{
int n;
scanf("%d", &n);
double e = 1;
long long m = 1;
for (int i = 1; i <= n; i++)
{
m *= i;
e += 1.0 / m;
}
printf("%.12f", e);
return 0;
}
4. 超越
题目链接:http://csp.magu.ltd/problem/J0607
【问题描述】
购买不同的理财产品,财富的增长速度会有很大差别。小明起初有 a a a 元存款,选择了年增长速度为 b % b\% b% 的理财方式;小刚起初有 x x x 元存款,选择了年增长速度为 y % y\% y% 的理财方式,所有理财产品都是年复利,即一年进行一次利息计算,所得的利息和本金一起作为下一年的本金。
编写程序,计算经过多少年,小刚的存款会超过小明。
【题解代码】
#include <iostream>
using namespace std;
int main()
{
double a, b, x, y;
scanf("%lf%lf%lf%lf", &a, &b, &x, &y);
int year = 0, t = 50;
while (t--)
{
if (x > a)
{
cout << year << endl;
return 0;
}
a = (1 + (0.01 * b)) * a;
x = (1 + (0.01 * y)) * x;
year++;
}
cout << "NOT POSSIBLE";
return 0;
}
470

被折叠的 条评论
为什么被折叠?



