Code: Select all
ACed
Moderator: Board moderators
Code: Select all
ACed
Code: Select all
(removed)
Code: Select all
4
10 20 20 30
40
3
5 35 20
40
Code: Select all
Peter should buy books whose prices are 20 and 20.
Peter should buy books whose prices are 5 and 35.
Code: Select all
(removed)
Code: Select all
#include <stdio.h>
#define MAX 10001
int main()
{
int i, j, livros, sub, menor, dinheiro, preco[MAX], res[MAX][2], pos, flag = 0, cont, soma;
while (scanf("%d\n", &livros) == 1)
{
for (i = 0; i < livros; i++)
{
scanf("%d", &preco[i]);
}
scanf("%d", &dinheiro);
cont = 0;
for (i = 0; i < livros; i++)
{
for (j = i; j < livros; j++)
{
if (i != j)
{
soma = preco[i] + preco[j];
if (soma == dinheiro)
{
cont++;
if (preco[i] > preco[j])
{
res[cont][0] = preco[i];
res[cont][1] = preco[j];
}
else
{
res[cont][0] = preco[j];
res[cont][1] = preco[i];
}
}
}
}
}
menor = 1000001;
if (cont > 1)
{
for (i = 1; i < (cont + 1); i++)
{
sub = res[i][0] - res[i][1];
if (sub < menor)
{
menor = sub;
pos = i;
}
}
}
else
{
pos = 1;
}
if (flag == 1)
{
printf("\n");
}
else
{
flag = 1;
}
if (res[pos][0] < res[pos][1])
{
printf("Peter should buy books whose prices are %d and %d.\n", res[pos][0], res[pos][1]);
}
else
{
printf("Peter should buy books whose prices are %d and %d.\n", res[pos][1], res[pos][0]);
}
}
return 0;
}
Code: Select all
I've modified the code and got acc
first i thought that
for input
3
1 2 3
4
output will be
peter should bye ... 1 and 3
but i modified the code to have the output
peter should bye ... 2 and 2
and got acc
::: why should peter buy duplicate books?? :::