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
Sample
这对英语不好的人简直就是虐待。
题意:我也没怎么看懂,半猜半做,大概是有p块大洋,想去商店买两样东西,使钱正好花光。商店有I样东西,保证存在输出,求出最小的他们的坐标。
#include <iostream>
using namespace std;
int main()
{
freopen("A-small-practice.in","r",stdin);
freopen("output.out","w",stdout);
int a[5010];
int n,i,j,p,m,f;
int t=1;
scanf("%d",&n);
while (n--)
{
f=1;
scanf("%d",&p);
scanf("%d",&m);
for (i=1;i<=m;i++)
scanf("%d",&a[i]);
for (i=1;i<m;i++)
if (f)
for (j=i+1;j<=m;j++)
if (a[i]+a[j]==p)
{
f=0;
printf("Case #%d: %d %d\n",t++,i,j);
break;
}
}
return 0;
}
本文探讨了如何通过算法高效地解决购物时寻找总价等于信用额度的商品组合的问题,包括输入处理、解决方案查找以及输出格式规范。
6025

被折叠的 条评论
为什么被折叠?



