UVA 10474

题意:给你一组数,再给几个数问是否在一组数中。

题很简单:STL入门。

没用到STL。

#include<iostream>
#include<cstdio>
#include<algorithm>

using namespace std;
int a[10005];
int main()
{
    int ncase = 1;
    //freopen("in.txt","r",stdin);
    int n,m;
    while(scanf("%d%d",&n,&m) != EOF){
        if(n == 0 && m == 0)
            break;
        printf("CASE# %d:\n",ncase++);
        for(int i = 1;i <= n; i++)
            scanf("%d",&a[i]);
        sort(a+1,a+n+1);
        for(int i = 1;i <= m; i++){
            int ans;
            scanf("%d",&ans);
            int flag = false;
            int j;
            for(j = 1;j <= n; j++)
                if(a[j] == ans){
                    flag = true;
                    break;
                }
            if(flag)
                printf("%d found at %d\n",ans,j);
            else
                printf("%d not found\n",ans);
        }
    }
    return 0;
}

STL。

#include<iostream>
#include<cstdio>
#include<algorithm>

using namespace std;
int a[10005];
int main()
{
    int ncase = 1;
    //freopen("in.txt","r",stdin);
    int n,m;
    while(scanf("%d%d",&n,&m) != EOF){
        if(n == 0 && m == 0)
            break;
        printf("CASE# %d:\n",ncase++);
        for(int i = 1;i <= n; i++)
            scanf("%d",&a[i]);
        sort(a+1,a+n+1);
        for(int i = 1;i <= m; i++){
            int ans;
            scanf("%d",&ans);
            int flag;
    //        for(j = 1;j <= n; j++)
    //            if(a[j] == ans){
    //                flag = true;
    //                break;
    //            }
            flag = lower_bound(a+1,a+n+1,ans) - a;  //很容易看出怎么操作的
            if(a[flag] == ans)
                printf("%d found at %d\n",ans,flag);
            else
                printf("%d not found\n",ans);
        }
    }
    return 0;
}
时间: 2024-12-13 20:08:32

UVA 10474的相关文章

uva 10474 Where is the Marble?(排序)

uva 10474 Where is the Marble? Raju and Meena love to play with Marbles. They have got a lot of marbles with numbers written on them. At the beginning, Raju would place the marbles one after another in ascending order of the numbers written on them.

UVA - 10474 - Where is the Marble? (基数排序)

UVA - 10474 Where is the Marble? Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Raju and Meena love to play with Marbles. They have got a lot of marbles with numbers written on them. At the beginni

UVA 10474:Where is the Marble?(STL初步)

 Where is the Marble?  Raju and Meena love to play with Marbles. They have got a lot of marbles with numbers written on them. At the beginning, Raju would place the marbles one after another in ascending order of the numbers written on them. Then Mee

UVA 10474 - Where is the Marble?

Where is the Marble?  Where is the Marble?  Raju and Meena love to play with Marbles. They have got a lot of marbles with numbers written on them. At the beginning, Raju would place the marbles one after another in ascending order of the numbers writ

大理石在哪里UVa 10474

我自己写的代码 #include<iostream>#include<algorithm>using namespace std;int main(){    int N,a[100],b[100],Q,flag;    int k=1;    while(cin>>N>>Q)    {        for(int i=0;i<N;i++)        {            cin>>a[i];        }        fo

uva 10474 Where is the Marble?(简单题)

我很奇怪为什么要把它归类到回溯上,明明就是简单排序,查找就OK了,wa了两次,我还很不解的怀疑了为什么会 wa,原来是我竟然把要找的数字也排序了,当时只是想着能快一点查找,所以就给他排序了,没考虑到要按给的顺序输 出答案,这次真是二了,,,看别人题解有用打表做的,那个应该是正确解法,我的耗时980ms,估计数据再大一些就 要TLE了 贴代码: #include<stdio.h> #include<string.h> #include<stdlib.h> int cmp(

uva 10474 Where is the Marble?[ vector ]

思路:sort+low_bound()二分 #include<vector> #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<set> using namespace std; int main() { vector<int>v; int n,m; int cas=1; while(scanf("

UVA 10474 STL

题目实在是水题,主要是学习sort以及 lower_bound x为待查找的元素 int p=lower_bound(a,a+n,x)-p;返回a中第一个大于或等于x的元素的位置,使用lower_bound前要将数组进行排序.函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置.如果所有元素都小于val,则返回last的位置,且last的位置是越界的! upper_bound #include<iostream> #includ

UVA 10474 大理石在哪 lower_bound

题意:找输入的数在排完序之后的位置. 主要是lower_bound 函数的使用.它的作用是查找大于或者等于x的第一个位置. #include<cstdio> #include<algorithm> using namespace std; const int maxn = 10000; int main() { int n,q,x,a[maxn],kase = 0; while(scanf("%d%d",&n,&q) == 2 &&