UVA 10474-Where is the Marble?

来源:互联网 发布:php xml 特殊字符转义 编辑:程序博客网 时间:2024/06/05 15:14

UVA 10474-Where is the Marble?

题目大意:先输入 2 个数字,一个是被查找的元素个数,一个是需要查找的元素个数,然后输入元素,如果输入 0 则结束,然后将被查找元素排序后查找

解题思路:先排序,然后找出元素所在位置

#include <stdio.h>#include <iostream>using namespace std;int main() {    int m, n;    int a[100000];    int b;    int all = 0;    while(scanf("%d%d", &m, &n) && m != 0 && n != 0) {        all++;        for(int i = 0; i < m; i++)            scanf("%d", &a[i]);        for(int i = 0; i < m - 1; i++)            for(int j = 0; j < m - i -1; j++)                if(a[j] >= a[j+1]) {                    int t = a[j];                    a[j] = a[j+1];                    a[j+1] = t;                }        printf("CASE# %d:\n", all);        for(int i = 0; i < n; i++) {            scanf("%d", &b);            int x = 0;            for(int j = 0; j < m; j++) {                    x++;                if(a[j] == b) {                    printf("%d found at %d\n", b, x);                    x = -1;                    break;                }            }            if(x != -1)                printf("%d not found\n", b);        }    }    return 0;}
0 0
原创粉丝点击