美文网首页
UVA 10474 Where is the Marble?

UVA 10474 Where is the Marble?

作者: iamkai | 来源:发表于2016-11-18 17:46 被阅读0次

    Problem

    https://uva.onlinejudge.org/external/104/10474.pdf

    Solution

    #include<iostream>
    #define MAX_NUM 10005 // 最大數字為10000
    using namespace std;
    
    int main()
    {    
        int N,Q,case_num = 0;
    
        //若N和Q為0結束
        while((cin >> N >> Q) && N && Q)
        {
            
            //用陣列計數所有的數字個數
            int count[MAX_NUM] = {0};
            for(int i = 0 ; i < N ; i++)
            {
                int temp;
                cin >> temp;
                count[temp]++;
            }
            
            cout << "CASE# " << ++case_num << ":" << endl;
            for(int i = 0 ; i < Q ; i++)
            {
                int ask_num;
                cin >> ask_num;
                
                int ans = 0;
                //如果數字個數為0個則為not found
                if(count[ask_num] == 0)
                    cout << ask_num << " not found" << endl;
                else
                {
                    //計數到前一個數字的總數
                    for(int j = 0 ; j != ask_num ; j++)
                        ans += count[j];
                    //下一個數字的第一個所以要加1
                    ans++;
                    cout << ask_num << " found at " << ans << endl;
                }
            
            }//end of for i
        
        }//end of while
        return 0;
    }
    
    

    相关文章

      网友评论

          本文标题:UVA 10474 Where is the Marble?

          本文链接:https://www.haomeiwen.com/subject/gwkspttx.html