10424 - Love Calculator

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

Moderator: Board moderators

10424 Love Calculator WA

Postby RustB » Wed Feb 23, 2005 12:57 pm

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?
RustB
New poster
 
Posts: 16
Joined: Mon Jun 14, 2004 5:08 pm

Postby sumankar » Wed Feb 23, 2005 3:09 pm

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.
Code: Select all
31 = 3 + 1 = 4
31 % 9 = 4

Regards,
Suman.
sumankar
A great helper
 
Posts: 288
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta

Postby RustB » Thu Feb 24, 2005 5:20 am

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?
RustB
New poster
 
Posts: 16
Joined: Mon Jun 14, 2004 5:08 pm

Postby sumankar » Thu Feb 24, 2005 1:28 pm

Hi,

Yeah!

But i dont think your code handles the case where both inputs are empty
correctly.Check that out.

Regards,
Suman.
sumankar
A great helper
 
Posts: 288
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta

Postby RustB » Thu Feb 24, 2005 1:39 pm

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?
RustB
New poster
 
Posts: 16
Joined: Mon Jun 14, 2004 5:08 pm

Postby sumankar » Thu Feb 24, 2005 2:39 pm

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.
sumankar
A great helper
 
Posts: 288
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta

Postby sumankar » Thu Feb 24, 2005 3:17 pm

One final try:

what is the first is a valid string and next input is EOF?
how do tackle that?

Regards,
Suman.
sumankar
A great helper
 
Posts: 288
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta

Postby RustB » Thu Feb 24, 2005 4:21 pm

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.
RustB
New poster
 
Posts: 16
Joined: Mon Jun 14, 2004 5:08 pm

Postby Ryan Pai » Thu Mar 03, 2005 7:14 pm

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)){
  //...
}
I'm always willing to help, if you do the same.
Ryan Pai
Learning poster
 
Posts: 67
Joined: Fri Jul 04, 2003 9:59 am
Location: USA

Postby Sedefcho » Tue Apr 05, 2005 12:50 am

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 !
User avatar
Sedefcho
A great helper
 
Posts: 375
Joined: Sun Jan 16, 2005 10:18 pm
Location: Bulgaria

Postby jaracz » Thu Jun 09, 2005 6:28 pm

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 ..opqrstuvxyz
Hope it helps!!
keep it real!
User avatar
jaracz
Learning poster
 
Posts: 79
Joined: Sun Sep 05, 2004 3:54 pm
Location: Poland

10424 Love Calculator

Postby kakashi » Sun Aug 14, 2005 3:43 pm

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 :cry:
kakashi
New poster
 
Posts: 5
Joined: Thu May 26, 2005 10:58 am

Postby Niaz » Mon Aug 22, 2005 5:39 pm

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. :D
Please join The ACM Solver Group at Yahoo
http://groups.yahoo.com/group/acm_solver/
Niaz
Learning poster
 
Posts: 77
Joined: Fri Dec 17, 2004 11:06 am
Location: East West University, Dhaka, Bangladesh

Postby kakashi » Sun Aug 28, 2005 3:05 pm

thanks......
I try to change input taking methods......
using getline and it does work... :D
kakashi
New poster
 
Posts: 5
Joined: Thu May 26, 2005 10:58 am

10424 love calculator , WA , please help me

Postby ashikzinnatkhan » Wed Jan 25, 2006 6:37 pm

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] ; i++)
{
if(name1[i]>=65 && name1[i]<=90) ch = letter[(int) name1[i] - 65];
else ch = name1[i];

for(j=0; j<26 ; j++)
{
if(ch == letter[j]) sum1 = sum1+j+1;
}

}

for(i=0 ; name2[i] ; i++)
{
if(name2[i]>=65 && name2[i]<=90) ch = letter[(int) name2[i] - 65];
else ch = name2[i];

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.
Ashik
ashikzinnatkhan
New poster
 
Posts: 8
Joined: Wed Jan 25, 2006 6:25 pm
Location: Dhaka, Bangladesh

PreviousNext

Return to Volume CIV

Who is online

Users browsing this forum: No registered users and 1 guest