close

題目概要:

第一筆輸入為總共有多少cases需要處理,接下來每行的數字為n、m,n為全部參賽者人數,m為每集所需要的人數。

解題方向:

n=n-m+1 (每集m個人比賽,並且1人獲勝)。當n<=1時即可停止迴圈。當n=1印出需要多少集數,其餘印出"cannot do this"。

程式碼:

//Java
import java.util.Scanner;
class uva11313{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int cases=sc.nextInt();
for(int i=0;i<cases;i++){
int n=sc.nextInt();
int m=sc.nextInt();
//n=n-m+1 (每集m個人比賽,並且1人獲勝)
int count=0;
while(n>1){
n=n-m+1;
count++;
}
//Output
if(n==1)System.out.println(count);
else System.out.println("cannot do this");
}
}
}
view raw uva11313.java hosted with ❤ by GitHub

arrow
arrow
    文章標籤
    Java
    全站熱搜
    創作者介紹
    創作者 a7069810 的頭像
    a7069810

    紀錄自己的程式人生

    a7069810 發表在 痞客邦 留言(0) 人氣()