🌕🌗🌑🌑🌑
# 題目連結
- 題目連結
- Online Judge
- uDebug
# 題目說明
Time limit: 3.000 seconds
# 題目
The Joseph’s problem is notoriously known. For those who are not familiar with the original problem: from among n
people, numbered 1, 2, ..., n
, standing in circle every m-th
is going to be executed and only the life of the last remaining person will be saved. Joseph was smart enough to choose the position of the last remaining person, thus saving his life to give us the message about the incident. For example when n = 6
and m = 5
then the people will be executed in the order 5
, 4
, 6
, 2
, 3
and 1
will be saved.
Suppose that there are k
good guys and k
bad guys. In the circle the first k
are good guys and the last k
bad guys. You have to determine such minimal m
that all the bad guys will be executed before the first good guy.
# Input
The input file consists of separate lines containing k
. The last line in the input file contains ‘0’
. You can suppose that 0 < k < 14
.
# Output
The output file will consist of separate lines containing m
corresponding to k
in the input file.
# Sample Input
3
4
0
# Sample Output
5
30
# 解題技巧
理解題目,然後暴力解,小提示題目只有要求 0 < k < 14
可以在讀取輸入前先把答案存進陣列中,如果後面有重複輸入就不用再跑一次,避免 TLE。
# Solution
import java.util.*; | |
public class Main { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
int[] arr = new int[15]; | |
for (int k = 1; k < 15; k++) { | |
int m = k + 1; | |
while (true) { | |
int killed = -1; | |
int group = k * 2; | |
while (group > k) { | |
killed += m; | |
killed %= group; | |
if (killed < k) { | |
break; | |
} | |
killed--; | |
group--; | |
} | |
if(group == k){ | |
arr[k] = m; | |
break; | |
} | |
m++; | |
} | |
} | |
int k = sc.nextInt(); | |
while (k != 0) { | |
System.out.println(arr[k]); | |
k = sc.nextInt(); | |
} | |
sc.close(); | |
} | |
} |
單字
** **
!! !!
片語 & 搭配詞
!! !!