ABC 209 B - Can you buy them all?

该博客介绍了一个简单的C++程序,用于解决一个商品折扣销售的问题。在折扣活动中,偶数索引的商品享有1日元的优惠,而奇数索引的商品按原价出售。用户需要判断给定的金额是否足够购买所有商品。程序通过读取商品数量和总金额,计算实际总价并与用户金额进行比较,最后输出能否购买所有商品的结果。
摘要由CSDN通过智能技术生成

https://atcoder.jp/contests/abc209/tasks/abc209_b

题目描述

Takahashi's shop sells N products. The usual price of the ii-th product is _{Ai} yen (Japanese currency).
It has a bargain sale today, with a discount of 1 yen off the usual prices for the 2-nd, 4-th, and the subsequent even-indexed products. The 1-st, 3-rd, and the subsequent odd-indexed products are sold for their usual prices.
You have X yen. Can you buy all the NN products with this money?

数据范围

  • 1≤N≤100
  • 1≤X≤10000
  • 1≤_{Ai}≤100
  • All values in input are integers.

 输入

Input is given from Standard Input in the following format:

N X
 …… 

输出

If you can buy all the N products, print Yes; otherwise, print No.

思路分析

水题,判断奇偶数,再拿相加值比大小


#include <iostream>
using namespace std;
int main () {
	int n,x,s=0;
	cin>>n>>x;
	int a[n+1];
	for (int i=1;i<=n;i++){
		cin>>a[i];
	}
	for (int i=1;i<=n;i++){
		if (i%2==0){
			a[i]--;
		}
		s+=a[i];
	}
	if (s<=x){
		cout<<"Yes";
	}
	else
		cout<<"No";
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值