更多 CSP 认证考试题目题解可以前往:CSP-CCF 认证考试真题题解
原题链接: 202212-1 现值计算
时间限制: 1.0s
内存限制: 512.0MB
问题描述
评估一个长期项目的投资收益,资金的时间价值是一个必须要考虑到的因素。简单来说,假设银行的年利率为 5 % 5\% 5%,那么当前的 100 100 100 元一年后就会变成 105 105 105 元,两年后变成 110.25 110.25 110.25 元。因此,现在收到 100 100 100 元比两年后收到 100 100 100 元收益更多,两年后再支出 100 100 100 元会比立刻支出 100 100 100 元更加划算。
基于上述分析,我们使用如下的模型来衡量时间价值:假设银行的年利率为 i i i,当前(第 0 0 0 年)的 x x x 元就等价于第 k k k 年的 x × ( 1 + i ) k x \times (1 + i)^k x×(1+i)k 元;相应的,第 k k k 年的 x x x 元的当前价值实际为 x × ( 1 + i ) − k x \times (1 + i)^{-k} x×(1+i)−k 元。
现给出某项目未来 n n n 年的预计收入支出情况,在将所有款项转换为当前价值后,试计算该项目的总收益。
输入格式
从标准输入读入数据。
输入的第一行包含空格分隔的一个正整数 n n n 和一个实数 i i i,分别表示年数和银行年利率。
输入的第二行包含空格分隔的 n + 1 n+1 n+1 个整数,依次表示该项目第 0 , 1 , ⋯ , n 0, 1, \cdots, n 0,1,⋯,n 年的预计收入(正数)或支出(负数)。
输出格式
输出到标准输出中。
输出一个实数,表示该项目在当前价值标准下的总盈利或亏损。
样例输入
2 0.05
-200 100 100
样例输出
-14.059
样例说明
该项目当前支出 200 200 200 元,在接下来两年每年收入 100 100 100 元。虽然表面看起来收支相抵,但计算当前价值可知总共亏损了约 14.059 14.059 14.059 元。
子任务
全部的测试数据满足 0 < n ≤ 50 0 < n \le 50 0<n≤50, 0 < i < 1 0 < i < 1 0<i<1 且 i i i 的有效数字不多于 3 3 3 位,每年预计收入(正数)或支出(负数)的绝对值不大于 1000 1000 1000。
评分方式
如果你输出的浮点数与参考结果相比,满足绝对误差不大于 0.1 0.1 0.1,则该测试点满分,否则不得分。
提示
C/C++:建议使用 double 类型存储浮点数,并使用 scanf(“%lf”, &x); 进行输入,printf(“%f”, x); 进行输出。
Python:直接使用 print(x) 进行输出即可。
Java:建议使用 double 类型存储浮点数,可以使用 System.out.print(x); 进行输出。
题解
相当于计算 ∑ k = 0 n a j ( 1 + i ) k \sum\limits_{k=0}^n\dfrac{a_j}{(1+i)^k} k=0∑n(1+i)kaj。
( 1 + i ) k (1+i)^k (1+i)k 可以每次暴力算,也可以通过上一个循环的值再乘上一个 ( 1 + i ) (1+i) (1+i) 得到。
时间复杂度: O ( n ) \mathcal{O}(n) O(n)。
参考代码(15ms,3.046MB)
/*
Created by Pujx on 2024/3/25.
*/
#pragma GCC optimize(2, 3, "Ofast", "inline")
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
//#define int long long
//#define double long double
using i64 = long long;
using ui64 = unsigned long long;
using i128 = __int128;
#define inf (int)0x3f3f3f3f3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define yn(x) cout << (x ? "yes" : "no") << endl
#define Yn(x) cout << (x ? "Yes" : "No") << endl
#define YN(x) cout << (x ? "YES" : "NO") << endl
#define mem(x, i) memset(x, i, sizeof(x))
#define cinarr(a, n) for (int i = 1; i <= n; i++) cin >> a[i]
#define cinstl(a) for (auto& x : a) cin >> x;
#define coutarr(a, n) for (int i = 1; i <= n; i++) cout << a[i] << " \n"[i == n]
#define coutstl(a) for (const auto& x : a) cout << x << ' '; cout << endl
#define all(x) (x).begin(), (x).end()
#define md(x) (((x) % mod + mod) % mod)
#define ls (s << 1)
#define rs (s << 1 | 1)
#define ft first
#define se second
#define pii pair<int, int>
#ifdef DEBUG
#include "debug.h"
#else
#define dbg(...) void(0)
#endif
const int N = 2e5 + 5;
//const int M = 1e5 + 5;
const int mod = 998244353;
//const int mod = 1e9 + 7;
//template <typename T> T ksm(T a, i64 b) { T ans = 1; for (; b; a = 1ll * a * a, b >>= 1) if (b & 1) ans = 1ll * ans * a; return ans; }
//template <typename T> T ksm(T a, i64 b, T m = mod) { T ans = 1; for (; b; a = 1ll * a * a % m, b >>= 1) if (b & 1) ans = 1ll * ans * a % m; return ans; }
int a[N];
int n, m, t, k, q;
void work() {
double i, ans, pw;
cin >> n >> i >> ans;
i += 1, pw = i;
for (int j = 1; j <= n; j++) {
double tem;
cin >> tem;
ans += tem / pw;
pw *= i;
}
cout << ans << endl;
}
signed main() {
#ifdef LOCAL
freopen("C:\\Users\\admin\\CLionProjects\\Practice\\data.in", "r", stdin);
freopen("C:\\Users\\admin\\CLionProjects\\Practice\\data.out", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int Case = 1;
//cin >> Case;
while (Case--) work();
return 0;
}
/*
_____ _ _ _ __ __
| _ \ | | | | | | \ \ / /
| |_| | | | | | | | \ \/ /
| ___/ | | | | _ | | } {
| | | |_| | | |_| | / /\ \
|_| \_____/ \_____/ /_/ \_\
*/
关于代码的亿点点说明:
- 代码的主体部分位于
void work()
函数中,另外会有部分变量申明、结构体定义、函数定义在上方。#pragma ...
是用来开启 O2、O3 等优化加快代码速度。- 中间一大堆
#define ...
是我习惯上的一些宏定义,用来加快代码编写的速度。"debug.h"
头文件是我用于调试输出的代码,没有这个头文件也可以正常运行(前提是没定义DEBUG
宏),在程序中如果看到dbg(...)
是我中途调试的输出的语句,可能没删干净,但是没有提交上去没有任何影响。ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
这三句话是用于解除流同步,加快输入cin
输出cout
速度(这个输入输出流的速度很慢)。在小数据量无所谓,但是在比较大的读入时建议加这句话,避免读入输出超时。如果记不下来可以换用scanf
和printf
,但使用了这句话后,cin
和scanf
、cout
和printf
不能混用。- 将
main
函数和work
函数分开写纯属个人习惯,主要是为了多组数据。