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, N. N 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的更多相关文章
- google在线測试练习题3
Problem The Latin alphabet contains 26 characters and telephones only have ten digits on the keypad. ...
- go五笔——基于Google在线五笔制作
go五笔 v0.0.2 加入新世纪版 86版收录几个不常用汉字,其它无更新 下载 86版64位密码: qe7k 86版32位密码: y25a 06版64位密码: d2ug 06版32位密码: bxxz ...
- [华为机试练习题]55.最大公约数 & 多个数的最大公约数
题目 描写叙述: 输入2个数字,最后输出2个数字的最大公约数 题目类别: 位运算 难度: 0基础 执行时间限制: 无限制 内存限制: 无限制 阶段: 入职前练习 输入: 2个整数 输出: 输出数字1和 ...
- [华为机试练习题]50.求M的N次方的最后三位
题目 描写叙述: 正整数M 的N次方有可能是一个很大的数字,我们仅仅求该数字的最后三位 例1: 比方输入5和3 ,5的3次方为125.则输出为125 例2: 比方输入2和10 2的10次方为1024 ...
- 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 ...
- Google Code Jam在线測试题目--Alien Language
Problem After years of study, scientists at Google Labs have discovered an alien language transmitte ...
- google校招在线測试题---2048
先附代码:(简单地说就是给出一个矩阵代表2048游戏的一个状态以及一个方向,输出往这个方向移动之后的矩阵) #include<iostream> #include<fstream&g ...
- google 在线代理浏览
谷歌访问不了,你又N多方法,比如搭建VPN,买VPN,或查找google多个IP访问, 或通过第三方反代理网站访问, 或通过客户端代理(类似goagent)等 下面罗列出可以访问google的几个代理 ...
- 免费APP在线測试工具以及其用法
免费APP漏洞安全检測工具:http://safe.ijiami.cn/ 漏洞分析是爱加密推出免费 APP 漏洞分析平台,服务包含一键对APK 进行签名数据信息採集.内部配置信息採集.市场渠道相关信息 ...
随机推荐
- css过渡+3D
<!DOCTYPE html><html><head> <title>guodu</title> <meta charset=&quo ...
- asp.net微信开发第六篇----高级群发(文本)
说到高级群发,微信的参考资料http://mp.weixin.qq.com/wiki/14/0c53fac3bdec3906aaa36987b91d64ea.html 首先我们先来讲解一下群发文本信息 ...
- OpenXml2.0 - 找不到类型或命名空间名称“DocumentFormat”
在使用 OpenXml SDK2.0的过程中,很是郁闷的是总是报 '找不到类型或命名空间名称“SpreadsheetDocument”(是否缺少 using 指令或程序集引用?)'的错误,命名已经添加 ...
- “The SQL Server license agreenment cannot be located for the selected edition.”MSSQL安装问题
今天老邹又来吐槽了.今天不和IE7较劲了.说点别的吧. 我呢什么软件都喜欢装最新版的,这部刚出来windows 8.1就赶紧装上了,随后就用上了vs2013.前天看到新闻说微软已经发布了sql ser ...
- HTML5 microdata
schema.org 测试地址 http://www.google.com/webmasters/tools/richsnippets
- msi软件包无法安装
安装某些msi软件包,提示“This advertised application will not be installed because it might be unsafe. Contact ...
- php 异常捕获
1 首先是try,catch <?php $path = "D:\\in.txt"; try //检测异常 { file_open($path); } catch(Excep ...
- How to install ffmpeg,mp4box,mplayer,mencoder,flvtool2,ffmpeg-php on centos
1. Enable RPM Fusion yum repository The CentOS rpm packages of ffmpeg, mplayer, mencoder and MP4Box ...
- SCM管理器
OpenSCManager 打开SCM管理器 CloseServiceHandle 关闭句柄 CreateService 创建服务 OpenService 打开服务 ControlService 控制 ...
- centos6.4虚拟机vmware-tools安装及启动到进度条卡死
vmware-tools安装: linux-VMware tools安装步骤: (1)在CD-ROM虚拟光驱中选择使用ISO镜像,找到VMWARE TOOLS 安装文件,如D:\VMware\VMwa ...