zoukankan      html  css  js  c++  java
  • [C++]Store Credit——Google Code Jam Qualification Round Africa 2010

    Google Code Jam Qualification Round Africa 2010 的第一题,很简单。

    Problem

    You receive a credit C at a local store and would like to buy two items. You first walk through the store and create a list L of all available items. From this list you would like to buy two items that add up to the entire value of the credit. The solution you provide will consist of the two integers indicating the positions of the items in your list (smaller number first).

    Input

    The first line of input gives the number of cases, NN test cases follow. For each test case there will be:

    • One line containing the value C, the amount of credit you have at the store.
    • One line containing the value I, the number of items in the store.
    • One line containing a space separated list of I integers. Each integer P indicates the price of an item in the store.
    • Each test case will have exactly one solution.

    Output

    For each test case, output one line containing "Case #x: " followed by the indices of the two items whose price adds up to the store credit. The lower index should be output first.

    Limits

    5 ≤ C ≤ 1000
    1 ≤ P ≤ 1000

    Small dataset

    N = 10
    3 ≤ I ≤ 100

    Large dataset

    N = 50
    3 ≤ I ≤ 2000

    Sample


    Input 

    Output 
    3
    100
    3
    5 75 25
    200
    7
    150 24 79 50 88 345 3
    8
    8
    2 1 9 4 4 56 90 3
    Case #1: 2 3
    Case #2: 1 4
    Case #3: 4 5

    就是找出和正好等于credit的两件商品,套用两个循环就可以解决问题。


    #include<iostream>
    #include<fstream>
    #include<vector>
    
    using namespace std;
    
    int main(){
    	ifstream in("A-large-practice.in");
    	ofstream out("A-large-practice.out");
    	if (!in){
    		out << "Open in failde!" << endl;
    	}
    	int N;
    	in >> N;
    	for (int i = 0; i < N; i++){
    		int credit;
    		in >> credit;
    		int list_size;
    		in >> list_size;
    		vector<int> shop_list;
    		for (int j = 0; j < list_size; j++){
    			int value;
    			in >>value;
    			shop_list.push_back(value);
    		}
    		for (int j = 0; j < shop_list.size(); j++){
    			for (int k = j + 1; k < shop_list.size(); k++){
    				if (shop_list[j] + shop_list[k] == credit){
    					out << "Case #" << i + 1 << ": " << j + 1<<" "<< k + 1 << endl;
    					j = shop_list.size();
    					break;
    				}
    			}
    		}
    	}
    	return 0;
    }


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    explode — 使用一个字符串分割另一个字符串
    echo — 输出一个或多个字符串
    count_chars — 返回字符串所用字符的信息
    convert_uuencode — 使用 uuencode 编码一个字符串
    convert_uudecode — 解码一个 uuencode 编码的字符串
    convert_cyr_string — 将字符由一种 Cyrillic 字符转换成另一种
    chunk_split — 将字符串分割成小块
    chr — 返回指定的字符
    addslashes — 使用反斜线引用字符串
    addcslashes — 以 C 语言风格使用反斜线转义字符串中的字符
  • 原文地址:https://www.cnblogs.com/xiaff/p/4856507.html
Copyright © 2011-2022 走看看