google在线测试练习题1

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

从文件输入,输出到文件中。代码:

#include<iostream>
#include<fstream>
using namespace std;
void quick_sort(int array[],int indexs[], int begin, int end)
{
    if(end > begin)
    {
        int pivot = begin;
        int last_small = begin;
        int i = end;
        while(last_small != i)
        {
            if(array[i] <= array[pivot])
            {
                int temp = array[i];
				int tmp = indexs[i];
                array[i] = array[++last_small];
                array[last_small] = temp;
				indexs[i] = indexs[last_small];
				indexs[last_small] = tmp;
            }
            else
                i--;
        }
        int tmp = array[pivot];
        array[pivot] = array[last_small];
        array[last_small] = tmp;
		int temp = indexs[pivot];
		indexs[pivot] = indexs[last_small];
		indexs[last_small] = temp;
        quick_sort(array, indexs, begin, last_small - 1);
        quick_sort(array, indexs, last_small + 1, end);
    }
}
int main()
{
	int sum, n;
	int array[2005];
	int n_case;
	ifstream file2("A-large-practice.in");
	ofstream file1("resulta2.txt");
	file2 >> n_case;
	for(int i_case = 1; i_case <= n_case; i_case++)
	{
		file2 >> sum;
		file2 >> n;
		for(int i = 0; i < n; i++)
			file2 >> array[i];
		int indexs[2005];
		for(int i = 0; i < n; i++)
			indexs[i] = i + 1;
		quick_sort(array, indexs, 0, n - 1);//sort the list
		int front = 0;
		int back = n - 1;
		while(true)
		{
			if(array[front] + array[back] == sum)//found
				break;
			else if(array[front] + array[back] > sum)
				back--;
			else front++;
		}
		if(indexs[front] > indexs[back])
		{
			int tmp = indexs[front];
			indexs[front] = indexs[back];
			indexs[back] = tmp;
		}
		file1 << "Case #" << i_case << ": ";
		file1 << indexs[front] << ' ' << indexs[back] << endl;
	}
	system("pause");
	return 0;
}
时间: 2024-08-15 17:18:25

google在线测试练习题1的相关文章

google在线测试练习题3

Problem The Latin alphabet contains 26 characters and telephones only have ten digits on the keypad. We would like to make it easier to write a message to your friend using a sequence of keypresses to indicate the desired characters. The letters are

google在线测试练习题2

Problem Given a list of space separated words, reverse the order of the words. Each line of text contains L letters and W words. A line will only consist of letters and space characters. There will be exactly one space character between each pair of

google在线测试练习2

Problem Given a list of space separated words, reverse the order of the words. Each line of text contains L letters and W words. A line will only consist of letters and space characters. There will be exactly one space character between each pair of

动态规划 Dynamic Programming

March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: 自由转载-非商用-非衍生-保持署名|Creative Commons BY-NC-ND 3.0 ,转载请注明作者及出处. 前言 本文翻译自TopCoder上的一篇文章: Dynamic Programming: From novice to advanced ,并非严格逐字逐句翻译,其中加入了自己的

动态规划:从新手到专家

http://www.hawstein.com/posts/dp-novice-to-advanced.html 前言 本文翻译自TopCoder上的一篇文章: Dynamic Programming: From novice to advanced ,并非严格逐字逐句翻译,其中加入了自己的一些理解.水平有限,还望指摘. 前言_ 我们遇到的问题中,有很大一部分可以用动态规划(简称DP)来解. 解决这类问题可以很大地提升你的能力与技巧,我会试着帮助你理解如何使用DP来解题. 这篇文章是基于实例展开

[转]动态规划

前言_ 我们遇到的问题中,有很大一部分可以用动态规划(简称DP)来解.解决这类问题可以很大地提升你的能力与技巧,我会试着帮助你理解如何使用DP来解题.这篇文章是基于实例展开来讲的,因为干巴巴的理论实在不好理解. 注意:如果你对于其中某一节已经了解并且不想阅读它,没关系,直接跳过它即可. 简介(入门) 什么是动态规划,我们要如何描述它? 动态规划算法通常基于一个递推公式及一个或多个初始状态.当前子问题的解将由上一次子问题的解推出.使用动态规划来解题只需要多项式时间复杂度,因此它比回溯法.暴力法等要

dp新手入门

网址转载链接:  http://bbs.chinaunix.net/thread-4094539-1-1.html 动态规划:从新手到专家 Hawstein翻译 前言 我们遇到的问题中,有很大一部分可以用动态规划(简称DP)来解. 解决这类问题可以很大地提升你的能力与技巧,我会试着帮助你理解如何使用DP来解题. 这篇文章是基于实例展开来讲的,因为干巴巴的理论实在不好理解. 注意:如果你对于其中某一节已经了解并且不想阅读它,没关系,直接跳过它即可. 简介(入门) 什么是动态规划,我们要如何描述它?

动态规划:从新手到专家(转)

动态规划:从新手到专家 March 26, 2013 作者:Hawstein出处:http://hawstein.com/posts/dp-novice-to-advanced.html声明:本文采用以下协议进行授权: 自由转载-非商用-非衍生-保持署名|Creative Commons BY-NC-ND 3.0 ,转载请注明作者及出处. 前言 本文翻译自TopCoder上的一篇文章: Dynamic Programming: From novice to advanced ,并非严格逐字逐句翻

Dynamic programming:from novice to advanced【动态规划】

动态规划:从新手到专家 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: 自由转载-非商用-非衍生-保持署名|Creative Commons BY-NC-ND 3.0 ,转载请注明作者及出处. 前言 本文翻译自TopCoder上的一篇文章: Dynamic Programming: From novice to advanced ,并非严格逐字逐句翻译,其中加入了自己的一些理解.水平有限,还望指摘.