URI 1837 Preface


A solution in c

#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;
}

Post a Comment

8 Comments

  1. Thanks for sharing this post. Your post is really very helpful its students. python online course

    ReplyDelete
  2. #include
    using namespace std;
    int main()
    {
    int a1,b1,a,b,q,r,c;
    cin>>a>>b;

    if(abs(a)<abs(b))
    {
    cout<<0<<" "<<abs(a)<<endl;

    }
    if(b==0)
    {
    cout<<0<<" "<<0<<endl;
    }
    else{
    for(r=0;r<abs(b);r++)
    {
    if(a-r==0)
    {
    q=0;
    a1=(b*q)+r;
    b1=0;
    }
    if(a-r!=0)
    {
    q=(a-r)/b;
    a1=(b*q)+r;
    b1=(a-r)/q;

    }

    if(a1==a&&b1==b)
    {
    cout<<q<<" "<<r<<endl;
    break;
    }

    }
    }

    return 0;
    }

    What's wrong with my code. It’shown 5% error.I tried my best.
    Please help me.

    ReplyDelete
    Replies
    1. If you use a = -4 and b = -7 then your code print (0, 4) base on your 1st condition if(abs(a)<abs(b)) but Right answer is (1, 3) so, it's problem on your code. :)

      Delete
  3. import java.util.Scanner;
    public class Main{
    public static void main(String[]args){
    Scanner sc=new Scanner(System.in);
    int a = sc.nextInt();
    int b = sc.nextInt();
    System.out.printf("%d %d\n",a/b,a%b);
    }
    }
    10% error.Can you do any help?

    ReplyDelete
    Replies
    1. you just find positive number divide and reminder value but problem say that a and b is positive or negative value. so you can follow my solution.

      Delete
  4. Hello sir, i am a new programmer and i am trying to solve the problem about one week but still did not get how does it work.....could you please explain.

    ReplyDelete

If you have any doubts, Please let me know