Page 3 of 10
10424 Love Calculator WA
Posted: Wed Feb 23, 2005 12:57 pm
by RustB
Code: Select all
#include <stdio.h>
int firstsum(char *inp)
{
int i=0,sum=0;
while(inp[i])
{
if(inp[i]>='a' && inp[i] <='z')
sum+=inp[i]-'a'+1;
else if(inp[i]>='A' && inp[i] <='Z')
sum+=inp[i]-'A'+1;
i+=1;
}
return sum;
}
int sumnum(int num)
{
int sum=0;
while(num)
{
sum+=num%10;
num/=10;
}
return sum;
}
int main()
{
char x[30],y[30];
int sumx,sumy;
float ratio;
while(!feof(stdin))
{
gets(x);
sumx=firstsum(x);
while(sumx>=10)
sumx=sumnum(sumx);
gets(y);
sumy=firstsum(y);
while(sumy>=10)
sumy=sumnum(sumy);
if(sumx > 0 && sumy > 0)
{
if(sumx>sumy)
ratio = ((float)sumy/(float)sumx)*100.0f;
else
ratio = ((float)sumx/(float)sumy)*100.0f;
}
else if(sumx==0 && sumy == 0)
{
printf("\n");
continue;
}
else
{
printf("0.00 %%\n");
continue;
}
printf("%.2f %%\n",ratio);
}
return 0;
}
I have checked it with every input I can find and it gives the right answer.
-I print blank line if both sums are 0
-I print upto 2 decimal points accuracy
Are there any special inputs?
Posted: Wed Feb 23, 2005 3:09 pm
by sumankar
Hi,
What happens when one of the sums is non-zero and the other is zero?
One more thing, I think that sum of digits as you have implemented can be
simplified by taking it modulo-9.
Regards,
Suman.
Posted: Thu Feb 24, 2005 5:20 am
by RustB
sumankar wrote:Hi,
What happens when one of the sums is non-zero and the other is zero?
The output in this case is 0.00 %. Is that correct?
Posted: Thu Feb 24, 2005 1:28 pm
by sumankar
Hi,
Yeah!
But i dont think your code handles the case where both inputs are empty
correctly.Check that out.
Regards,
Suman.
Posted: Thu Feb 24, 2005 1:39 pm
by RustB
If either or both inputs are empty it just prints a blank line.
Can you give me the specific inputs for which you think this program is generating incorrect outputs?
Posted: Thu Feb 24, 2005 2:39 pm
by sumankar
Hi,
No I am clueless.Solved this one long long back.Forgot most tricks ...if there was one.
But i'll look into it.
Regards,
Suman.
Posted: Thu Feb 24, 2005 3:17 pm
by sumankar
One final try:
what is the first is a valid string and next input is EOF?
how do tackle that?
Regards,
Suman.
Posted: Thu Feb 24, 2005 4:21 pm
by RustB
My original code would exit if either input was EOF.
I changed it such that even if the second line is EOF, it continues calculation with the second input as a null string, still WA.
Thanks for all the help. I think this is not a problem with my logic, but an input validation issue. I do not have the patience to sit and debug this either. I will not try too much, it is a waste of time.
Posted: Thu Mar 03, 2005 7:14 pm
by Ryan Pai
After running your code on a few different inputs, you didn't get any answer different than me, but yours did print out an extra line of input. You should check to make sure that at least two lines exist.
It is a very bad habbit to check for end of file in programming contests, many beginners fail due to mistakes like this, especially since sometimes there is an end of line after the last data set, and sometimes there isn't.
So it's usually a better idea to try to read a dataset past where the last one ends, and if you can't read it, then bail. For example:
Code: Select all
while(gets(x) && gets(y)){
//...
}
Posted: Tue Apr 05, 2005 12:50 am
by Sedefcho
Hi, RustB !
Your program is almost working.
I have just made some minor changes to the first 10-15 lines of
your main() function. Just repeat them and you'll get Aceepted.
One of the problems fixed was this one. You have 2*N lines
and the last line ends with EOL. Then an EOF follows.
In that case your program was printing an additional
output line containing 0.00%. Which resulted in WA of course.
Here is your code after my changes.
Code: Select all
int main()
{
char x[30],y[30];
int sumx,sumy;
float ratio;
// while(!feof(stdin))
while(gets(x))
{
gets(y);
if ( strlen(x)==0 ) break;
// gets(x);
sumx=firstsum(x);
while(sumx>=10)
sumx=sumnum(sumx);
// gets(y);
sumy=firstsum(y);
while(sumy>=10)
sumy=sumnum(sumy);
// REST OF main() REMAINS UNCHANGED !
Good luck !
Posted: Thu Jun 09, 2005 6:28 pm
by jaracz
I solve this problem without any suggestions which took place above.
my loop looks like this:
Code: Select all
while(gets(name1) && gets(name2))
{
...(calculate both values)
if(value1>value2)ratio = value2/value1;
else ratio = value1/value2;
printf("%.2lf %%\n",ratio*100);
}
you guys don't have to check if strlen(a)==0 ... or any value == 0 and so on...
You'd better check your alphabet before post;)
Remember that ..op
qrstu
vxyz
Hope it helps!!
10424 Love Calculator
Posted: Sun Aug 14, 2005 3:43 pm
by kakashi
Code: Select all
#include<iostream>
#include<cstdio>
#include<string>
#include<cctype>
using namespace std;
int main(){
string name1,name2;
int length1,length2;
int i;
int num1,num2;
double ans1,ans2;
for(cin >> name1 >> name2;cin;cin >> name1 >> name2){
num1=0;
num2=0;
length1=name1.length();
length2=name2.length();
for(i=0;i<length1;i++){
if(islower(name1[i]))
name1[i]=(name1[i]-32);
num1+=int(name1[i])-64;
}
for(i=0;i<length2;i++){
if(islower(name2[i]))
name2[i]=(name2[i]-32);
num2+=int(name2[i])-64;
}
while(num1>9||num2>9){
ans1=0;
while(num1){
ans1+=num1%10;
num1/=10;
}
num1=int(ans1);
ans2=0;
while(num2){
ans2+=num2%10;
num2/=10;
}
num2=int(ans2);
}
printf("%.2f %%\n",(ans1>ans2 ? (ans2/ans1)*100 : (ans1/ans2)*100));
}
}
I try many input data...
but I can`t find which is wrong .
plz help me

Posted: Mon Aug 22, 2005 5:39 pm
by Niaz
You have done a very simple mistake. Just change your input taking methods, i.e. consider spaces. I hope you got my point !
Thanks for trying my problem.

Posted: Sun Aug 28, 2005 3:05 pm
by kakashi
thanks......
I try to change input taking methods......
using getline and it does work...

10424 love calculator , WA , please help me
Posted: Wed Jan 25, 2006 6:37 pm
by ashikzinnatkhan
Here is my code:
#include <stdio.h>
char name1[30] , name2[30] ;
void main()
{
char letter[26] = { 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' , 'j' , 'k' , 'l' , 'm' , 'n' , 'o' , 'p' ,
'q' , 'r' , 's' , 't' , 'u' , 'v' , 'w' , 'x' , 'y' , 'z' };
int sum1 , sum2 , i , j , temp[3] , temp2;
float ratio , one , two;
char ch;
while( (scanf("%s",name1)) == 1 )
{
scanf("%s",name2);
sum1 = 0;
sum2 = 0;
for(i=0 ; name1 ; i++)
{
if(name1>=65 && name1<=90) ch = letter[(int) name1 - 65];
else ch = name1;
for(j=0; j<26 ; j++)
{
if(ch == letter[j]) sum1 = sum1+j+1;
}
}
for(i=0 ; name2 ; i++)
{
if(name2>=65 && name2<=90) ch = letter[(int) name2 - 65];
else ch = name2;
for(j=0; j<26 ; j++)
{
if(ch == letter[j]) sum2 = sum2+j+1;
}
}
if( sum1 > 10 )
{
while(sum1 > 10)
{
temp2 = sum1;
temp[0] = temp2%10;
temp2 = temp2 - temp[0];
temp2 = temp2/10;
temp[1] = temp2%10;
temp2 = temp2 - temp[1];
temp[2] = temp2/10;
sum1 = temp[0] + temp[1] + temp[2] ;
}
}
if( sum2 > 10 )
{
while(sum2 > 10)
{
temp2 = sum2;
temp[0] = temp2%10;
temp2 = temp2 - temp[0];
temp2 = temp2/10;
temp[1] = temp2%10;
temp2 = temp2 - temp[1];
temp[2] = temp2/10;
sum2 = temp[0] + temp[1] + temp[2] ;
}
}
one = (float) sum1;
two = (float) sum2;
if(one > two) ratio = two/one ;
else ratio = one/two ;
printf("%.2f %%\n", ratio*100.0);
}
}
It is giving WA.
Please help me.