URI Online Judge Solution 1022 TDA Rational - Solution in C, C++, Java, Python and C# - Online Judge Solution

Latest

It is a free Online judges problems solution list. Here you can find UVA online Judge Solution, URI Online Judge Solution, Code Marshal Online Judge Solution, Spoz Online Judge Problems Solution

Tuesday, September 19, 2017

URI Online Judge Solution 1022 TDA Rational - Solution in C, C++, Java, Python and C#

URI Online Judge Solution 1002 Area of a Circle - Solution in C, C++, Java, Python and C#


URI Online Judge Solution 1022 TDA Rational  | Beginner
URI Problem Link - https://www.urionlinejudge.com.br/judge/en/problems/view/1022

Problem Name: 1022 TDA Rational solution
Problem Number : URI - 1022 TDA Rational code
Online Judge : URI Online Judge Solution
Category: Beginner
Solution Language : C,C plus plus, java, python, c#(c sharp)

URI Online Judge Solution 1022 TDA Rational - Solution in C, C++, Java, Python and C#

URI Solution 1022 TDA Rational  problem simple input output:

Input SampleOutput Sample
4
1 / 2 + 3 / 4
1 / 2 - 3 / 4
2 / 3 * 6 / 6
1 / 2 / 3 / 4
10/8 = 5/4
-2/8 = -1/4
12/18 = 2/3
4/6 = 2/3

URI Solution 1022 TDA Rational  Code in C:


#include <stdio.h>

int euclides(int a, int b)
{
    int divisor, dividendo, c;

    if(a == 0)
        return 1;

    if(b > a)
    {
        dividendo = b;
        divisor = a;
    }
    else
    {
        dividendo = a;
        divisor = b;
    }

    while(dividendo % divisor != 0)
    {
        c = dividendo % divisor;
        dividendo = divisor;
        divisor = c;
    }
    return divisor;
}

int main()
{
    char c1, c2, c3;
    int n, N1, N2, D1, D2, num, den, numS, denS, e;
    scanf("%i", &n);

    int i;
    for (i = 0; i < n; ++i)
    {
        scanf("%i %c %i %c %i %c %i", &N1, &c1, &D1, &c2, &N2, &c3, &D2);
        if(c2 == '+')
        {
            num = ((N1 * D2) + (N2 * D1));
            den = (D1 * D2);
        }
        else if(c2 == '-')
        {
            num = ((N1 * D2) - (N2 * D1));
            den = (D1 * D2);
        }
        else if(c2 == '*')
        {
            num = (N1 * N2);
            den = (D1 * D2);
        }
        else
        {
            num = (N1 * D2);
            den = (N2 * D1);
        }

        e = euclides(num, den);
        numS = num / e;
        denS = den / e;

        if(numS > 0 && denS > 0)
        {
            printf("%i/%i = %i/%i\n", num, den, numS, denS);
        }
        else
        {
            if(denS < 0)
            {
                denS = -denS;
                numS = -numS;
            }
            printf("%i/%i = %i/%i\n", num, den, numS, denS);
        }
    }

    return 0;
}

URI Solution 1022 TDA Rational  Code in c++/ URI 1022 solution in CPP:


#include <cstdio>
using namespace std;

int euclides(int a, int b)
{
 int divisor, dividendo, c;

 if(a == 0)
  return 1;

 if(b > a){
  dividendo = b;
  divisor = a;
 }else{
  dividendo = a;
  divisor = b;
 }

 while(dividendo % divisor != 0)
 {
  c = dividendo % divisor;
  dividendo = divisor;
  divisor = c;
 }
 return divisor;
}

int main()
{
 char c1, c2, c3;
 int n, N1, N2, D1, D2, num, den, numS, denS, e;
 scanf("%i", &n);

 for (int i = 0; i < n; ++i)
 {
  scanf("%i %c %i %c %i %c %i", &N1, &c1, &D1, &c2, &N2, &c3, &D2);
  if(c2 == '+'){
   num = ((N1 * D2) + (N2 * D1));
   den = (D1 * D2);
  }else if(c2 == '-'){
   num = ((N1 * D2) - (N2 * D1));
   den = (D1 * D2);
  }else if(c2 == '*'){
   num = (N1 * N2);
   den = (D1 * D2);
  }else{
   num = (N1 * D2);
   den = (N2 * D1);
  }

  e = euclides(num, den);
  numS = num / e; 
  denS = den / e;

  if(numS > 0 && denS > 0){
   printf("%i/%i = %i/%i\n", num, den, numS, denS);
  }else{
   if(denS < 0){
    denS = -denS;
    numS = -numS;
   }
   printf("%i/%i = %i/%i\n", num, den, numS, denS);
  }
 }

 return 0;
}

URI Solution 1022 TDA Rational  Code / URI 1022 solution in Java:



URI Solution 1022 TDA Rational  Code / URI 1022 solution in  Python:

URI Solution 1022 TDA Rational  Code / URI 1022 solution in  C# (C Sharp):


Demonstration:

Just implement this in coding. Since having any problem just put a comment below. Thanks



Tags: URI Online Judge Solution, URI OJ Solution list, URI Problems Solution, URI solver, URI all problem solution list, URI 1022 TDA Rational  code in C, URI 1022 TDA Rational  code in C++, URI 1022 TDA Rational  solution in C, URI solution, URI 1022 TDA Rational  solution in C,URI 1022 TDA Rational  solution in C++-CPP, URI 1022 TDA Rational  solution in C# (C sharp),URI 1022 TDA Rational  solution in Java,URI 1022 TDA Rational  solution in Python,

1 comment:

  1. In python this code is:

    A,B,C,D = list(map(int,input().split()))

    if B>C and D>A and (C+D)>(A+B) and C==abs(C) and D==abs(D) and A%2==0 :
    print("Valores aceitos")
    else:
    print("Valores nao aceitos")

    ReplyDelete