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 7

output
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;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值