URI Online Judge Solution 1030 Flavious Josephus Legend - 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

Thursday, December 21, 2017

URI Online Judge Solution 1030 Flavious Josephus Legend - Solution in C, C++, Java, Python and C#

URI Online Judge Solution 1030 Flavious Josephus Legend - Solution in C, C++, Java, Python and C#

URI Online Judge Solution 1030 Flavious Josephus Legend   | Beginner
URI Problem Link -  link

Problem Name: 1030 Flavious Josephus Legend
Problem Number : URI - 1030 Flavious Josephus Legend
Online Judge : URI Online Judge Solution
Category: Beginner
Solution Language : C,C plus plus, java, python, c#(c sharp)



URI Solution 1030 Flavious Josephus Legend  Code in C:


#include <stdio.h>
int josephusLoop(int n, int k, int startingPoint)
{
 if(n == 1)
  return 1;

 int newSp, survivor;
 newSp = (startingPoint + k - 2) % n + 1;
  survivor = josephusLoop(n - 1, k, newSp);

  if(survivor < newSp){
   return survivor;
  }else{
   return survivor + 1;
  }
}

int josephus(int n, int k)
{
 return josephusLoop(n, k, 1);
}

int main()
{
 int nc, n, k, s, i;
 scanf("%i", &nc);

 for (i = 1; i <= nc; ++i)
 {
  scanf("%i %i", &n, &k);
  s = josephus(n, k);
  printf("Case %i: %i\n", i, s);
 }

 return 0;
}




URI Solution 1030 Flavious Josephus Legend  Code / URI 1030 solution in CPP:

#include <cstdio>
using namespace std;

int josephusLoop(int n, int k, int startingPoint)
{
 if(n == 1)
  return 1;

 int newSp, survivor;
 newSp = (startingPoint + k - 2) % n + 1;
  survivor = josephusLoop(n - 1, k, newSp);
  
  if(survivor < newSp){
   return survivor;
  }else{
   return survivor + 1;
  }
}

int josephus(int n, int k)
{
 return josephusLoop(n, k, 1);
}

int main()
{
 int nc, n, k, s;
 scanf("%i", &nc);

 for (int i = 1; i <= nc; ++i)
 {
  scanf("%i %i", &n, &k);
  s = josephus(n, k);
  printf("Case %i: %i\n", i, s);
 }

 return 0;
}

URI Solution 1030 Flavious Josephus Legend  Code / URI 1030 solution in Java:

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main {

    static FastScanner in = new FastScanner(System.in);
    static PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) throws IOException {

        int NC = in.nextInt(), n, k;
        for (int i = 1; i <= NC; i++) {
            n = in.nextInt();
            k = in.nextInt();
            out.println("Case " + i + ": " + (josephus(n, k) + 1));
        }
        in.close();
        out.close();
    }

    private static int josephus(int n, int k) {
        // Runtime error :/
        /* if (n == 1) {
            return 0;
        }
        return (josephus(n - 1, m) + m) % n; */
        int ans = 0;
        for (int i = 2; i <= n; i++) {
            ans = (ans + k) % i;
        }

        return ans;
    }

    static class FastScanner implements Closeable {

        private final BufferedReader reader;
        private StringTokenizer tokenizer;

        public FastScanner(InputStream input) {
            reader = new BufferedReader(
                    new InputStreamReader(input));
            tokenizer = new StringTokenizer("");
        }

        public String next() throws IOException {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                String line = reader.readLine();
                if (line == null) {
                    return null;
                }
                tokenizer = new StringTokenizer(line);
            }
            return tokenizer.nextToken();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public float nextFloat() throws IOException {
            return Float.parseFloat(next());
        }

        public double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }

        @Override
        public void close() throws IOException {
            tokenizer = null;
            reader.close();
        }
    }
}

URI Solution 1030 Flavious Josephus Legend Code / URI solution in  Python:


URI Solution Code / URI  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 Online Judge Solution, URI Solution - Ad Hoc,  URI 1030 Flavious Josephus Legend, URI Online Judge Solution 1030 Flavious Josephus Legend - Solution in C, C++, Java, Python and C#

1 comment:

  1. Good post man,,


    For basic problem solution you can follow my website.

    https://oloshcoder.org

    ReplyDelete