AtCoder题解——Beginner Contest 170——A - Five Variables

题目相关

题目链接

AtCoder Beginner Contest 170 A题,https://atcoder.jp/contests/abc170/tasks/abc170_a

真不好意思,水一题吧。大家原谅。

Problem Statement

We have five variables x1,x2,x3,x4, and x5.

The variable xi was initially assigned a value of i.

Snuke chose one of these variables and assigned it 0.

You are given the values of the five variables after this assignment.

Find out which variable Snuke assigned 0.

Input

Input is given from Standard Input in the following format:

x1 x2 x3 x4 x5

Output

If the variable Snuke assigned 0 was xi, print the integer i.

Samples1

Sample Input 1

0 2 3 4 5

Sample Output 1

1

Explaination

In this case, Snuke assigned 0 to x1, so we should print 1.

Samples2

Sample Input 2

1 2 0 4 5

Sample Output 2

3

Constraints

  • The values of x1,x2,x3,x4, and x5 given as input are a possible outcome of the assignment by Snuke.

题解报告

题目翻译

就是给你 5 个数字,分别为 x1, x2, x3, x4 和 x5。请找出哪个位置的数字值为 0。

题目分析

学过循环就可以完成本题。当然没有学习过循环,只要学习了 if 语句,也可以完成。

非常简单的一个题目。我来凑数罢了。

AC 参考代码

使用 for

#include <bits/stdc++.h>
using namespace std;

int main() {
	int x;
	for (int i=1; i<6; i++) {
		cin>>x;
		if (0==x) {
			cout << i << endl;
			return 0;
		}
	}

	return 0;
}

仅使用 if

#include <bits/stdc++.h>
using namespace std;

int main() {
	int x1,x2,x3,x4,x5;
	cin>>x1>>x2>>x3>>x4>>x5;

	if (0==x1) {
		cout << "1" << endl;
	} else if (0==x2) {
		cout << "2" << endl;
	} else if (0==x3) {
		cout << "3" << endl;
	} else if (0==x4) {
		cout << "4" << endl;
	} else {
		cout << "5" << endl;
	}

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力的老周

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值