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:

Post by anupam » Fri Jul 09, 2004 2:59 pm

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

Post by Frostina » 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.. :-?
Please help..

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]
Thanks for your help ! ;)

jamu
New poster
Posts: 13
Joined: Wed Dec 03, 2003 11:15 am

Post by jamu » 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"

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

Post by Guest » Fri Aug 06, 2004 9:28 am

Hello,
I'm getting WA for this problem. :cry: 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
Location: Dhaka, Bangladesh
Contact:

Post by Guest » 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: :cry:

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
Location: Bangladesh
Contact:

Post by Raiyan Kamal » 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.

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

AC

Post by Guest » Mon Aug 09, 2004 3:15 pm

Thank you so much Raiyan, I got AC. :D

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

Post by wanderley2k » 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]) {
/*

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

Post by Guest » 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,

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

Post by wanderley2k » Sun Aug 29, 2004 5:54 pm

Hi,

[quote="

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

Post by Guest » 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]) {
/*

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

Post by wanderley2k » Sun Aug 29, 2004 11:13 pm

[quote="

Rony
New poster
Posts: 16
Joined: Wed Jun 30, 2004 6:46 am
Location: Dhaka
Contact:

Post by Rony » 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. :oops:

Rony
New poster
Posts: 16
Joined: Wed Jun 30, 2004 6:46 am
Location: Dhaka
Contact:

WA Please

Post by Rony » 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. :oops:






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

Post by Antonio Ocampo » Sun Feb 27, 2005 5:07 am

Please help me. I don't know why I got WA

Code: Select all


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

Post Reply

Return to “Volume 104 (10400-10499)”