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

最新文章

  1. WebForm(二)——控件和数据库连接方式
  2. javascript数组去重的两个方法
  3. JavaScript分离代码理解
  4. 从veth看虚拟网络设备的qdisc
  5. 我为什么要做富文本编辑器【wangEditor5个月总结】
  6. 读书笔记——Windows核心编程(2)禁止C运行时触发的所有Debug Assertion Failed对话框
  7. codevs 1128 导弹拦截 (贪心)
  8. Java中如何判断当前环境是大端字节顺序还是小端字节顺序
  9. css多行文本居中
  10. &lt;td style=&quot;word-break:break-all&quot;&gt; 在html中控制自动换行
  11. AD进行行PCB DRC检查时,软件提示...report_drc.xsl不存在
  12. MySQL基础语法命令
  13. 英语-TOEFL和GRE复习计划与资料
  14. js 时间格式化 兼容safari 苹果手机
  15. centos7更换镜像源
  16. git常用命令常用场景
  17. Maven内置属性,pom属性
  18. create-react-app脚手架使用
  19. 模拟赛T1 素数
  20. nc命令的常用参数介绍

热门文章

  1. TFS统计编码行数语句
  2. Android实用代码块
  3. Cacti监控Windows主机,Windows主机的正确配置
  4. JS实现Tab切换
  5. Java反射 - 1(得到类对象的几种方法,调用方法,得到包下的所有类)
  6. Android应用开发中关于this.context=context的理解
  7. Oracle Union All 排序
  8. HTML5 初始文档声明
  9. AJAX快速上手
  10. 获取元素位置信息:getBoundingClientRect