Page 3 of 6
Posted: Fri Jul 09, 2004 2:59 pm
Oops! Thank you for such input jamu. Correcting the mistakes.

Posted: Sat Jul 10, 2004 2:41 pm
I tested the data above, my answers were the same as yours.
But why I still got WA..

I just test all a+b when a!=b

[c]#include <stdio.h>
#include <limits.h>
#include <math.h>
int main(void) {
int n, list[1000], k, max, ans, cases = 0,
i, j, m, ques[25];
while (scanf("%d", &n)==1) {
if (!n) break;
for (i=0;i<n;i++)
scanf("%d", &list);
scanf("%d", &m);
for (i=0;i<m;i++)
scanf("%d", &ques);
printf("Case %d:\n", ++cases);
for (k=0;k<m;k++) {
for (i=0,max=INT_MAX;i<n;i++)
for (j=i+1;j<n;j++)
if (abs(list+list[j]-ques[k])<max&&list!=list[j]) {
max = abs(list+list[j]-ques[k]);
ans = list+list[j];
}
printf("Closest sum to %d is %d\n", ques[k], ans);
}
}
return 0;
} [/c]

Posted: Sat Jul 10, 2004 7:48 pm
I just test all a+b when a!=b
why a!=b ?? test all a+b when a and b are in different lines (but this is not very fast solution)

for this input data:
2
4
4
1
8
0
output should be:
Case 1:
Closest sum to 8 is 8.
while yours is:
Case 1:
Closest sum to 8 is 0
because in this case you never execute the line that changes variable 'ans'

and remember to add a full stop at the end of "Closest sum to %d is %d"

Posted: Fri Aug 06, 2004 9:28 am
Hello,
I'm getting WA for this problem. Can anyone give me some sample i/o for this problem?

Thank you,

Posted: Fri Aug 06, 2004 9:33 am
Hello,
My program seems to give correct answer for all the sample i/o I could find. But I'm still getting WA. Someone please help me find the mistake. Here is my code:

Code: Select all

``Code removed. 09.08.2004``
Thank you,

Posted: Mon Aug 09, 2004 2:02 pm
Dear Turjo, hope this helps you. The output is generated by my AC program.

input:

5
2
2
3
4
1
1
5
3
2
2
2
1
4
5
1
2
3
4
5
1
6
4
4
4
4
1
1
5
0

Output:

Case 1:
Closest sum to 5 is 5.
Case 2:
Closest sum to 4 is 0.
Case 3:
Closest sum to 6 is 6.
Case 4:
Closest sum to 5 is 5.

### AC

Posted: Mon Aug 09, 2004 3:15 pm
Thank you so much Raiyan, I got AC.

But, for the following case,

3 2 2 2 (Numbers)
1 4 (Query)
Your output is: Closest sum to 4 is 0.
Mine is: Closest sum to 4 is 4.

Which means, There are no repeated numbers in the judge input.
Anyway, thank you again.

### Wrong too

Posted: Sun Aug 29, 2004 5:20 pm
Hi,

I tried to solve this problem but I got wrong. Why?? I don't know.

My code is here:

[cpp]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <algorithm>
using namespace std;

const int INF = INT_MAX;

#define MAX_NUM 1000000

int nums[MAX_NUM];

int main() {

int k, i, j, n, m, resp, s, diff;
int inst = 1;
int num;

while(true) {
scanf("%d", &n);
if (n == 0) {
break;
}

printf("Case %d:\n", inst++);

for (i = 0; i < n; i++) {
scanf("%d", &j);
nums = j;
}

sort(&nums[0], &nums[n]);

scanf("%d", &m);
for (k = 0; k < m; k++) {
scanf("%d", &resp);
/*if (resp == INF || resp == -INF) {
num = 0;
}
else {
num = INF;
}*/
num = nums[1];

for (i = 0; i < n; i++) {
for (j = i+1; j < n; j++) {
//if (nums != nums[j]) {
/*

Posted: Sun Aug 29, 2004 5:38 pm
Hi wanderley2k,

You'll find some sample i/o here. Check your program with these.

http://online-judge.uva.es/board/viewto ... c&start=15

regards,

Posted: Sun Aug 29, 2004 5:54 pm
Hi,

[quote="

Posted: Sun Aug 29, 2004 7:57 pm
Dear Wanderley,
Your code doesn't produce correct output for the sample input given in the problemset. Maybe, this is the reason: You should initialize num with a big number, but you're doing num=num[1] for the first time when you're entering the k-loop. So change it to num=any big number.

[cpp]for (k = 0; k < m; k++) {
scanf("%d", &resp);
/*if (resp == INF || resp == -INF) {
num = 0;
}
else {
num = INF;
}*/
num = num[1]; // Change num=num[1] to num=2<<30 or any big number;

for (i = 0; i < n; i++) {
for (j = i+1; j < n; j++) {
//if (nums != nums[j]) {
/*

Posted: Sun Aug 29, 2004 11:13 pm
[quote="

Posted: Fri Nov 05, 2004 7:38 am
Hi
Wanderley, Can you please find out my problem. Here is my code

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

int array[105],query;
int n,i,m,sum,j,d;
long long t_num;

int compare(const void *a,const void *b);

int main(){

int tcase;
tcase=1;

while(1){
scanf("%d",&n);
if(!n) break;

for(i=0;i<n;i++) scanf("%d",&array);
qsort(array,n,sizeof(int),compare);

scanf("%d",&m);
printf("Case %d:\n",tcase++);
for(d=0;d<m;d++){
scanf("%d",&query);
t_num=1>>31;

for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(array!=array[j]) /* sum of two distinct numbers */
{
sum=array+array[j];
if(sum<t_num)
t_num=sum;
}

}

}
printf("Closest sum to %d is %lld.\n",query,t_num);

}

}

return(0);
}

int compare(const void *a,const void *b) {
int *x = (int *) a;
int *y = (int *) b;
return *x - *y;
}

Regards

Rony.

Posted: Mon Nov 08, 2004 11:19 am
Rony wrote:Hi
Wanderley, Can you please find out my problem. Here is my code

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

int array[105],query;
int n,i,m,sum,j,d;
long long t_num;

int compare(const void *a,const void *b);

int main(){

int tcase;
tcase=1;

while(1){
scanf("%d",&n);
if(!n) break;

for(i=0;i<n;i++) scanf("%d",&array);
qsort(array,n,sizeof(int),compare);

scanf("%d",&m);
printf("Case %d:\n",tcase++);
for(d=0;d<m;d++){
scanf("%d",&query);
t_num=1>>31;

for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(array!=array[j]) /* sum of two distinct numbers */
{
sum=array+array[j];
if(sum<t_num)
t_num=sum;
}

}

}
printf("Closest sum to %d is %lld.\n",query,t_num);

}

}

return(0);
}

int compare(const void *a,const void *b) {
int *x = (int *) a;
int *y = (int *) b;
return *x - *y;
}

Regards

Rony.

Hi,
``````