GoogleCodeJam
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, T. T 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 |
Case #1: INSOMNIA |
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的更多相关文章
- 算法入门(C++)
iostream,这个头文件里有很多常用的函数,比如swap交换两个变量的值,max求两个值的最大值等. cstdio头文件,这个头文件里包含C风格的输入输出.如果你之前学习过C++语言应该知道cin ...
- Java学习笔记(5)----使用正则表达式解决Google Code Jam Qualification2009赛题 Alien Language
原题地址:https://code.google.com/codejam/contest/90101/dashboard#s=p0 题目描述: Problem After years of study ...
随机推荐
- 多模块分布式系统的简单服务访问 - OSGI原形(.NET)
多模块分布式系统的简单服务访问 - OSGI原形(.NET) 先描述一下本篇描述的适用场景(3台server, 各个模块分布在各个Server上,分布式模块互相依赖.交互的场景): 多个OSIG引擎交 ...
- js在IE浏览器和非IE浏览器中的兼容性问题
下面列出IE和非IE中常见的一些js兼容性问题. //window.event IE:有window.event对象 非IE:没有window.event对象.可以通过给函数的参数传递eve ...
- SQL Server 2008 - Cannot set a credential for principal 'sa'.
SQL Server 2008 - Cannot set a credential for principal 'sa'. 很久没有用到SQL Server了,今天有幸在帮同事解决一个SQL Serv ...
- D3DXCreateTextureFromFileInMemoryEx函数
注:限于翻译水平限制,详情请查阅MSDN D3DXCreateTextureFromFileInMemoryEx 函数 从内存文件创建一个纹理,这是个比D3DXCreateTextureFromFil ...
- .NET接口和类 反射的差异性发现
1 背景 在项目中使用反射,反射出某类型的所有属性(Property)和对应的属性值.起初为了性能考虑在模块首次加载就反射类型的所有属性并将其存入字典.根据一般的编程规范——基于接口编程,所以首次传入 ...
- ASP.NET MVC:利用ASP.NET MVC4的IBundleTransform集成LESS
ASP.NET MVC:利用ASP.NET MVC4的IBundleTransform集成LESS 背景 LESS确实不错,只是每次写完LESS都要手工编译一下有点麻烦(VS插件一直没有安装好),昨天 ...
- Sqlite in Android
在Android上保存本地数据有三种方式,SharedPreferences.Files和Sqlite.SharedPreferences主要是用来保存键值对形式的程序配置信息,与ini.proper ...
- ajax简单后台交互
ajax简单后台交互 1,扯淡 单身的生活,大部分时间享受自由,小部分时间忍受寂寞. 生活有时候,其实蛮苦涩,让人难以下咽.那些用岁月积累起来的苦闷,无处宣泄,在自己的脑海里蔓延成一片片荆棘,让你每每 ...
- android jni——helloworld
看了网上好多牛人写的学习系列都是用HelloWorld作为开始,我们这里也用HelloWorld来开始我们的学习,首先我们来介绍下JNI吧. JNI作为java代码和C/C++的桥梁而存在的,为了让j ...
- 10161 - Ant on a Chessboard
Problem A.Ant on a Chessboard Background One day, an ant called Alice came to an M*M chessboard. She ...