URI Solution 1030 - Flavious Josephus Legend - Solution in C++ | Ad Hoc
URI Online Judge Solution Flavious Josephus Legend | Ad Hoc
Problem Name: URI Problem
Problem Number : URI - 1030
Online Judge : URI Online Judge Solution
Level: Ad Hoc
Solution Language : C plus plus
URI Solution 1030 Code 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; }
No comments:
Write commentsTo know more about the problem, give us your valuable commment. We'll try to help you. Thanks