HDOJ(HDU) 1562 Guess the number(水题,枚举就行)

简介: Problem Description Happy new year to everybody! Now, I want you to guess a minimum number x betwwn 1000 and 9999 to let (1) x % a = 0;...

Problem Description
Happy new year to everybody!
Now, I want you to guess a minimum number x betwwn 1000 and 9999 to let
(1) x % a = 0;
(2) (x+1) % b = 0;
(3) (x+2) % c = 0;
and a, b, c are integers between 1 and 100.
Given a,b,c, tell me what is the number of x ?

Input
The number of test cases c is in the first line of input, then c test cases followed.every test contains three integers a, b, c.

Output
For each test case your program should output one line with the minimal number x, you should remember that x is between 1000 and 9999. If there is no answer for x, output “Impossible”.

Sample Input
2
44 38 49
25 56 3

Sample Output
Impossible
2575

枚举就可以了。。

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (t-- > 0) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            int c = sc.nextInt();
            boolean is = true;
            int tm=a;
            while(a<1000){
                a=a+tm;
            }

            //注意!答案的范围是[1000,9999]
            for (int i = a; i < 10000; i = i + tm) {
                //此处注意i是加tm,而不是再加a了,因为a可能变了。
                if ((i+1)%b==0&&(i+2)%c==0) {
                    System.out.println(i);
                    is = false;
                    break;
                }
            }

            if (is) {
                System.out.println("Impossible");
            }

        }
    }

}
目录
相关文章
|
API
LeetCode 375. Guess Number Higher or Lower II
我们正在玩一个猜数游戏,游戏规则如下: 我从 1 到 n 之间选择一个数字,你来猜我选了哪个数字。 每次你猜错了,我都会告诉你,我选的数字比你的大了或者小了。 然而,当你猜了数字 x 并且猜错了的时候,你需要支付金额为 x 的现金。直到你猜到我选的数字,你才算赢得了这个游戏。
86 0
LeetCode 375. Guess Number Higher or Lower II
|
API
LeetCode 374. Guess Number Higher or Lower
我们正在玩一个猜数字游戏。 游戏规则如下: 我从 1 到 n 选择一个数字。 你需要猜我选择了哪个数字。 每次你猜错了,我会告诉你这个数字是大了还是小了。
60 0
LeetCode 374. Guess Number Higher or Lower
HDOJ 1391 Number Steps(打表DP)
HDOJ 1391 Number Steps(打表DP)
108 0
HDOJ 1391 Number Steps(打表DP)
|
人工智能 Java
HDU - 2018杭电ACM集训队单人排位赛 - 4 - Problem J. number sequence
HDU - 2018杭电ACM集训队单人排位赛 - 4 - Problem J. number sequence
114 0
HDOJ(HDU) 2113 Secret Number(遍历数字位数的每个数字)
HDOJ(HDU) 2113 Secret Number(遍历数字位数的每个数字)
99 0
HDOJ(HDU) 1562 Guess the number(水题,枚举就行)
HDOJ(HDU) 1562 Guess the number(水题,枚举就行)
102 0
HDOJ 1266 Reverse Number(数字反向输出题)
HDOJ 1266 Reverse Number(数字反向输出题)
95 0
|
Java
HDOJ 1018 Big Number(大数位数公式)
HDOJ 1018 Big Number(大数位数公式)
91 0
HDOJ 1005 Number Sequence
HDOJ 1005 Number Sequence
85 0
|
8月前
|
算法
Leetcode 313. Super Ugly Number
题目翻译成中文是『超级丑数』,啥叫丑数?丑数就是素因子只有2,3,5的数,7 14 21不是丑数,因为他们都有7这个素数。 这里的超级丑数只是对丑数的一个扩展,超级丑数的素因子不再仅限于2 3 5,而是由题目给定一个素数数组。与朴素丑数算法相比,只是将素因子变了而已,解法还是和朴素丑数一致的。
73 1