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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章