Tuesday, 12 January 2016

Solution of URI 1837 :: Preface


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,e,f,q,r;
    scanf("%d%d", &a, &b);
    if(a<0)
    {
        e=b;
        if(b<0) e=b*-1;
        for(r=0; r<e; r++)
        {
            f=a-r;
            if(f%b==0) break;
        }
        q=f/b;
    }
    else
    {
        q=a/b;
        r=a%b;
    }
    printf("%d %d\n",q,r);
    return 0;
}

11 comments:

  1. Couldn't understand the logic, can you explain please.

    ReplyDelete
    Replies
    1. yeah, I don't know why the answer is different if the first number is negative :/ to my mind, a division between 7 and 3 is never going to result a quotient of -3, that's crazy :s

      Delete
    2. Fabriccio. This is because the rest can't be negative. The rest must be always positive, so when dividend is negative, you have to multiply once more to make rest being positive. The math's life.

      Delete
    3. i've printed the output they wanted!! then, where is the wrong of 5%!!


      #include

      int main()
      {
      int a,b,c,d;
      scanf("%d %d",&a,&b);
      c=a/b;
      if(a<0) d=(a%b)*(-1);
      else d=a%b;
      printf("%d %d\n",c,d);
      return 0;
      }

      Delete
  2. why 10% wrong?i am so tried for this problem.
    please describe me..
    #include
    using namespace std;
    int main()
    {
    int a,b,p,r;
    scanf("%d %d",&a,&b);
    p=a/b;
    r=a-(b*p);
    r=a%b;
    if(r<0){
    if(p>0)p++;
    if(p<0)p--;
    r=a-(b*p);
    }
    printf("%d %d\n",p,r);
    }


    ReplyDelete
  3. if we solve like that where is the problem??!!

    #include

    int main()
    {
    int a,b,c,d;
    scanf("%d %d",&a,&b);
    c=a/b;
    if(a<0) d=(a%b)*(-1);
    else d=a%b;
    printf("%d %d\n",c,d);
    return 0;
    }

    ReplyDelete
  4. #include
    int main()
    {
    int a,b,c,d;
    scanf("%d %d",&a,&b);
    c=a/b;
    if(a<0)
    d=(a%b)*(-1);
    else
    d=a%b;
    printf("%d %d\n",c,d);
    return 0;


    }
    why it is 5 % wrong .......

    ReplyDelete
  5. #include
    #include
    int main()
    {
    int a,b,x,y,i;
    scanf("%d%d",&a,&b);
    if(a<0&&b>0)
    {
    i=a/b-1;
    y=a-(i*b);
    printf("%d %d\n",i,y);
    }
    else if(a<0&&b<0)
    {
    x=a;
    b=abs(b);
    a=abs(a);
    for(i=1;;i++)
    {
    if(b*i>=a)
    {
    y=(b*i)+x;
    printf("%d %d\n",i,y);
    break;
    }
    }
    }
    else
    {
    i=a/b;
    y=a%b;
    printf("%d %d\n",i,y);
    }
    return 0;
    }

    Why this code is 5% wrong?

    ReplyDelete
  6. #include
    int main ()
    {
    int e,f,a,b;
    scanf ("%d%d",&a,&b);

    if (a>0&&b>0&&b!=0)
    {
    e=a/b;
    f=a -b*e;
    }
    else if (a>0&&b<0&&b!=0)
    {
    e=(a/b);
    f=a - (e*b);
    }
    else if (a<0&&b>0&&b!=0)
    {
    e = (a/b)-1;
    f= a-b*e;
    }
    else
    {
    e= a/b;
    f= a - b*e;
    }
    printf ("%d %d\n",e,f);
    return 0;
    }



    why this is showing 5% wrong answer??

    ReplyDelete
  7. #include
    int main ()
    {
    int a,b,c,e,f;
    scanf ("%d%d",&a,&b);
    if (a>=0&&b>0)
    {
    e=a/b;
    f=a -b*e;
    }
    else if (a>=0&&b<0)
    {

    e=(a/b);
    f=a-(b*e);
    }
    else if (a<0&&b>0)
    {
    c=a*(-1);
    if (c==b)
    {
    e=a/b;
    f=a-e*b;
    }
    else
    {
    e = (a/b)-1;
    f= a-b*e;
    }

    }
    else if (a<0&&b<0)
    {
    if (a==b)
    {
    e=(a/b);
    f=a-e*b;
    }
    else
    {
    e= (a/b)+1;
    f= a - b*e;
    }
    }
    printf ("%d %d\n",e,f);
    return 0;
    }


    why this is showing me 5% wrong

    ReplyDelete

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