10424 - Love Calculator

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

longted
New poster
Posts: 6
Joined: Wed Aug 20, 2014 10:39 am

10424

Post by longted »

can you tell me my answer is right but upload uva web is wrong.
where is my wrong
ths

Code: Select all

my code is below
// uva_10424.cpp 
//


#include <iostream>
#include <string.h>
#include<iomanip>
#include<stdio.h>
#define MAX_SIZE 512
using namespace std;
int caluated_str_value(char *str)
{
/*
A=65
Z=90
a=97
z=122
0=48
9=57*/
	int sum=0;
	char str_array[MAX_SIZE]={NULL};
	strcpy(str_array,str);
	do
	{	sum=0;
		for(int i=0;i<strlen(str_array);i++)
		{
			int char_value=str_array[i];
			
			if(char_value>=48 &&char_value<=57)
			{
				sum=sum+int(str_array[i]-48);			
			}
			if(char_value>=65 &&char_value<=90)
			{
				sum=sum+int(str_array[i]-64);			
			}
			if(char_value>=97 &&char_value<=122)
			{
				sum=sum+int(str_array[i]-96);			
			}
		}
		char sum_array[MAX_SIZE]={NULL};
		sprintf(sum_array,"%d",sum);
		strcpy(str_array,sum_array);
		
	}while(strlen(str_array)!=1);
		return sum;

}

int  main()
{
	char a_str[MAX_SIZE]={NULL},b_str[MAX_SIZE]={NULL};
	float a_val=0,b_val=0;
	while(cin>>a_str>>b_str)
	{
		for(int i=0;i<=1;i++)
		{
			if(i==0)
			 a_val=caluated_str_value(a_str);
			 else
			 b_val=caluated_str_value(b_str);
		
		}
		if(a_val>b_val)
		{
			float ans=b_val/a_val*100;
			cout<<fixed<<setprecision(2)<<ans<<" %"<<endl;
		}
		else
		{
			float ans=a_val/b_val*100;
			cout<<fixed<<setprecision(2)<<ans<<" %"<<endl;
		
		}
	
	

	
	}

	return 0;
}
lighted
Guru
Posts: 587
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 10424

Post by lighted »

Your code gets Compile Error. Change line

Code: Select all

int _tmain(int argc, _TCHAR* argv[])
You must use int main(), so change to

Code: Select all

int main()
Remove line

Code: Select all

#include "stdafx.h"
Change reading to

Code: Select all

while(gets(a_str)  &&  gets(b_str))

Code: Select all

It is difficult to read your code. Use code tags. Like this.
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
longted
New poster
Posts: 6
Joined: Wed Aug 20, 2014 10:39 am

Re: 10424

Post by longted »

can you tell me why i use while(gets(a_str) && gets(b_str) is accept but use while(cin>>a_str>>b _str) is wrong answer
lighted
Guru
Posts: 587
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 10424

Post by lighted »

If you want to use cin. Try this way. :)

Code: Select all

 while(cin.get(a_str,MAX_SIZE),cin.ignore(),cin.get(b_str,MAX_SIZE),cin.ignore())
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
longted
New poster
Posts: 6
Joined: Wed Aug 20, 2014 10:39 am

Re: 10424

Post by longted »

ths,i want to understand to use different between cin and gets
lighted
Guru
Posts: 587
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 10424

Post by lighted »

At the end of each line there is newline character '\n'.
gets reads line until newline, then newline character is extracted from standart input stream.

As i understand when you use just cin it reads string from input stream but newline character is not extracted from input stream, it is leaved there.
cin.ignore() extracts or removes newline character from input stream.

So to handle newline characters correct you must use gets or cin + cin.ignore together.
There can be other ways to do this. I showed you 2 ways. :)
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
Nood
New poster
Posts: 1
Joined: Fri Sep 12, 2014 11:27 pm

Re: 10424 - Love Calculator

Post by Nood »

I got TLE, don't know why!

Here's my code

Code: Select all

Removed After AC
Xenon Kfr
New poster
Posts: 13
Joined: Tue Nov 18, 2014 1:14 am

Re: 10424 - Love Calculator

Post by Xenon Kfr »

why compilation error?

Code: Select all

#include<stdio.h>
int digit(int);
int main()
{
    int na,nb,sum1,sum,i,j,value;
    float r;
    char a[25],b[25];
    while(gets(a))
    {
        gets(b);
        sum=0;
        for(i=0;a[i];i++)
        {
            if(a[i]>64 && a[i]<91)
            {
                value=a[i]-64;
            }
            else if(a[i]>96 && a[i]<123)
            {
                value=a[i]-96;
            }
            sum=sum+value;
        }
        na=digit(sum);
        sum1=0;
        for(i=0;b[i];i++)
        {
            if(b[i]>64 && b[i]<91)
            {
                value=b[i]-64;
            }
            else if(b[i]>96 && b[i]<123)
            {
                value=b[i]-96;
            }
            sum1=sum1+value;
        }
        nb=digit(sum1);
        if(na<nb)
            r=(float)na/(float)nb*100.00;
        else
            r=(float)nb/(float)na*100.00;
        printf("%.2f\n",r);
    }
    return 0;
}
int digit(int x)
{
    int n,i,sum,d;
    while(1)
    {
        for(i=1;;i*=10)
        {
            if((x/i>0) && (x/i<10))
                break;
        }
        n=i;sum=0;
        for(i=n;i>0;i=i/10)
        {
            d=x/i;
            sum=sum+d;
            x=x-i*d;
        }
        if(sum<10 && sum>0)
            return sum;
        x=sum;
    }
    return sum;
}

