题解【CodeForces171C】A Piece of Cake
Description
给你\(n\)个数,求出\(\sum_{i=1}^{n} a_{i}\times i\qquad\)
Input
共\(n + 1\)个数,分别为\(n\)和\(n\)个数\(a_{i}\)(\(1\)\(\leq i \le n\))。
Output
\(\sum_{i=1}^{n} a_{i}\times i\qquad\)
Examples
Input
4 1 2 3 4
Output
30
Solution
根据题目翻译可知:这是一道模拟题。
首先,输入一个整数\(n\),接下来输入\(n\)个整数,分别为\(a_{1}\),\(a_{2}\), \(\cdots\),\(a_{n}\) ,要你输出\(ans\) \(=\) \(\sum_{i=1}^{n} a_{i}\times i\qquad\)
我们可以用一层循环来枚举\(i\),\(i\)的范围从\(1\) ~ \(n\),每次输入\(a_{i}\)时,就可以将答案\(ans\)增加\(a_{i}\) \(\times\) \(i\),最后输出答案\(ans\)即可。
算法时间复杂度:\(\Theta(n)\),空间复杂度:\(\Theta(1)\)
Code
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>//头文件准备
using namespace std;//使用标准名字空间
inline int gi()//快速读入,不解释
{
int f = 1, x = 0;
char c = getchar();
while (c < '0' || c > '9')
{
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
{
x = x * 10 + c - '0';
c = getchar();
}
return f * x;
}
int n, sum;//n为数字的个数,sum为答案
int main()//进入主函数
{
n = gi();//输入数字个数
for (int i = 1; i <= n; i++)
{
int x = gi();//依次输入每个数
sum = sum + i * x;//更新答案
}
printf("%d\n", sum);//输出最终答案
return 0;//完美结束
}
题解【CodeForces171C】A Piece of Cake的更多相关文章
- A - Piece of Cake Kattis - pieceofcake (数学)
题目链接: A - Piece of Cake Kattis - pieceofcake 题目大意:给你一个多边形,然后给你这个多边形的每个点的坐标,让你从这个n个点中选出k个点,问这个k个点形成的面 ...
- Jarvis OJ A Piece Of Cake
看图片的隐写术自闭,本来想看一看jarvisoj 的basic放松一下心情,结果一道题就做了一晚上qwq 首先看到这道题的时候想到的是凯撒密码(这其实是Google之后才知道这个名字的)枚举了26种位 ...
- NAIPC 2019 A - Piece of Cake(凸包计算)
学习:https://blog.csdn.net/qq_21334057/article/details/99550805 题意:从nn个点中选择kk个点构成多边形,问期望面积. 题解:如果能够确定两 ...
- SCUT - 249 - A piece of Cake - 组合数学
https://scut.online/contest/25/I 由结论:d维物体切n刀分成的部分=sum(C(n,0)~C(n,d)),直接算就行了.
- CF171C 【A Piece of Cake】
遵从题意枚举暴力读人n,再求出$\sum^n_1a[i]*i$然后输出答案,(记得开个long long以免炸掉)以下是代码: #include<bits/stdc++.h> using ...
- 【LeetCode】1465. 切割后面积最大的蛋糕 Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 找最大间隔之积 日期 题目地址:https://lee ...
- Anniversary Cake
Anniversary Cake Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 15704 Accepted: 5123 ...
- POJ 1020 Anniversary Cake(DFS)
Anniversary Cake Time Limit: 1000MSMemory Limit: 10000KB64bit IO Format: %I64d & %I64u Submit St ...
- poj 1020 Anniversary Cake(切正方形蛋糕+搜索)
...
随机推荐
- 输出《Harry Potter and the Sorcerer's Stone》文本中的前N个最长用的英文单词及其数量
输出<Harry Potter and the Sorcerer's Stone>文本中的前N个最长用的英文单词及其数量 实验思路: 1. 利用输入流将文件当中内容读入. 2. 将文件内容 ...
- linux 第一个知识点(my)
关于Linux - rwx rwx rwx root user 194 Oct : 19 21:24 test -:此符号为文件名,如果是d 则为目录,如果是l 则为连接 rwx: 这是所有者所有的 ...
- Codeforces Round #624 (Div. 3)
A.题意:通过加奇数减偶数的操作从a到b最少需要几步 签到题 #include <algorithm> #include <iostream> #include <cst ...
- golang channel 的一次内存错误
起因 原因调查 原因分析 问题解决 总结 起因 今天在做数据库数据读取时, 首先通过多个 goroutine 将从数据库读取的数据写入 channel, 同时通过另一个 goroutine 从 cha ...
- 4P遇上了5P
(1)4P工作要素:任何一位从业者,都应该好好想想自己工作的初衷是什么?你将自己所从事的工作又是定位在什么位置?而这份工作的视角又有多宽.多广?最后是你会在某个周期内完成工作或者是实现突破. (2)5 ...
- mybatis第一天02
mybatis第二天02 1.映射文件之输入输出映射 1.1映射文件之输入映射类型(parameterType) 1.1.1简单类型 当parameterType为简单类型时,我们只需要直接填写“in ...
- 2018ICPC南京站Problem J. Prime Game
题意: 对于所有数字分解质因子,如果某个质因子在这个区间出现,则贡献为1,求所有质因子对所有区间做的贡献. 解析: 考虑如果所有全部区间都有这个质因子则这个质因子的贡献是n*(n+1)/2,对于任意因 ...
- Yaf自定义autoload以实现Model文件和Controller文件命名区分
先上图: 由于Yaf作者在设计Yaf框架目录时没有直接区分开models文件和controllers文件,所以在IDE看着会很难受,眼睛离开了编辑器就不大好区分这两个文件夹的文件.所以自己写了一个au ...
- bootstrap之表格自适应 table-responsive
<div class=table-responsive"> <table class="table"> <thead> <tr& ...
- nginx ip配置反向代理为本地域名
#### gitlab反向代理 server { listen ; server_name gitlab.hp.com; location / { proxy_pass http://192.168. ...