## 10783 - Odd Sum

Moderator: Board moderators

Mamet
New poster
Posts: 7
Joined: Tue Mar 14, 2006 12:59 am

### 10783 - Odd Sum

I am trying to solve this so easy problem but I keep getting wrong answer. plz can someone tell me why?? Thx!

Code: Select all

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

int main()
{
long t, a,b,c=0,r;
scanf("%d",&t);
while (t>0){
c++;
scanf("%d", &a);
scanf("%d", &b);
/*r=(1+(b-a)/2)*((a+b)/2);*/
r=0;
while (a<=b){
r+=a;
a+=2;
}
printf("Case %d: %d\n", c, r);
t--;
}

return 0;
}
``````

kryptolus
New poster
Posts: 16
Joined: Sun Mar 19, 2006 9:36 am
Location: New York
Try entering an even number for a... You shouldn't limit yourself to the testcases in the problem.

Mamet
New poster
Posts: 7
Joined: Tue Mar 14, 2006 12:59 am
Oh thx sorry I read wrong the problem. I didn't look i was looking only for the odd terms . thx a lot. I got AC now.

toni
New poster
Posts: 7
Joined: Wed Jul 25, 2007 6:03 pm

Code: Select all

``````
code removed
``````
hello everybody , i have no idea about why this code got WA.
I use the formula to solve the question.
I have try many case but cannot figured out why?
can any one help me ?
thanks Last edited by toni on Tue Aug 21, 2007 6:02 pm, edited 1 time in total.

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:
toni wrote:I have try many case but cannot figured out why?
Oh really?
It doesn't even pass the sample input/output. Here's a hint: your program's output is compared byte-wise with the correct output, and even a single mismatch (e.g. in case numbering) will lead to a WA (or PE) verdict.

toni
New poster
Posts: 7
Joined: Wed Jul 25, 2007 6:03 pm
toni wrote:

Code: Select all

``````/*
code remove.
``````
hello everybody , i have no idea about why this code got WA.
I use the formula to solve the question.
I have try many case but cannot figured out why?
can any one help me ?
thanks i fell really embarrassed, thanks mf so much i didn't take notice that i have changed my code.

WAandAC
New poster
Posts: 4
Joined: Sat Nov 03, 2007 4:32 am

### Why WA

Hi,

I know this is an easy problem, but why I get WA?

I tested the even number case, but is there anything wrong with output or tricky test cases?

Code: Select all

``````#include <iostream>
#include <string>

int calc (int a, int b)
{
if ( a == b && a%2 == 0 && b % 2 == 0)
{
return 0;
}
int low = a;
int high = b;

if (a > b)
{
low = b;
high = a;
}

if ( 0 == low % 2)
{
low++;
}

if ( 0 == high % 2)
{
high--;
}

return (high + low) / 2 * (((high - low)/2) + 1);

}

int main ()
{
int caseNum = 0;
int first = 0;
int second = 0;
while (true)
{
std::cin >> caseNum;
for (int i = 0; i < caseNum; i++)
{
std::cin >> first >> second;
if ( i == caseNum - 1)
{
std::cout <<"Case " << i+1 << ": "  << calc (first, second);
}else{
std::cout <<"Case " << i+1 << ": "  << calc (first, second) <<std::endl;
}

}
if (std::cin.eof())
{
break;
}
}
return 0;
}

``````
Thank you.

S.M.ferdous
New poster
Posts: 13
Joined: Fri Nov 03, 2006 2:53 pm
Contact:
It may be late.
std::cout <<"Case " << i+1 << ": " << calc (first, second)<<endl;

put an endline here also.

S.M.Ferdous

theharshest
New poster
Posts: 20
Joined: Thu Jan 17, 2008 10:47 pm
Location: India

### WA

getting WA for this code--- #include<iostream>

using namespace std;

int sum(int,int);
int main()
{
int n,i,k=0,s,x,y,j;

while(cin>>n)
{
k++;
for(i=0;i<n;i++)
{
cin>>x>>y;
s=sum(x,y);
cout<<"Case "<<k<<": "<<s<<endl;
}
}

}

int sum(int x, int y)
{
int dif=y-x;
int t=0;

if(x==y)
{
if(x%2==0)
return 0;
else
return x;
}
if(x%2==0 && y%2==0)
{
return ((y-x)/2+1)*(x+1) + ((y-x)/2)*(((y-x)/2)-1);
}
if(x%2==0 && y%2!=0)
{
return ((y-x)/2+1)*(x+1) + (((y-x)/2)+1)*(y-x)/2;
}
if(x%2!=0 && y%2!=0)
{
return ((y-x)/2+1)*(x) + (((y-x)/2)+1)*(y-x)/2;
}
if(x%2!=0 && y%2==0)
{
return ((y-x)/2+1)*(x) + (((y-x)/2)+1)*(y-x)/2;
}

}

plz help.....
tx in anticipation...
"if u r goin thru hell, keep goin"

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

### Re: WA

theharshest wrote:getting WA for this code---
There's sample input/output in the problem statement, and your code doesn't pass it. (one byte is wrong)
So what other outcome could you have possibly expected?

And please, use phpbb's [ code] tags for code and proper English spelling next time when you ask for help.

theharshest
New poster
Posts: 20
Joined: Thu Jan 17, 2008 10:47 pm
Location: India

### @mf

changed the code....

but still getting WA Code: Select all

``````deleted after AC
``````
Last edited by theharshest on Thu Feb 21, 2008 11:10 pm, edited 2 times in total.
"if u r goin thru hell, keep goin"

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:
Input:

Code: Select all

``````4
1 100
2 100
1 99
2 99
``````
The correct output for this input is:

Code: Select all

``````Case 1: 2500
Case 2: 2499
Case 3: 2500
Case 4: 2499
``````
You don't need any complicated formulas for this problem. Keep it simple - a dumb one-line for-loop is all that you need for this problem.

theharshest
New poster
Posts: 20
Joined: Thu Jan 17, 2008 10:47 pm
Location: India

### @mf

thanks...
finally got AC.... "if u r goin thru hell, keep goin"

wayne_911
New poster
Posts: 4
Joined: Thu Jun 25, 2009 10:02 am

### Always get WA...Help comment on code pls

I am a newbie to uva judge online website..I am still not very sure how the format of the code should be..So far, i seem get reply none other than WA.... So i try on this simple question...which also give me WA as well. Can anyone pls comment on my code? Thx..

Code: Select all

``````#include <iostream>
using namespace std;

int main()
{
int a,b;
int total=0;

cout<<"input: ";
cin>>a;
cin>>b;

for(int i=a; i<=b; i++)
if(i%2)
total+=i;

cout<<"output: "<<total<<endl;

return 0;
}``````

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

### Re: 10783 - Odd Sum

There are just three simple rules:

1) Follow the output format specification exactly, don't output anything extra, like "input: " or "output: ". Judge is only a dumb robot who compares output files byte-wise, and those words don't make much sense to it.

Instead of "output: ", you're supposed to print "Case 1: ", "Case 2: ", etc. in this particular problem.

2) Follow the input format specification exactly.
The first line of input gives you the number of test cases, T.
Then T test cases follow. Each test case consists of 2 integers a and b in two separate lines.
Your program must process all the test cases present in the input.

3) Solve the problem correctly, within its time and memory limits. Your program looks good here.