close
題目概要:
有一間Hotel有無限多間房間,請算出在指定的日期有多少人在這間Hotel。
入住這間Hotel有特別的規定:
1. 一次只能有一團旅行團住在裡面。
2. 每團旅行團都是當天早上入住,隔天晚上退房。
3. 後入住的團旅行團需再前一團退房後的隔天早上才能入住。
4. 除了第一團,其餘的旅行團人數都會比前面一團的人數多1人。
5. 旅行團人數即是待在飯店的天數。
解題方向:
每行輸入的第一個數字為旅行團人數(S)、第二個數字為指定日期(D)。S+(S+1)+(S+2)+......+(S+N)<D,求出N即是答案。
程式碼:
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 uva10170{ | |
public static void main(String args[]){ | |
Scanner sc=new Scanner(System.in); | |
while(sc.hasNext()){ | |
long S=sc.nextLong(); | |
long D=sc.nextLong(); | |
long Stemp=S; | |
long count=S; | |
while(Stemp<D){ | |
count++; | |
Stemp=Stemp+count; | |
} | |
System.out.println(count); | |
} | |
} | |
} |
文章標籤
全站熱搜