2016年没有参赛,在师兄的介绍下,试了一下简单的一题,需要注意的是读写数据的形式还有具体代码。

2016资格赛 A题

Problem

Bleatrix Trotter the sheep has devised a strategy that helps her fall asleep faster. First, she picks a number N. Then she starts naming N, 2 × N, 3 × N, and so on. Whenever she names a number, she thinks about all of the digits in that number. She keeps track of which digits (0, 1, 2, 3, 4, 5, 6, 7, 8, and 9) she has seen at least once so far as part of any number she has named. Once she has seen each of the ten digits at least once, she will fall asleep.

Bleatrix must start with N and must always name (i + 1) × N directly after i × N. For example, suppose that Bleatrix picks N = 1692. She would count as follows:

  • N = 1692. Now she has seen the digits 1, 2, 6, and 9.
  • 2N = 3384. Now she has seen the digits 1, 2, 3, 4, 6, 8, and 9.
  • 3N = 5076. Now she has seen all ten digits, and falls asleep.

What is the last number that she will name before falling asleep? If she will count forever, print INSOMNIA instead.

Input

The first line of the input gives the number of test cases, TT test cases follow. Each consists of one line with a single integer N, the number Bleatrix has chosen.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the last number that Bleatrix will name before falling asleep, according to the rules described in the statement.

Limits

1 ≤ T ≤ 100.

Small dataset

0 ≤ N ≤ 200.

Large dataset

0 ≤ N ≤ 106.

Sample

Input 
 
Output 
 
5
0
1
2
11
1692
Case #1: INSOMNIA
Case #2: 10
Case #3: 90
Case #4: 110
Case #5: 5076

In Case #1, since 2 × 0 = 0, 3 × 0 = 0, and so on, Bleatrix will never see any digit other than 0, and so she will count forever and never fall asleep. Poor sheep!

In Case #2, Bleatrix will name 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. The 0 will be the last digit needed, and so she will fall asleep after 10.

In Case #3, Bleatrix will name 2, 4, 6... and so on. She will not see the digit 9 in any number until 90, at which point she will fall asleep. By that point, she will have already seen the digits 0, 1, 2, 3, 4, 5, 6, 7, and 8, which will have appeared for the first time in the numbers 10, 10, 2, 30, 4, 50, 6, 70, and 8, respectively.

In Case #4, Bleatrix will name 11, 22, 33, 44, 55, 66, 77, 88, 99, 110 and then fall asleep.

Case #5 is the one described in the problem statement. Note that it would only show up in the Large dataset, and not in the Small dataset.

特殊情况就是输入为0的时候,永远也不会结束,其他情况就不会出现。本以为可能会有某个数导致答案非常大,发生int越界,但是也没有。文件名好像无所谓。

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Scanner; public class CountingSheep {
public static void main(String[] args) throws FileNotFoundException {
FileInputStream fis = new FileInputStream("A-large.in");
PrintStream out = new PrintStream(new FileOutputStream("A-large.out"));
System.setIn(fis);
System.setOut(out);
Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
while (in.hasNext()) {
int t = in.nextInt();
for (int i = 1;i < t+1;i++) {
int n = in.nextInt();
if (n == 0) {
System.out.println("Case #"+i+": "+"INSOMNIA");
} else {
int[] ds = new int[10];
int count = 0;
for (int j = 1;j != 0;j++) {
int temp = n*j;
while (temp != 0) {
int digit = temp%10;
if (ds[digit] == 0) {
ds[digit] = 1;
count++;
}
temp = temp/10;
if (count == 10) {
System.out.println("Case #"+i+": "+n*j);
j = -1;
break;
}
}
}
} }
}
}
}

GoogleCodeJam的更多相关文章

  1. 算法入门(C++)

    iostream,这个头文件里有很多常用的函数,比如swap交换两个变量的值,max求两个值的最大值等. cstdio头文件,这个头文件里包含C风格的输入输出.如果你之前学习过C++语言应该知道cin ...

  2. Java学习笔记(5)----使用正则表达式解决Google Code Jam Qualification2009赛题 Alien Language

    原题地址:https://code.google.com/codejam/contest/90101/dashboard#s=p0 题目描述: Problem After years of study ...

随机推荐

  1. ngx-push-stream模块源码学习(五)——内存清理

    1.定时器         采用nginx自身的定时器管理机制,具体细节待学习过nginx源码后加以补充 2.channel的生成周期 (0).初始(诞生)         发布.订阅均有可能产生ch ...

  2. Mongodb 集群搭建以及常见错误

    Mongodb 集群搭建以及常见错误 1 关于Replica Sets +Sharding(主从复制加分片)搭建,不这详细去说,网上有很多,大部分的例子就三台服务器之间做主从复制,分2个shard,架 ...

  3. 大数据应用日志采集之Scribe演示实例完全解析

    大数据应用日志采集之Scribe演示实例完全解析 引子: Scribe是Facebook开源的日志收集系统,在Facebook内部已经得到大量的应用.它能够从各种日志源上收集日志,存储到一个中央存储系 ...

  4. python 提供INI配置文件的操作 ConfigParser

    原文地址:http://www.cnblogs.com/pumaboyd/archive/2008/08/11/1265416.html 红色的为标注信息 +++++++++++++++++引用+++ ...

  5. TFS体系结构和概念

    TFS体系结构和概念 TFS是Team Fundation Server的简称,是微软VSTS的一部分,它是Microsoft应用程序生命周期管理(ALM)工具的核心协作平台,简单的说它是管理和开发软 ...

  6. linux netstat 命令详解

    linux netstat 命令详解 1.功能与说明 netstat 用于显示linux中各种网络相关信息.如网络链接 路由表  接口状态链接 多播成员等等. 2.参数含义介绍 -a (all)显示所 ...

  7. Python之路2Day(基础深入)

    一,整数 1.int型 具有的功能: class int(object): (1).返回表示该数字的时占用的最少位数 def bit_length(self): 例: >>> bin ...

  8. 解密:LL与LR解析 1(译)

    解密:LL与LR解析 1 作者:Josh Haberman 翻译:杨贵福 由于GFW,我无法联系到作者,所以没有授权,瞎翻译的.原文在这里[http://blog.reverberate.org/20 ...

  9. [置顶] 如何高效使用和管理Bitmap--图片缓存管理模块的设计与实现

    传送门 ☞ 轮子的专栏 ☞ 转载请注明 ☞ http://blog.csdn.net/leverage_1229 上周为360全景项目引入了图片缓存模块.因为是在Android4.0平台以上运作,出于 ...

  10. Linux内核源代码

    说明:只供学习交流 一,目录结构 Linux内核源代码采用树形结构进行组织,非常合理地把功能相关的文件都放在同一个子目录下,使得程序更具有可读性. 二,目录结构 arch目录 arch是archite ...