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. 使用__doPostBack函数来达到使用客户端的控件来调用服务器端的函数的--小结

    类比LinkButton按钮 LinkButton前台生成代码: JS代码: //<![CDATA[ var theForm = document.forms['form1']; if (!th ...

  2. FineUI页面级别的参数配置

    Theme: 控件主题,目前支持三种主题风格(blue/gray/access,默认值:blue) Language: 控件语言(en/zh_CN/zh_TW/...,默认值:zh_CN) FormM ...

  3. Android-第一个Android程序

    Android项目的目录结构及功能 目录 功能 gen 自动生成的文件,不要修改 assets 项目中自定义的需要用到的资源,Android平台不能识别 res 工程资源,Android平台能够识别, ...

  4. validate()的配置项

    1.submitHandler //通过验证成功后运行的函数 代码: $("#mainForm").validate({ ...... rules:{ username:{//此处 ...

  5. swf上传

    swfupload多文件异步上传 多文件选择异步上传的原理 传统上:多个文件逐一选.PHP开始处理,循环上 PHP+Flash上:JS调用flash控,Flash批量选取并保持选取所有文件列 swfu ...

  6. Math对象

    <script type="text/javascript"> /* Math对象常用的方法: ceil 向上取整 floor() 向下取整 random() 随机数方 ...

  7. web登录与授权

    web开发已经流行了很多年,登录与授权也基本有一套通用的流程,下面是我自己常用的登录与授权方式,欢迎大家讨论与吐槽. 概念: 登录是过程,授权是结果.登录只是为了获得页面的访问权限 or 操作权限 o ...

  8. NET调用Java之100-Continue的坑

    场景:这段时间开发的时候遇到了需要NET调用java的restful api的情况,java端用的服务器是tomcat,框架是spring boot,net调用java端的接口之后只要java端的接口 ...

  9. Golang http包下FileServer的使用

    FileServer文档:https://godoc.org/net/http#FileServer 今天看到http的 Handle 方法,所以就像试试,就找到FileServer FileServ ...

  10. MLlib 卡方检验

    1.卡方检验理论 1.1.  简介 总体的分布函数完全未知或只知形式.但不知其参数的情况,为了推断总体的某些未知特性,提出某些关于总体的假设.我们要根据样本对所提出的假设作出是接受,还是拒绝的决策.假 ...