lighted
Guru
Posts: 587
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 10424 - Love Calculator

Post by lighted »

It gives WA, not CE. Check it again
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
Xenon Kfr
New poster
Posts: 13
Joined: Tue Nov 18, 2014 1:14 am

Re: 10424 - Love Calculator

Post by Xenon Kfr »

i did check it again.its CE.and why its wrong?
lighted
Guru
Posts: 587
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 10424 - Love Calculator

Post by lighted »

Copy/paste code from post above, not from your pc. It gives WA with ANSI C 4.8.2, C++ 4.8.2, C++11 4.8.2. You can get CE only if you send it with JAVA or PASCAL. :D

Look at your outputs carefully. They don't match. You missed "%" symbol. Don't forget to remove your codes after getting acccepted. 8)
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
Xenon Kfr
New poster
Posts: 13
Joined: Tue Nov 18, 2014 1:14 am

Re: 10424 - Love Calculator

Post by Xenon Kfr »

its WA .why?

Code: Select all

#include<stdio.h>
int digit(int);
int main()
{
    int na,nb,sum1,sum,i,j,value;
    float r;
    char a[25],b[25];
    while(gets(a))
    {
        gets(b);
        sum=0;
        for(i=0;a[i];i++)
        {
            if(a[i]>64 && a[i]<91)
            {
                value=a[i]-64;
            }
            else if(a[i]>96 && a[i]<123)
            {
                value=a[i]-96;
            }
            sum=sum+value;
        }
        na=digit(sum);
        sum1=0;
        for(i=0;b[i];i++)
        {
            if(b[i]>64 && b[i]<91)
            {
                value=b[i]-64;
            }
            else if(b[i]>96 && b[i]<123)
            {
                value=b[i]-96;
            }
            sum1=sum1+value;
        }
        nb=digit(sum1);
        if(na<nb)
            r=(float)na/(float)nb*100.00;
        else
            r=(float)nb/(float)na*100.00;
        printf("%.2f %%\n",r);
    }
    return 0;
}
int digit(int x)
{
    int n,i,sum,d;
    while(1)
    {
        for(i=1;;i*=10)
        {
            if((x/i>0) && (x/i<10))
                break;
        }
        n=i;sum=0;
        for(i=n;i>0;i=i/10)
        {
            d=x/i;
            sum=sum+d;
            x=x-i*d;
        }
        if(sum<10 && sum>0)
            return sum;
        x=sum;
    }
    return sum;
}

lighted
Guru
Posts: 587
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 10424 - Love Calculator

Post by lighted »

Input

Code: Select all

Xenon Kfr
Love Calculator
Acc Output

Code: Select all

87.50 %
Also check input here. http://www.udebug.com/UVa/10424
Your input will be two names. Each name holds not more than 25 characters
So you should make a and b of length 26. One char for '/0'.
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
Xenon Kfr
New poster
Posts: 13
Joined: Tue Nov 18, 2014 1:14 am

Re: 10424 - Love Calculator

Post by Xenon Kfr »

thanx a lot
MD.AMINUL ISLAM
New poster
Posts: 1
Joined: Fri Dec 05, 2014 10:57 pm

Re: 10424 - Love Calculator

Post by MD.AMINUL ISLAM »

Why getting WA ????

Code: Select all

#include<stdio.h>
#include<string.h>
#include<ctype.h>
int digitsum(int n);
int main()
{
    char s1[40],s2[40];
    int i,j,g,sum=0,sum2=0,a,b,l;
    double p,c,d;
    //freopen("1.txt", "r", stdin);
    while(gets(s1))
    {
        gets(s2);
        sum=0,sum2=0,p=0.0,c=0.0,d=0.0;
        //l=strlen(s1);
        for(i=0;i<strlen(s1);i++)
        {
            if(isalpha(s1[i]))
            {
                if(islower(s1[i]))
                    sum+=s1[i]+1-'a';
                else sum+=s1[i]+1-'A';
            }
        }
      // printf("%d\n%d\n",sum,l);
        a=digitsum(sum);
        /*while(a>9)
        {
           a=digitsum(a);
        }*/
        //printf("%d\n",a);
        for(j=0;j<strlen(s2);j++)
        {
            if(isalpha(s2[j]))
            {
                if(islower(s2[j]))
                    sum2+=s2[j]+1-'a';
                else sum2+=s2[j]+1-'A';
            }
        }
        //printf("%d\n",sum2);

        b=digitsum(sum2);
       /* while(b>9)
        {
            b=digitsum(b);
        }*/
       //printf("%d\n", b);
        c=a,d=b;
        p=(d*100.0)/c;
        if(p<100.0)
        printf("%.2lf %%\n",p);
        else printf("100.00 %%\n");
    }
    return 0;
}
int digitsum(int n)
{
   int s=0;
   if(n==0) return 0;
   else
   {
       if((s=n%10+digitsum(n/10))>9)
            return digitsum((s=n%10+digitsum(n/10)));
       else
        return (s=n%10+digitsum(n/10));
   }
   return s;
}
[color=#00BF00][/color]
Post Reply

Return to “Volume 104 (10400-10499)”