10487 - Closest Sums
Moderator: Board moderators
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]
But why I still got WA..
![:-?](./images/smilies/icon_confused.gif)
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 ! ![;)](./images/smilies/icon_wink.gif)
![;)](./images/smilies/icon_wink.gif)
why a!=b ?? test all a+b when a and b are in different lines (but this is not very fast solution)I just test all a+b when a!=b
for this input data:
output should be:2
4
4
1
8
0
while yours is:Case 1:
Closest sum to 8 is 8.
because in this case you never execute the line that changes variable 'ans'Case 1:
Closest sum to 8 is 0
and remember to add a full stop at the end of "Closest sum to %d is %d"
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:
Thank you,
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:](./images/smilies/icon_cry.gif)
Code: Select all
Code removed. 09.08.2004
-
- Experienced poster
- Posts: 106
- Joined: Thu Jan 29, 2004 12:07 pm
- Location: Bangladesh
- 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.
![:D](./images/smilies/icon_biggrin.gif)
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.
-
- 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[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]) {
/*
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]) {
/*
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,
You'll find some sample i/o here. Check your program with these.
http://online-judge.uva.es/board/viewto ... c&start=15
regards,
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]) {
/*
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]) {
/*
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:](./images/smilies/icon_redface.gif)
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:](./images/smilies/icon_redface.gif)
WA Please
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,
please help me any one . I am not understing my problem .
-
- Experienced poster
- Posts: 131
- Joined: Sat Jul 17, 2004 4:09 am
- Location: Lima, Per
Last edited by Antonio Ocampo on Wed Mar 02, 2005 4:39 am, edited 1 time in total.