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
nahid
New poster
Posts: 18 Joined: Wed Oct 04, 2006 8:59 pm
Location: DHAKA,BANGLADESH
Contact:
Post
by nahid » Mon Oct 16, 2006 3:48 am
i've tried all possible cases (included spases and special letters) but getting
and again
. help me.pls.
Code: Select all
#include<stdio.h>
int calcu(char a[],int n);
int add(int d);
void main()
{
int c,d;
float res;
char a[50],b[50];
while(gets(a)&&gets(b))
{
c=calcu(a,50);
d=calcu(b,50);
if(c<d)
res=c/(float)d*100;
else
res=d/(float)c*100;
printf("%.2f %%\n",res);
}
}
int calcu(char a[],int n)
{
int c,d=0,i;
for(i=0;a[i]!='\0';i++)
{
if(a[i]>=65&&a[i]<=90)
{
c=a[i]-64;
d+=c;
}
else if(a[i]>=97&&a[i]<=122)
{
c=a[i]-96;
d+=c;
}
}
while(d>10)
d=add(d);
return(d);
}
int add(int d)
{
int e,f=0;
while(d!=0)
{
e=d%10;
d/=10;
f+=e;
}
return(f);
}
Tariq Shahriar
New poster
Posts: 17 Joined: Wed Mar 01, 2006 8:34 pm
Location: 2nd floor
Post
by Tariq Shahriar » Tue Oct 17, 2006 11:13 am
Perhaps, problem is in your calcu() function
problem description wrote: make the sum of these numbers until it comes in one digit
so
is wrong. beacuse, if d=10 then the loop will stop.
you may change the loop condition by this way-
or,
[ Common thing of every man is that, everyone thinks that he is uncommon ]
john_locke
New poster
Posts: 13 Joined: Sat Oct 07, 2006 6:42 pm
Contact:
Post
by john_locke » Mon Jan 29, 2007 7:11 am
hi friends this is my code dont know why i am getting wrong answer
#include<iostream>
#include<string>
#include<stdio.h>
using namespace std;
int main()
{
float percen,m1,m2;
char s1[30],s2[30];
int len1,len2,temp,temp2;
while(cin.getline(s1,26))
{
cin.getline(s2,26);
int sum1=0,sum2=0;
len1=strlen(s1);
len2=strlen(s2);
for(int i=0;i<len1;i++)
{
if(s1>=65&&s1<91)
sum1=sum1+s1-65+1;
if(s1>=97&&s1<122)
sum1=sum1+s1-97+1;
}
while(sum1/10!=0)
{
sum1=sum1%10+(sum1/10);
}
for(int i=0;i<len2;i++)
{
if(s2>=65&&s2<91)
sum2=sum2+s2-65+1;
if(s2>=97&&s2[i]<122)
sum2=sum2+s2[i]-97+1;
}
while(sum2/10!=0)
{
sum2=sum2%10+(sum2/10);
}
m1=(float)sum1;
m2=(float)sum2;
if(m1==0&&m2==0)
cout<<"0.00 %\n";
else{
if(sum1>=sum2)
percen=(m2/m1)*100.0;
else
percen=(m1/m2)*100.0;
printf("%.2f ",percen);
cout<<"%\n";}
}
return 0;
}
ishtiaq ahmed
Learning poster
Posts: 53 Joined: Sat Jul 29, 2006 7:33 am
Location: (CSE,DU), Dhaka,Bangladesh
Post
by ishtiaq ahmed » Sat Mar 03, 2007 12:52 pm
PLZ TRY TO HELP ME. MY PROBLEM HANGS ON PRESENTATION ERROR.HERE IS MY CODE
Code: Select all
The code is removed after getting AC.
No venture no gain
with best regards
------------------------
ishtiaq ahmed
newton
Experienced poster
Posts: 162 Joined: Thu Jul 13, 2006 7:07 am
Location: Campus Area. Dhaka.Bangladesh
Contact:
Post
by newton » Sat Mar 10, 2007 2:09 pm
dear ishtiaq
please try to change your code
to:
and must remove the code after AC.
newton.................. simply the best
mrmbdctg
New poster
Posts: 18 Joined: Sun Mar 04, 2007 7:12 am
Location: Bangladesh
Contact:
Post
by mrmbdctg » Sat May 19, 2007 5:58 pm
#include<stdio.h>
#include<string.h>
int sum(char alpha[25]);
void main()
{
char name1[25]="\x0",name2[25]="\x0";
while(scanf("%s%s",&name1,&name2)!=EOF)
{
float ratio=0;
int len1, len2;
int p=0,q=0;
len1=strlen(name1);
len2=strlen(name2);
if(len1>25 || len2>25)
{
continue ;
}
p=sum(name1);
q=sum(name2);
if(p<q)
ratio=((float)p/(float)q)*100;
else
ratio=((float)q/(float)p)*100;
printf("%.2f %%\n",ratio);
//printf(" %\n");
}
}
int sum(char alpha[25])
{
int let=0,len,s=0,r,i=0,a[25]={0};
len=strlen(alpha);
for(i=0;i<=len-1;i++)
{
switch(alpha)
{
case'a':
let+=1;
break;
case'b':
let+=2;
break;
case'c':
let+=3;
break;
case'd':
let+=4;
break;
case'e':
let+=5;
break;
case'f':
let+=6;
break;
case'g':
let+=7;
break;
case'h':
let+=8;
break;
case'i':
let+=9;
break;
case'j':
let+=10;
break;
case'k':
let+=11;
break;
case'l':
let+=12;
break;
case'm':
let+=13;
break;
case'n':
let+=14;
break;
case'o':
let+=15;
break;
case'p':
let+=16;
break;
case'q':
let+=17;
break;
case'r':
let+=18;
break;
case's':
let+=19;
break;
case't':
let+=20;
break;
case'u':
let+=21;
break;
case'v':
let+=22;
break;
case'w':
let+=23;
break;
case'x':
let+=24;
break;
case'y':
let+=25;
break;
case'z':
let+=26;
break;
case'A':
let+=1;
break;
case'B':
let+=2;
break;
case'C':
let+=3;
break;
case'D':
let+=4;
break;
case'E':
let+=5;
break;
case'F':
let+=6;
break;
case'G':
let+=7;
break;
case'H':
let+=8;
break;
case'I':
let+=9;
break;
case'J':
let+=10;
break;
case'K':
let+=11;
break;
case'L':
let+=12;
break;
case'M':
let+=13;
break;
case'N':
let+=14;
break;
case'O':
let+=15;
break;
case'P':
let+=16;
break;
case'Q':
let+=17;
break;
case'R':
let+=18;
break;
case'S':
let+=19;
break;
case'T':
let+=20;
break;
case'U':
let+=21;
break;
case'V':
let+=22;
break;
case'W':
let+=23;
break;
case'X':
let+=24;
break;
case'Y':
let+=25;
break;
case'Z':
let+=26;
break;
}
}
while(let>0)
{
r=let%10;
a=r;
s=s+r;
i++;
let=let/10;
if(s>9 && let==0)
{
let=s;
s=0;
}
}
r=0;
return s;
}
sufi
New poster
Posts: 1 Joined: Mon Feb 12, 2007 12:21 pm
Location: Dhaka University
Post
by sufi » Sat Jul 28, 2007 2:00 pm
Wrong Answer
Plz some one help me.
Code: Select all
#include<stdio.h>
float cout(char st[100000]);
void main()
{
float x,y;
float sum=0;
char st1[100000],st2[100000];
while(gets(st1))
{
x=cout(st1);
gets(st2);
y=cout(st2);
if(x>y)
sum=(float)(y*100/x);
else
sum=(float)(x*100/y);
printf("%.2f %c",sum,'%');
printf("\n");
}
}
float cout(char st[])
{
long i,tot=0;
float sum;
for(i=0;st[i];i++)
{
sum=0;
if(st[i]>='a'&&st[i]<='z')
sum=st[i]-96;
if(st[i]>='A'&&st[i]<='Z')
sum=st[i]-64;
tot=tot+sum;
}
sum=tot%9;
return sum;
}
apurba
New poster
Posts: 42 Joined: Sun Oct 07, 2007 10:29 pm
Post
by apurba » Fri Nov 30, 2007 11:22 am
Last edited by
apurba on Wed Dec 12, 2007 8:46 pm, edited 2 times in total.
Jan
Guru
Posts: 1334 Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:
Post
by Jan » Fri Nov 30, 2007 7:29 pm
mask_man
New poster
Posts: 2 Joined: Mon Mar 17, 2008 8:11 pm
Post
by mask_man » Mon Mar 17, 2008 8:41 pm
#include<stdio.h>
#include<string.h>
int main()
{
char name1[27];
char name2[27],a='%';
int sum1,sum2;
int len1,len2;
float output,s1,s2;
while(scanf("%s%s",name1,name2)==2)
{
len1=strlen(name1);
len2=strlen(name2);
sum1=0;
sum2=0;
for(int i=0;i<len1;i++)
{
if(name1>=65&&name1<=90)
{
sum1+=name1-64;
}
if(name1>=97&&name1<=122)
{
sum1+=name1-96;
}
}
for( i=0;i<len2;i++)
{
if(name2>=65&&name2<=90)
{
sum2+=name2-64;
}
if(name2>=97&&name2[i]<=122)
{
sum2+=name2[i]-96;
}
}
sum1=sum1%9;
sum2=sum2%9;
if(sum1==0)
sum1=9;
if(sum2==0)
sum2=9;
s1=(float)sum1;
s2=(float)sum2;
output=s2/s1;
if(output>100)
printf("100.00 %c\n",a);
else if(output==0)
printf("100.00 %c\n",a);
else
{
output=output*100.00;
printf("%.2f %c\n",output,a);
}
}
return 0;
}
/// Can any one say where is the Compilation Error........help me
OmarOthman
New poster
Posts: 4 Joined: Mon Apr 03, 2006 1:05 am
Location: Cairo - Egypt
Contact:
Post
by OmarOthman » Fri Mar 21, 2008 4:45 pm
This is my code. It throws a runtime error. My question is: Is this way of acquiring user input correct ?! Please if it were correct take a look and tell me why the runtime error appears...
import java.io.*;
import java.text.DecimalFormat;
class Main {
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader( System.in );
BufferedReader stdin = new BufferedReader( isr );
String input = "", output = "";
double lover1, lover2, loveRatio;
DecimalFormat formatter = new DecimalFormat("##0.00");
formatter.setDecimalSeparatorAlwaysShown(true);
input = stdin.readLine();
while(!(input.equals(null) || input.equals("")))
{
input = input.toUpperCase();
input = input.replaceAll("[^A-Z]", "");
lover1 = EvaluateDigitsSum(input);
input = stdin.readLine();
input = input.toUpperCase();
input = input.replaceAll("[^A-Z]", "");
lover2 = EvaluateDigitsSum(input);
if (lover1 == 0 || lover2 == 0)
loveRatio = 0;
else
loveRatio = (lover1 > lover2) ? (lover2 / lover1) : (lover1 / lover2);
loveRatio *= 100;
output += formatter.format(loveRatio) + " %\n";
input = stdin.readLine();
}
System.out.print(output.substring(0, output.length() - 1));
}
private static int EvaluateDigitsSum(String input)
{
int inputLength, i, sum, digisum;
sum = 0;
inputLength = input.length();
for (i = 0; i < inputLength; i++)
sum += input.charAt(i) - 'A' + 1;
// Find the individual digits sum.
digisum = sum % 10;
sum /= 10;
digisum += sum % 10;
sum /= 10;
digisum += sum % 10;
if (digisum > 9)
digisum = digisum % 10 + digisum / 10;
return digisum;
}
}
Omar Mohammad Othman
Teaching Assistant
Scientific Computing Department
Faculty of Computer & Information Sciences
AinShams University
Abbassia, Cairo, Egypt
http://cis.shams.edu.eg/
Mob.: +20 10 535 94 96
Mail:
omar_m_othman@yahoo.com
mf
Guru
Posts: 1244 Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:
Post
by mf » Fri Mar 21, 2008 8:09 pm
Well, this one is obviously wrong:
while(!(input.equals(null) || input.equals("")))
If input is null, you're not allowed to call any of its methods. The right thing to use is just 'input == null'.
System.out.print(output.substring(0, output.length() - 1));
Why strip the last \n? It doesn't harm anyone.
And you can do formatted output much easier - System.out.printf("%.2f %%\n", x);
OmarOthman
New poster
Posts: 4 Joined: Mon Apr 03, 2006 1:05 am
Location: Cairo - Egypt
Contact:
Post
by OmarOthman » Fri Mar 21, 2008 8:48 pm
Thank you very much for the fast and detailed reply. Sorry about that... but the anomaly of using == and equals in Java always causes me nightmares!! I'm an expert on C#, and this one is well handled there...
Regarding the last '\n', actually it was considered an "output format error" during the college days, so this has become a "habit"...
Omar Mohammad Othman
Teaching Assistant
Scientific Computing Department
Faculty of Computer & Information Sciences
AinShams University
Abbassia, Cairo, Egypt
http://cis.shams.edu.eg/
Mob.: +20 10 535 94 96
Mail:
omar_m_othman@yahoo.com
Obaida
A great helper
Posts: 380 Joined: Wed Jan 16, 2008 6:51 am
Location: (BUBT) Dhaka,Bagladesh.
Post
by Obaida » Thu Jun 05, 2008 1:37 pm
Last edited by
Obaida on Sun Nov 02, 2008 11:53 am, edited 1 time in total.
try_try_try_try_&&&
_try@try.com
This may be the address of success.
Jan
Guru
Posts: 1334 Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:
Post
by Jan » Thu Jun 05, 2008 7:01 pm
Try the case.
Input:
Output:
Hope it helps. Never use 'float', use 'double'.