[๋ฐฑ์ค] 1 - 4375(with.Java)


๐ก๋ฌธ์ ๋ถ์ ์์ฝ
-- ๋ฌธ์ --
2์ 5๋ก ๋๋์ด ๋จ์ด์ง์ง ์๋ ์ ์ n(1 โค n โค 10000)๊ฐ ์ฃผ์ด์ก์ ๋, ๊ฐ ์๋ฆฟ์๊ฐ ๋ชจ๋ 1๋ก๋ง ์ด๋ฃจ์ด์ง n์ ๋ฐฐ์๋ฅผ ์ฐพ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค.
์ ๋ ฅ : ์ฌ๋ฌ ๊ฐ์ ํ ์คํธ ์ผ์ด์ค๋ก ์ด๋ฃจ์ด์ ธ ์๋ค. ๊ฐ ํ ์คํธ ์ผ์ด์ค๋ ํ ์ค๋ก ์ด๋ฃจ์ด์ ธ ์๊ณ , n์ด ์ฃผ์ด์ง๋ค.
์ถ๋ ฅ : ๊ฐ ์๋ฆฟ์๊ฐ ๋ชจ๋ 1๋ก๋ง ์ด๋ฃจ์ด์ง n์ ๋ฐฐ์ ์ค ๊ฐ์ฅ ์์ ์์ ์๋ฆฌ์๋ฅผ ์ถ๋ ฅํ๋ค.
๐ก์๊ณ ๋ฆฌ์ฆ ์ค๊ณ
n์ BufferedReader๋ก ์ ๋ ฅ๋ฐ์ ํ 1๋ถํฐ ์์ํด์ n์ผ๋ก ๋๋๊ธฐ
1๋ค์์ 11
cnt์ด์ฉํด์ ์๋ฆฌ์ ์ธ๊ธฐ 1โ1, 11โ2
๐ก์ฝ๋
package backjoon;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class BackJoon4375 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n;
long oneNumber,cnt;
while (br.readLine()!=null) {
n = Integer.parseInt(br.readLine());
oneNumber=1;
cnt=1;
while (true) {
if (oneNumber % n == 0) {
System.out.println(cnt);
break;
}
else{
oneNumber=(oneNumber*10)+1;
cnt++;
}
}
}
}
}
๐ก์๊ฐ๋ณต์ก๋
O(N*N)
๐กํ๋ฆฐ ์ด์
์๊ฐ ์ด๊ณผ : long์ 19์๋ฆฌ, 1,11,111โฆ..๋์ด๋๋ฉด์ 20์๋ฆฌ ๋์ด๊ฐ๋ฉด ์ค๋ฒํ๋ก์ฐ(์๋ฃํ์ด ์ ์ฅํ ์ ์๋ ์ต๋๊ฐ ์ด๊ณผ)
๐กํ๋ฆฐ ๋ถ๋ถ ์์ or ๋ค๋ฅธ ํ์ด
package backjoon;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class BackJoon4375 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n;
long oneNumber,cnt;
while (br.readLine()!=null) {
n = Integer.parseInt(br.readLine());
oneNumber=1;
cnt=1;
while (true) {
if (oneNumber % n == 0) {
System.out.println(cnt);
break;
}
else{
oneNumber=(oneNumber*10)+1;
oneNumber %= n; //์ฌ๊ธฐ์ ๋ฏธ๋ฆฌ ์ฒซ๋ฒ์งธ ๋๋๊ธฐ ํ๊ฑฐ๋ผ ์๊ฐ 111%7=41->6 41์ ์ ๊ฐ์ด์๋ค!
cnt++;
}
}
}
}
}
๐ก๋๋์ or ๊ธฐ์ต ํ ์ ๋ณด
- ์ง๋ฌธ : 1, 11, 111, 1111 โฆ. ์ด๋ ๊ฒ ์ซ์๊ฐ ์ปค์ง๋๋ฐ ๋ง์นจ % ์ฐ์ฐ์ด๋ค ? โ ๋ชจ๋๋ฌ ์ฐ์ฐ ์จ์ผ์ง ? ์๊ฐํ๋ฉด ๋๋??
Subscribe to my newsletter
Read articles from Soyulia directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Soyulia
Soyulia
Nice to meet u :) Im Backend Developer