AtCoder Beginner Contest 082 A - Round Up the Mean

本文针对AtCoder平台上的一个基础算法题目进行了解析,题目要求计算两个正整数的平均值并向上取整。通过示例输入输出展示了如何使用C++实现这一逻辑,包括对结果为小数时的正确处理。

题目链接:https://abc082.contest.atcoder.jp/tasks/abc082_a

Time limit : 2sec / Memory limit : 256MB

Score : 100 points

Problem Statement

You are given two positive integers a and b. Let x be the average of a and b. Print x rounded up to the nearest integer.

Constraints

  • a and b are integers.
  • 1≤a,b≤100

Input

Input is given from Standard Input in the following format:

a b

Output

Print x rounded up to the nearest integer.


Sample Input 1

Copy
1 3

Sample Output 1

Copy
2

The average of 1 and 3 is 2.0, and it will be rounded up to the nearest integer, 2.


Sample Input 2

Copy
7 4

Sample Output 2

Copy
6

The average of 7 and 4 is 5.5, and it will be rounded up to the nearest integer, 6.


Sample Input 3

Copy
5 5

Sample Output 3

Copy
5

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <stack>
 5 #include <string>
 6 #include <cstring>
 7 #include <cmath>
 8 #include <cstdio>
 9 using namespace std;
10 int main()
11 {
12     int a,b;
13     while(cin>>a>>b){
14         if((a+b)%2==0) cout<<(a+b)/2<<endl;
15         else{
16             int c=(a+b)/2;
17             if(a+b-2*c<=1) cout<<c+1<<endl;
18             else cout<<c<<endl;
19         }
20     }
21     return 0;
22 }

 

转载于:https://www.cnblogs.com/shixinzei/p/8075871.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值