Monday, 7 March 2016

Solution of URI 1094 :: Experiments


Before seeing the solution make sure that you tried enough. Don’t paste the whole code, just find out the logic. If you stuck in trouble, just inform me on comment.

/**Bismillahir Rahmanir Rahim.**/

#include <stdio.h>
int main()
{
    int a,b,c,d=0,e=0,f=0,g;
    double x,y,z;
    char m[2],n[2],p[2],q[2];
    scanf("%d", &a);
    for(b=1; b<=a; b++)
    {
        scanf("%d%s",&c,&m);
        if(m[0]=='C') d+=c;
        else if(m[0]=='R') e+=c;
        else if(m[0]=='S') f+=c;
    }
    g=d+e+f;
    x=(d/(g*1.0))*100.00;
    y=(e/(g*1.0))*100.00;
    z=(f/(g*1.0))*100.00;
    printf("Total: %d cobaias\n",g);
    printf("Total de coelhos: %d\n",d);
    printf("Total de ratos: %d\n",e);
    printf("Total de sapos: %d\n",f);
    printf("Percentual de coelhos: %.2lf %%\n",x);
    printf("Percentual de ratos: %.2lf %%\n",y);
    printf("Percentual de sapos: %.2lf %%\n",z);
    return 0;
}

7 comments:

  1. why you multiple 1.0 with the total?

    ReplyDelete
    Replies
    1. because if you want to chnage a data type from integer to flote or double.you have to multiply it with 1.0

      Delete
  2. #include
    int main ()
    {

    float i,b,c,d=0,f,g,h;
    char a;
    scanf("%f",&b);
    for(i=1;i<=b;i++){
    scanf("%f %c",&c,&a);
    d+=c;
    if(a=='C'){
    f+=c;
    }
    if(a=='R'){
    g+=c;
    }
    if(a=='S'){
    h+=c;
    }
    }

    float l=((f/d)*100);
    float k=((g/d)*100);
    float m=((h/d)*100);

    printf("Total: %.0f cobaias\n",d);
    printf("Total de coelhos: %.0f\n",f);
    printf("Total de ratos: %.0f\n",g);
    printf("Total de sapos: %.0f\n",h);
    printf("Percentual de coelhos: %.2f %\n",l);
    printf("Percentual de ratos: %.2f %\n",k);
    printf("Percentual de sapos: %.2f %\n",m);
    }
    please tell me why its having 30% eror ?

    ReplyDelete
  3. can you please tell me where is wrong??
    it shows 5% error
    #include
    main()
    {
    int i,n,s=0,c=0,r=0,t,a;
    double cp,rp,sp;
    char ch[2];
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
    scanf("%d%s",&a,&ch);
    if(ch[0]=='C')
    {
    c+=a;
    }
    else if(ch[0]=='R')
    {
    r+=a;
    }
    else if(ch[0]=='S')
    {
    s+=a;
    }
    }
    t=r+c+s;
    rp=(r/(t*1.0))*100.00;
    cp=(c/(t*1.0))*100.00;
    sp=(s/(t*1.0))*100.00;
    printf("Total: %d\n",t);
    printf("Total de coelhos: %d\n",c);
    printf("Total de ratos: %d\n",r);
    printf("Total de sapos: %d\n",s);
    printf("Percentual de coelhos: %.2lf %\n",cp);
    printf("Percentual de ratos: %.2lf %\n",rp);
    printf("Percentual de sapos: %.2lf %\n",sp);
    return 0;
    }

    ReplyDelete
    Replies
    1. You haven't given on the top.

      Delete
  4. #include
    int main()
    {
    char name;
    int a[15],i,n;
    int C=0,R=0,S=0;
    float c,r,s,total;

    scanf("%d",&n);


    /**
    scanf("%d", &n);
    for(i=1; i<=a; i++)
    {
    scanf("%d%s",&a[i],&name);
    if(name[0]=='C') C+=a[i];
    else if(name[0]=='R') R+=a[i];
    else if(name[0]=='S') S+=a[i];
    }

    */


    for(i=0 ; i<n ; i++)
    {
    scanf("%d",&a[i]);
    scanf(" %c",&name);
    if(name=='C')
    {
    C+=a[i];
    }
    else if(name=='R')
    {
    R+=a[i];
    }
    else if(name=='S')
    {
    S+=a[i];
    }

    }

    total=C+R+S;

    c=(C/(total*1.0))*100;
    r=(R/(total*1.0))*100;
    s=(S/(total*1.0))*100;

    printf("Total: %.0f cobaias\n",total);
    printf("Total de coelhos: %d\n",C);
    printf("Total de ratos: %d\n",R);
    printf("Total de sapos: %d\n",S);

    printf("Percentual de coelhos: %.2f %c\n",c,'%');
    printf("Percentual de ratos: %.2f %c\n",r,'%');
    printf("Percentual de sapos: %.2f %c\n",s,'%');


    return 0;
    }


    i face runtime error. what happen. plz help me :(

    ReplyDelete
  5. i don't know what is wrong in my code, can someone help me?


    #include

    int main()
    {
    char a;
    int b=0,cc=0,rr=0,ss=0,tot=0,x=0,y=0;
    float med1,med2,med3;
    scanf("%i",&x);
    do{
    scanf("%i %c\n",&b,&a);
    tot=tot+b;
    if (a=='C')
    cc=cc+b;
    else if (a=='R')
    rr=rr+b;
    else if (a=='S')
    ss=ss+b;
    y++;
    }while (y!=x);
    printf("Total: %i cobaias\n",tot);
    printf("Total de coelhos: %i\n",cc);
    printf("Total de ratos: %i\n",rr);
    printf("Total de sapos: %i\n",ss);
    med1=(100*cc)/tot;
    printf("Percentual de coelhos: %.2f %\n",med1);
    med2=(100*rr)/tot;
    printf("Percentual de ratos: %.2f %\n",med2);
    med3=(100*ss)/tot;
    printf("Percentual de sapos: %.2f %\n",med3);
    return 0;
    }

    ReplyDelete

Note: only a member of this blog may post a comment.