样例输入:5
100 200 50 300 50
3
3 4 5
样例输出:
350,650,700
算法一
算法优化
- #include <bits/stdc++.h>
- using namespace std;
- int s[100010];
- int main()
- {
- int m, n, x, i, j, sum;
- cin >> n;
- for(i = 1; i <= n; i++)
- cin >> s[i];
- cin >> m;
- for(i = 1; i <= m; i++)
- {
- cin >> x;
- sum = 0;
- for(j = 1; j <= x; j++)
- sum = sum + s[j];
- printf("%d", sum);
- if(i < m)
- printf(",");
- }
- return 0;
- }
- #include <bits/stdc++.h>
- using namespace std;
- int s[100010], sum[100010];
- int main()
- {
- int m, n, x, i, j;
- cin >> n;
- for(i = 1; i <= n; i++)
- {
- cin >> s[i];
- sum[i] = sum[i - 1] + s[i];
- }
- cin >> m;
- for(i = 1; i <= m; i++)
- {
- cin >> x;
- printf("%d", sum[x]);
- if(i < m)
- printf(",");
- }
- return 0;
- }