close
題目概要:
第一筆輸入為總共有多少cases需要處理,接下來每行的數字為n、m,n為全部參賽者人數,m為每集所需要的人數。
解題方向:
n=n-m+1 (每集m個人比賽,並且1人獲勝)。當n<=1時即可停止迴圈。當n=1印出需要多少集數,其餘印出"cannot do this"。
程式碼:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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"); | |
} | |
} | |
} |
文章標籤
全站熱搜