九度OJ 1052 找x

題目鏈接:http://ac.jobdu.com/problem.php?pid=1052


題目分析:

將輸入數據存到數組中,設置一個標誌位temp判斷是否找到數據。


源代碼:

#include <iostream>
using namespace std;

int main()
{
	int n;
	while (cin>>n)
	{
		int a[210] = {0};
		int x;
		int temp = -1;
		for (int i = 0; i < n; i ++)
		{
			cin>>a[i];
		}
		cin>>x;
		for (int j = 0; j < n; j ++)
		{
			if (x == a[j])
			{
				temp = j;
				cout<<temp<<endl;
			}
			
		}
		if (temp == -1)
		{
			cout<<"-1"<<endl;
		}

	}

	return 0;
}


發佈了67 篇原創文章 · 獲贊 18 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章