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. 字符串子串查找strstr

    问题: 函数名: strstr 函数原型:char *strstr(const char *str1, const char *str2); 语法:* strstr(str1,str2) str1: ...

  2. hdu1686

    题目意思:找到上串在下串中有多少个 Problem Description The French author Georges Perec (1936–1982) once wrote a book, ...

  3. JavaScript判断对象的类型

    JavaScript判断对象的类型 最近阅读了一些关于JavaScript判断对象类型的文章.总结下来,主要有constructor属性.typeof操作符.instanceof操作符和Object. ...

  4. SQL Server 如何读写数据

    01. SQL Server 如何读写数据   一. 数据读写流程简要SQL Server作为一个关系型数据库,自然也维持了事务的ACID特性,数据库的读写冲突由事务隔离级别控制.无论有没有显示开启事 ...

  5. Kemaswill 机器学习 数据挖掘 推荐系统 Ranking SVM 简介

    Ranking SVM 简介 排序一直是信息检索的核心问题之一,Learning to Rank(简称LTR)用机器学习的思想来解决排序问题(关于Learning to Rank的简介请见我的博文Le ...

  6. Visual Studio 2012的开发使用技巧

    分享10条Visual Studio 2012的开发使用技巧 使用Visual Studio 2012有一段时间了,并不是追赶潮流,而是被逼迫无可奈何.客户要求的ASP.NET MVC 4的项目,要用 ...

  7. KnockOut文档--模板绑定

    目的 模板绑定使用数据render模板,然后把渲染的结果填充到Dom树中.模板通过重复或嵌套块(通常为您的视图模型数据的函数)用一种简单,方便的方式来建立复杂的UI结构 . 有两种方式使用模板: Na ...

  8. UVA 216 - Getting in Line

    216 - Getting in Line Computer networking requires that the computers in the network be linked. This ...

  9. Elasticsearch中doc_value的认识

    前言:本文的目的是为后续磁盘空间利用优化做铺垫.主要知识点来源于官网文档 一.doc_value是什么 绝大多数的fields在默认情况下是indexed,因此字段数据是可被搜索的.倒排索引中按照一定 ...

  10. springmvc+mybatis+spring 整合 SSM

    A 调用摄像头拍照,自定义裁剪编辑头像 [新录针对本系统的视频教程,手把手教开发一个模块,快速掌握本系统]B 集成代码生成器 [正反双向](单表.主表.明细表.树形表,开发利器)+快速构建表单;  技 ...