M과 N보다 작거나 같은 두 자연수 x,y를 이용해서 년도를 <x:y>로 표현한다.
첫 번째 해는 <1:1>, 두 번째 해는 <2:2>이다
<x:y>의 다음 해는 <x':y'>이다.
- x < M 이면 x'=x+1, 아니면 x'=1
- y < N 이면 y'=y+1, 아니면 y'=1
M,N,x,y가 주어졌을 때, <x:y>이 몇 번째 해인지 구하는 문제
1 <= M,N <= 40,000
전체 경우의 수는 MN=1,600,000,000가지라서 너무 많다.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
for(int j=0;j<num;j++) {
int res=0;
int m = sc.nextInt();
int n = sc.nextInt();
int x = sc.nextInt();
int y = sc.nextInt();
int max = m*n;
int i = 0;
if(y==n)
y=0;
while(true) {
if((m*i+x) % n ==y) {
res = m*i + x;
break;
}
if(m*i + x > max) {
res = -1;
break;
}
i++;
}
System.out.println(res);
}
}
}
1부터 N까지 수를 이어서 쓰면 새로운 하나의 수를 얻게된다.(1<=N<=100,000,000)
이 때 새로운 수는 몇자리 일까?
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
long res =0;
for(int i=1;i<=num;i*=10) {
res += num- (i-1);
}
System.out.println(res);
}
}
정수 n을 1,2,3의 합으로 나타내는 방법의 수를 구하는 문제
n = 4
1+1+1+1
1+1+2
1+2+1
2+1+1
2+2
1+3
3+1
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int arr[] = new int[11];
arr[1]=1;
arr[2]=2;
arr[3]=4;
//1,2,3을 넣었을 때의 경우의 수를 지정.
for(int i=0;i<num;i++) {
int a = sc.nextInt();
for(int j=4;j<=a;j++) {
arr[j]= arr[j-1] + arr[j-2] + arr[j-3];
}
System.out.println(arr[a]);
}
}
}
=> 재귀함수를 쓰지 않고 어차피 1,2,3만 더하는 것을 이용해서 풀었다. 1을 넣었을 때 경우의 수 1, 2를 넣었을 때 2, 3을 넣었을 때 4개를 배열에 넣어놓고 이중 포문을 사용했다.