10487 - Closest Sums

All about problems in Volume 104. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

anupam
A great helper
Posts: 405
Joined: Wed Aug 28, 2002 6:45 pm
Contact:
Oops! Thank you for such input jamu. Correcting the mistakes.
"Everything should be made simple, but not always simpler"

Frostina
New poster
Posts: 23
Joined: Mon Dec 15, 2003 5:21 am
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, k, max, ans, cases = 0,
i, j, m, ques;
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]
Thanks for your help ! jamu
New poster
Posts: 13
Joined: Wed Dec 03, 2003 11:15 am
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"

Guest
New poster
Posts: 39
Joined: Wed May 19, 2004 5:52 pm
Contact:
Hello,
I'm getting WA for this problem. Can anyone give me some sample i/o for this problem?

Thank you,

Guest
New poster
Posts: 39
Joined: Wed May 19, 2004 5:52 pm
Contact:
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,
Last edited by on Mon Aug 09, 2004 3:11 pm, edited 1 time in total.

Raiyan Kamal
Experienced poster
Posts: 106
Joined: Thu Jan 29, 2004 12:07 pm
Contact:
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.

Guest
New poster
Posts: 39
Joined: Wed May 19, 2004 5:52 pm
Contact:

AC

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.

wanderley2k
New poster
Posts: 28
Joined: Mon Mar 01, 2004 11:29 pm

Wrong too

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, &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;

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

Guest
New poster
Posts: 39
Joined: Wed May 19, 2004 5:52 pm
Contact:
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,

wanderley2k
New poster
Posts: 28
Joined: Mon Mar 01, 2004 11:29 pm
Hi,

[quote="

Guest
New poster
Posts: 39
Joined: Wed May 19, 2004 5:52 pm
Contact:
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 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; // Change num=num 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]) {
/*

wanderley2k
New poster
Posts: 28
Joined: Mon Mar 01, 2004 11:29 pm
[quote="

Rony
New poster
Posts: 16
Joined: Wed Jun 30, 2004 6:46 am
Location: Dhaka
Contact:
Hi
Wanderley, Can you please find out my problem. Here is my code

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

int array,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. Rony
New poster
Posts: 16
Joined: Wed Jun 30, 2004 6:46 am
Location: Dhaka
Contact:

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,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,
please help me any one . I am not understing my problem .

Antonio Ocampo
Experienced poster
Posts: 131
Joined: Sat Jul 17, 2004 4:09 am
Location: Lima, Per
Please help me. I don't know why I got WA

Code: Select all

Cut after AC :)
Thx in advance Last edited by Antonio Ocampo on Wed Mar 02, 2005 4:39 am, edited 1 time in total.