Thursday, May 11, 2017

URI Factorial Sum Solution in C, C++, Java | URI 1161 Solution

URI Factorial Sum Solution in C, C++, Java | Mathematics

URI Online Judge Solution  Factorial Sum | Mathematics
URI Main Problem Link - https://www.urionlinejudge.com.br/judge/en/problems/view/1161

Problem Name: URI Problem Factorial Sum
Problem Number : URI Problem 1161 Solution Factorial Sum
Online Judge : URI Online Judge Solution
Level: Mathematics
Solution Language : C, C plus plus, java

URI Factorial Sum Solution in C, C++, Java | URI 1161 Solution

URI Factorial Sum Solution/Code in C:


Tips For solve URI 1161 in C: Look in C code you have to use the long long int to take and store value. Because in problem it has said that the digits can be more than 48 digits.

And to take long long int value we use %lld, Also we have to use != EOF (End Of File) in C. Otherwise you will get a time limit exit problem.
Just look this line--> while(scanf("%lld %lld", &m, &n) != EOF)

And also to print long long int value use: %lld, in this line - printf("%lld\n", tmp + tmp2);


#include <stdio.h>
int main()
{
 long long int m, n, tmp, tmp2;
    int i;

 while(scanf("%lld %lld", &m, &n) != EOF)
 {
  tmp = 1;
  tmp2 = 1;
  for (i = m; i > 0; --i)
  {
   tmp *= m;
   m--;
  }

  for (i = n; i > 0; --i)
  {
   tmp2 *= n;
   n--;
  }

  printf("%lld\n", tmp + tmp2);
 }

 return 0;
}


URI 1161 Solution - Factorial Sum solution in C++

URI Factorial Sum Solution like C:


#include <iostream>
using namespace std;

int main()
{
 long long int m, n, tmp, tmp2;

 while(cin >> m >> n)
 {
  tmp = 1;
  tmp2 = 1;
  for (int i = m; i > 0; --i)
  {
   tmp *= m;
   m--;
  }

  for (int i = n; i > 0; --i)
  {
   tmp2 *= n;
   n--;
  }

  cout << (tmp + tmp2) << endl;
 }

 return 0;
}



URI Factorial Sum Solution in C++ Using array:


#include <stdio.h>
#include <iostream>

int main(){
    long long m, n;
    
    long long fat[21];
    fat[0] = 1;
    
    for(int i = 1; i <= 20; i++){
            fat[i] = fat[i-1]*i;
     }
    
    while(std::cin >> m){
                   std::cin >> n;
                 printf("%lld\n", fat[m] + fat[n]);
    }
    
    return 0;
}




Tags: Uri solve , Uri Factorial Sum solution, URI oj Solve Factorial Sum , URI Online Judge Solution list, URI 1161 Solution, URI 1161 solution in C, URI 1161 Solution in C++, URI 1161 Solution in java

2 comments:
Write comments
  1. I tried this code:

    #include
    #include

    long long int fat(long int result, int num)
    {
    if(num==0)
    {
    return 1;
    }
    result = result*num;
    if(num==1)
    {
    return result;
    }
    else
    {
    return fat(result, num-1);
    }
    }
    int main()
    {
    long long int M, N;
    while (scanf("%lld %lld", &M, &N)!=EOF)
    {
    printf("%lld", fat(1, M) + fat(1, N));
    }
    }

    and even it works perfectly, URI says "wrong answer 20%". I find it pretty annoying how the right answer has to be exactly how they want to, because everyone knows that there are milions of ways of doing the same thing in programming

    ReplyDelete
  2. the c++ solution never end, the loob never stop... i had that problem before
    read ur solution

    ReplyDelete

To know more about the problem, give us your valuable commment. We'll try to help you. Thanks

All rights reserved ©2016 -URI ONLINE JUDGE SOLUTION | Developed by Maniruzzaman Akash

© 2016 URI ONLINE JUDGE SOLUTION. Developed by Maniruzzaman Akash | Distributed By Gooyaabi Templates
Powered by Blogger.