找x
Time Limit:1000MS Memory Limit:65536K
Total Submit:117 Accepted:58
Description
输入一个数n,然后输入n个数值各不相同,再输入一个值x,输出这个值在这个数组中的下标(从0开始,若不在数组中则输出-1)。
Input
测试数据有多组,输入n(1<=n<=200),接着输入n个数,然后输入x。
Output
对于每组输入,请输出结果。
Sample Input
2
1 3
0
Sample Output
-1
Source
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AK1190 {
class Program {
static void Main(string[] args) {
string sb;
while ((sb = Console.ReadLine()) != null) {
int n = int.Parse(sb);
int[] a = new int[n];
string[] s = Console.ReadLine().Split();
for (int i = 0; i < n; i++) a[i] = int.Parse(s[i]);
int x = int.Parse(Console.ReadLine());
bool flag = true;
for(int i=0;i<n;i++)
if (a[i] == x) {
flag = false;
Console.WriteLine(i);
break;
}
if (flag) Console.WriteLine("-1");
}
}
}
}