[UVA][java] 10992 - The Ghost of Programmers
Problem C
The Ghost of Programmers
Input: Standard Input
Output: Standard Output
Year 2500.
Head of the CSE department of IIUC Dhaka Campus Mr. Shamsul Alam is telling a story to the new students, "This is the oldest building of IIUC-Dhaka Campus. We never go into this building." Students ask him why. He says. "It is the story of 21st century. Our authority always used to arrange programming contests in this building and for this purpose all of the problemsetters of that time used to come here. All of them liked this place very much. But the main problem is that even after their death their ghosts began to come here." Students ask "How many ghosts are there, sir?". "No body knows their number. But I can tell the name of main nine ghosts."
0. The ghost of Tanveer Ahsan – It comes in every 2 year
1. The ghost of Shahriar Manzoor - It comes in every 5 year
2. The ghost of Adrian Kugel - It comes in every 7 year
3. The ghost of Anton Maydell - It comes in every 11 year
4. The ghost of Derek Kisman - It comes in every 15 year
5. The ghost of Rezaul Alam Chowdhury - It comes in every 20 year
6. The ghost of Jimmy Mardell - It comes in every 28 year
7. The ghost of Monirul Hasan - It comes in every 36 year
and finally the second most frequent visitor
8. The ghost of K. M. Iftekhar - It comes in every leap year
Students again ask which ghost came first. His reply was, "All the ghost first came at the year 2148".
Now your task is to write a program which can answer that which ghost/ghosts will come for a given year.
Input
There are will be at most 250 lines of data in the input file each containing a positive integer Y. Y has less than fifty digits. Input is terminated by a line which contains a zero.
Output
For each Y, output will contain the year itself on the first line followed by the name of the ghosts which will come on that year in the form "Ghost of G!!!" (without quotes). G should be replaced with the name of the ghost. If more than one ghost come then follow the order of names as given in the problem statement. If for a given year no ghost from the given names will come then print "No ghost will come in this year"(without quotes). Print a blank line between two consecutive output.
Sample Input Output for Sample Input
2500 0 |
2500 Ghost of Tanveer Ahsan!!! Ghost of Anton Maydell!!!
3000 Ghost of Tanveer Ahsan!!! |
Problem setter: K. M. Iftekhar
Moderator: Shahriar Manzoor, EPS
NB: Leap year is a year that is divisible by 4 and not divisible by 100. But the years that are divisible by 400 are also leap year. For example 2004, 2000 is a leapyear but 1973, 1700 is not leapyear.
This problem will be set by the Ghost of K. M. Iftekhar from the balcony of the oldest building of IIUC Dhaka Campus.
This problem will be significantly moderated by Shahriar Manzoor (Southeast University, Bangladesh) to make it more interesting, when he will first meet the problemsetter after the story being told by the head of IIUC DC in 2500 AD.
題目描述:
鬼全部到的時間點為 2148 年,而每隻鬼幾乎都是每隔一個周期來一次,而最後一隻鬼例外,來的頻率異常高,因為他總是在閏年的時候來。
題目解法:
除了最後一隻鬼外,其他的鬼要先扣除 2148,再去算週期。
而最後一隻而外考慮,不用扣 2148,計算閏年(被4整除,但不被 100 整除,或者被 400 整除。)
基本上是一題大數除小數,應該撐不上是大數的。
很久沒用 java 寫,來一題殺雞用牛刀。
import java.util.Scanner;
import java.math.BigInteger;
public class Main {
public static void main(String[] args) {
int cases = 0;
Scanner cin = new Scanner(System.in);
while(cin.hasNext()) {
String str = cin.next();
if(str.equals("0")) break;
if(cases != 0)
System.out.println("");
++cases;
System.out.println(str);
BigInteger n = new BigInteger(str);
int flag = 0;
if(n.compareTo(BigInteger.valueOf(2148)) < 0) {
System.out.println("No ghost will come in this year");
continue;
}
n = n.subtract(BigInteger.valueOf(2148));
if(n.mod(BigInteger.valueOf(2)).compareTo(BigInteger.ZERO) == 0) {
flag++;
System.out.println("Ghost of Tanveer Ahsan!!!");
}
if(n.mod(BigInteger.valueOf(5)).compareTo(BigInteger.ZERO) == 0) {
flag++;
System.out.println("Ghost of Shahriar Manzoor!!!");
}
if(n.mod(BigInteger.valueOf(7)).compareTo(BigInteger.ZERO) == 0) {
flag++;
System.out.println("Ghost of Adrian Kugel!!!");
}
if(n.mod(BigInteger.valueOf(11)).compareTo(BigInteger.ZERO) == 0) {
flag++;
System.out.println("Ghost of Anton Maydell!!!");
}
if(n.mod(BigInteger.valueOf(15)).compareTo(BigInteger.ZERO) == 0) {
flag++;
System.out.println("Ghost of Derek Kisman!!!");
}
if(n.mod(BigInteger.valueOf(20)).compareTo(BigInteger.ZERO) == 0) {
flag++;
System.out.println("Ghost of Rezaul Alam Chowdhury!!!");
}
if(n.mod(BigInteger.valueOf(28)).compareTo(BigInteger.ZERO) == 0) {
flag++;
System.out.println("Ghost of Jimmy Mardell!!!");
}
if(n.mod(BigInteger.valueOf(36)).compareTo(BigInteger.ZERO) == 0) {
flag++;
System.out.println("Ghost of Monirul Hasan!!!");
}
n = new BigInteger(str);
if(n.mod(BigInteger.valueOf(4)).compareTo(BigInteger.ZERO) == 0 &&
n.mod(BigInteger.valueOf(100)).compareTo(BigInteger.ZERO) != 0 ||
n.mod(BigInteger.valueOf(400)).compareTo(BigInteger.ZERO) == 0) {
flag++;
System.out.println("Ghost of K. M. Iftekhar!!!");
}
if(flag == 0)
System.out.println("No ghost will come in this year");
}
}
}