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;
}

google在线測试练习题1的更多相关文章

  1. google在线測试练习题3

    Problem The Latin alphabet contains 26 characters and telephones only have ten digits on the keypad. ...

  2. go五笔——基于Google在线五笔制作

    go五笔 v0.0.2 加入新世纪版 86版收录几个不常用汉字,其它无更新 下载 86版64位密码: qe7k 86版32位密码: y25a 06版64位密码: d2ug 06版32位密码: bxxz ...

  3. [华为机试练习题]55.最大公约数 &amp; 多个数的最大公约数

    题目 描写叙述: 输入2个数字,最后输出2个数字的最大公约数 题目类别: 位运算 难度: 0基础 执行时间限制: 无限制 内存限制: 无限制 阶段: 入职前练习 输入: 2个整数 输出: 输出数字1和 ...

  4. [华为机试练习题]50.求M的N次方的最后三位

    题目 描写叙述: 正整数M 的N次方有可能是一个很大的数字,我们仅仅求该数字的最后三位 例1: 比方输入5和3 ,5的3次方为125.则输出为125 例2: 比方输入2和10 2的10次方为1024 ...

  5. jsfiddle在线測试Html、CSS、JavaScript——http://jsfiddle.net/

    jsfiddle在线測试Html.CSS.JavaScript,并展示測试结果 1.选择jQuery1.9.1 2.选择jQuery UI 1.9.2 3.Html <ul id="n ...

  6. Google Code Jam在线測试题目--Alien Language

    Problem After years of study, scientists at Google Labs have discovered an alien language transmitte ...

  7. google校招在线測试题---2048

    先附代码:(简单地说就是给出一个矩阵代表2048游戏的一个状态以及一个方向,输出往这个方向移动之后的矩阵) #include<iostream> #include<fstream&g ...

  8. google 在线代理浏览

    谷歌访问不了,你又N多方法,比如搭建VPN,买VPN,或查找google多个IP访问, 或通过第三方反代理网站访问, 或通过客户端代理(类似goagent)等 下面罗列出可以访问google的几个代理 ...

  9. 免费APP在线測试工具以及其用法

    免费APP漏洞安全检測工具:http://safe.ijiami.cn/ 漏洞分析是爱加密推出免费 APP 漏洞分析平台,服务包含一键对APK 进行签名数据信息採集.内部配置信息採集.市场渠道相关信息 ...

随机推荐

  1. SQL Server 2008 R2 的版本和组件

    SQL Server 2008 R2 的版本和组件 SQL Server 2008 R2   其他版本 SQL Server 2008 SQL Server 2005 SQL Server 2012 ...

  2. FAL[client]: Failed to request gap sequence GAP - thread 1 sequence 29-29

    一:问题描述 主备库所在机器都重启后,rac(主库)开机自动启动,但备库(单实例)不是开机启动,现已手工启动到mount,并已在备库上alter database recover  standby d ...

  3. java事件处理4(焦点,键盘

    FocusEvent焦点事件 接口 addFocusListener(FocusListener listener) 有两个方法 public void focusGains(FocusEvent e ...

  4. Android的消息处理机制(Looper,Handler,Message)(转)

    Handler Handler的定义: 主要接收子线程发送的数据,并用此数据配合主线程更新UI. 当应用程序启动时,Android首先会开启一个主线程(也就是UI线程),主线程为管理界面中的UI空间进 ...

  5. PHPCMSv9 更改后台地址(测试)

    最新发布的PHPCMS V9由于采用了MVC的设计模式,所以它的后台访问地址是固定的,虽然可以通过修改路由配置文件来实现修改,但每次都修改路由配置文件对于我来说有点麻烦了,而且一不小心就会出错.这里使 ...

  6. php 魔术方法 __autoload()

    PHP在魔术函数__autoload()方法出现以前,如果你要在一个程序文件中实例化100个对象,那么你必须用include或者require包含进来100个类文件,或者你把这100个类定义在同一个类 ...

  7. Python Socket Programming

    本文介绍使用Python进行Socket网络编程,假设读者已经具备了基本的网络编程知识和Python的基本语法知识,本文中的代码如果没有说明则都是运行在Python 3.4下. Python的sock ...

  8. 大整数算法[11] Karatsuba乘法

    ★ 引子         前面两篇介绍了 Comba 乘法,最后提到当输入的规模很大时,所需的计算时间会急剧增长,因为 Comba 乘法的时间复杂度仍然是 O(n^2).想要打破乘法中 O(n^2) ...

  9. 复杂事件处理引擎—Esper参考(事件部分)

    声明:Esper官方未提供中文文档,以后更新的大部分内容,均来自官方文档.本人英语小白一枚,翻译内容仅供参考.有些翻译确实不忍直视,君可略过. (有人可能会说,翻译的不好不如不翻,可能会误人子弟:不过 ...

  10. IE中对于stylesheet的个数限制

    对于IE6-9: 1.样式规则最多只能有4095个,多于这个数目的会被忽略: 2.样式表(通过@import, <link> 或 <style>)最多可以有31个(总和),多于 ...