zoukankan      html  css  js  c++  java
  • 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 Meena would ask Raju to find the first marble with a certain number. She would count 1...2...3. Raju gets one point for correct answer, and Meena gets the point if Raju fails. After some fixed number of trials the game ends and the player with maximum points wins. Today it’s your chance to play as Raju. Being the smart kid, you’d be taking the favor of a computer. But don’t underestimate Meena, she had written a program to keep track how much time you’re taking to give all the answers. So now you have to write a program, which will help you in your role as Raju.
    (输入)Input
    There can be multiple test cases. Total no of test cases is less than 65. Each test case consists begins with 2 integers: N the number of marbles and Q the number of queries Mina would make. The next N lines would contain the numbers written on the N marbles. These marble numbers will not come in any particular order. Following Q lines will have Q queries. Be assured, none of the input numbers are greater than 10000 and none of them are negative. Input is terminated by a test case where N = 0 and Q = 0.
    (输出)Output
    For each test case output the serial number of the case. For each of the queries, print one line of output. The format of this line will depend upon whether or not the query number is written upon any of the marbles. The two different formats are described below: • ‘x found at y’, if the first marble with number x was found at position y. Positions are numbered 1,2,...,N. • ‘x not found’, if the marble with number x is not present. Look at the output for sample input for details.
    (样例输入)Sample Input
    4 1

    2

    3

    5

    1

    5

    5 2

    1

    3

    3

    3

    1

    2

    3

    0 0

    (样例输出)ample Output
    CASE# 1: 5 found at 4

    CASE# 2: 2 not found

    3 found at 3

    简要写明一下题目意思:

    先输入n和m两个数字,n为大理石的个数,每块大理石上都有一个数字,在下面依次输入这n个数;m代表你接下来要在这些大理石上寻找m个数字,在最后依次输入你要找的这m个数,如果找到了就要输出那个数在大理石上排第几(按从小到大),如果没有找到就输出not found.

    我做的时候有参考CSDN上大神的代码啦(忘了是谁)

    根据题目呢,这题要排序,于是用sort函数(好像sort函数只能对数组里的数字进行排序的),老样子,在主函数前加上bool cmp便于下面sort排序。

    (注意多组输入)先实现n和m,及n个数和m个数的输入。然后因为上面(用蓝色背景标注)的要求,肯定要对大理石上的数字进行排序,于是大理石上的数字用数组a[1000]输入,然后sort(a,a+n,cmp)对大理石排好序了(但1a数组仍然从a[0]开始,但a[0]的值已经不是原来那个了,而是n个数中最小的那个了。接着用循环实现m个数的输入。然后开始找数字,在n个数里找你输入的m个数。用循环for(i=0;i<m;i++) [因为找m个数就行了,所以<m],这时候就用一个新的函数lower_bound,简单写一下这个函数的用处:

    lower_bound:【注意待查找数组必须已经排好序!得到的是第一个大于等于表达式3的数组下标!】

    迁移到这道题,已经排好序的数组是a[1000],对a数组使用这个函数:pos=lower_bound(a,a+n,k[i])-a,于是就找到了第一个大于等于k[i](需要的数字)在大理石上的数字(这个第一个数字最小也等于所需数字k[i],如果不等于,说明大理石上没有你要找的数字)数组下标,然后用if判断所找的k[i]是否等于大理石数组上的a[pos],如果等于就可以输出is found那个,否则就输出not found那个。

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    bool cmp(int a,int b)
    {
        return a<b;
    }
    int s[11000];
    int main()
    {
        int a,b,c=0;
        while(scanf("%d%d",&a,&b)!=EOF)
        {
            if((a==0)&&(b==0))
                break;
            else
            {
                int i,k[11000];
                for(i=0; i<a; i++)
                {
                    scanf("%d",&s[i]);/*依次a块大理石上的数字s[i]*/

                }

                sort(s,s+a,cmp);/*对a块大理石上的数字进行由小到大的排序*/

                for(i=0;i<b;i++)
                {
                    scanf("%d",&k[i]);/*依次输入需要在大理石上寻找的数字k[i]*/
                }
                 printf("CASE# %d:\n",++c);
                for(i=0;i<b;i++)
                {
                    int pos=lower_bound(s,s+a,k[i])-s;/*在排好序的大理石上寻找第一个大于等于k[i]的数字,pos为s数组的下标*/
                    if(s[pos]==k[i])
                    {
                        printf("%d found at %d\n",k[i],pos+1);
                    }
                    else
                    {
                        printf("%d not found\n",k[i]);
                    }
                }
            }


        }

        return 0;
    }

  • 相关阅读:
    python实战之爬取喜玛拉雅专辑信息
    python工具之exccel模板生成报表
    python模拟登录博客园(附:问题求教)
    maven 三个基本插件 clean dependency compiler
    oracle 安装注意
    mybatis generate 自动生成 entity dao 和 xml 文件
    mybatis 打印sql 语句
    mybatis 关联查询 association
    oracle 多级菜单查询 。start with connect by prior
    mybatis 控制台打印sql
  • 原文地址:https://www.cnblogs.com/programming123/p/10467309.html
Copyright © 2011-2022 走看看