Codeforces Round 1050 A. Sublime Sequence
A. Sublime Sequence
Farmer John has an integer xx. He creates a sequence of length nn by alternating integers xx and −x−x, starting with xx.
For example, if n=5n=5, the sequence looks like: x,−x,x,−x,xx,−x,x,−x,x.
He asks you to find the sum of all integers in the sequence.
Input
The first line contains an integer tt (1≤t≤100) — the number of test cases.
The only line of input for each test case is two integers xx and nn (1≤x,n≤10).
Output
For each test case, output the sum of all integers in the sequence.
题意:n个x个数字,x数字为x,-x,x,-x等n个,计算这些x的和。
思路按照输入,然后整除2的为x,否则是-x,然后求和。
说明:可以一边输入一遍输出,也可以最后一起输出。最后输出需要一个vector数组或者int *数组
/*
4
1 4
2 5
3 6
4 7output
0
2
0
4
*/
#include <iostream>
using namespace std;int main()
{int t = 0;cin >> t;int x=0, n=0;int v1 = 0,sum=0;for (int i = 0; i < t; i++){cin >> x >> n;sum = 0;v1 = 0;for (int k = 0; k < n; k++){if (k%2==0){v1 = x;}else{v1 = x*(-1);}sum += v1;}cout << sum << endl;}return 0;
}