10474 - Where is the Marble?

题目:10474 - Where is the Marble?


题目大意:从小到大排序后找到指定数的位置。


解题思路:用sort()。

#include<stdio.h>
#include<algorithm>
using namespace std;

const int N = 10005;
int t, n, q,  s1[N], s[N];

int cmp(const int a, const int b) {

	return (a < b)? 1: 0;
}
int main () {

	while(scanf("%d %d", &n, &q)) {
			
			int i, j , count ;
			if(n == 0 && q == 0)
				break;
			printf("CASE# %d:\n", ++t);
			for ( i = 0 ; i < n; i++) {
				
				scanf("%d", &s[i]);
			}
			for( i = 0; i < q; i++) {

				scanf("%d", &s1[i]);
			}
			sort(s, s + n, cmp);
			for(i = 0; i < q; i++) {
				count = 0;
				for (j = 0; j < n; j++)  {

					if(s1[i] == s[j]) {

						printf("%d found at %d\n", s1[i] , ++count);
						break;
					}
					else
						count++;
				}
				if(j == n)
					printf("%d not found\n", s1[i]);
			}

	}		
	return 0;
}


你可能感兴趣的:(10474 - Where is the Marble?)