I tried Google's entrance exam (unofficial) [java]

[I tried Google's entrance exam (unofficial)](http://yasigani-ni.com/every-day/google%E3%81%AE%E5%85%A5%E7%A4%BE% E8% A9% A6% E9% A8% 93% E9% 9D% 9E% E5% 85% AC% E5% BC% 8F% E3% 81% AB% E3% 83% 81% E3% 83% A3% E3% 83% AC% E3% 83% B3% E3% 82% B8% E3% 81% 97% E3% 81% A6% E3% 81% BF% E3% 81% 9F /)

I tried it with java.

Tips

If you use a function that counts the number of 1s that appear, the amount of calculation increases as the number increases, so it is efficient to calculate sequentially without making it a function.

hoge.java


class hoge{
	public static void main(String[] args) {		
		int ret =0;
		for(int j=1;j<Integer.parseInt(args[0]);j++){
			ret +=((" "+j+" ").split("1").length-1);
			if(j<=ret){
				System.out.println(ret);
			}
		}
	}
}

result

>java hoge 200000
1
199981
199982
199983
199984
199985
199986
199987
199988
199989
199990
199992
199993
199994
199995
199996
199997
199998
199999
200000

answer

199981

Impressions

Since there is a calculation, I feel that it can be speeded up by exchanging numbers, but I stopped because it did not appear in the formula.

Alternatively, it may be faster to derive an approximate expression and then guess. If you don't use the library, it will take time.

Recommended Posts

I tried Google's entrance exam (unofficial) [java]
I tried Drools (Java, InputStream)
I tried using Java REPL
I tried metaprogramming in Java
I tried UDP communication with Java
I tried the Java framework "Quarkus"
I tried using Java8 Stream API
I tried using JWT in Java
I tried to summarize Java learning (1)
I tried to summarize Java 8 now
I tried using Java memo LocalDate
I tried using GoogleHttpClient of Java
I tried Cassandra's Object Mapper for Java
I tried to summarize Java lambda expressions
Java9 was included, so I tried jshell.
I tried using OpenCV with Java + Tomcat
I tried putting Java on my Mac easily
I tried to make Basic authentication with Java
java I tried to break a simple block
I tried hitting a Java method from ABCL
I tried to implement deep learning in Java
I tried Spring.
I tried tomcat
I tried youtubeDataApi.
I tried refactoring ①
I tried to create Alexa skill in Java
I tried FizzBuzz.
I tried to break a block with java (1)
I tried running Java on a Mac terminal
I tried JHipster 5.1
I tried Java Lambda input / output type ~ POJO edition ~
I tried to implement TCP / IP + BIO with JAVA
I tried to implement Firebase push notification in Java
I tried using Google Cloud Vision API in Java
I tried to operate SQS using AWS Java SDK
# 2 [Note] I tried to calculate multiplication tables in Java.
I tried Java Lambda input / output type ~ Stream version ~
I tried to create a Clova skill in Java
I tried to make a login function in Java
I tried using Log4j2 on a Java EE server
I tried calling Java / Objective-C native code from Flutter
I tried OCR processing a PDF file with Java
I tried to implement Stalin sort with Java Collector
[Java] I tried to implement Yahoo API product search
I tried using an extended for statement in Java
I tried passing Java Silver in 2 weeks without knowing Java
I tried to implement the Euclidean algorithm in Java
~ I tried to learn functional programming in Java now ~
I tried scraping a stock chart using Java (Jsoup)
I tried to find out what changed in Java 9
[I tried] Spring tutorial
I tried running Autoware
I tried using Gson
I first touched Java ③
I tried QUARKUS immediately
I first touched Java ④
I tried using TestNG
I tried Spring Batch
I tried using Galasa
I tried node-jt400 (Programs)
I first touched Java