[SWEA] 5604. 구간합 (java, 수학)
728x90
반응형
풀이 참고
package algo0421;
import java.util.Scanner;
public class S_5604_구간합_Solution {
static long t,a,b;
static long[] count;
static long point;
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
count=new long[10];
point=1;//초기화
a=1;
b=sc.nextLong();
while(a<=b) {
while(b%10!=9 && a<=b) {
calculate(b);
b--;
}
if(b<a)break;
while(a%10!=0 && a<=b) {
calculate(a);
a++;
}
a/=10;
b/=10;
for (int i = 0; i < 10; i++) {
count[i]+=(b-a+1)*point;
}
point*=10;
}
for (int i = 0; i < 10; i++) {
System.out.print(count[i]+" ");
}
}
public static void calculate(long x) {
for (long i = x; i >0; i/=10) {
String s=Long.toString(i);
count[s.charAt(s.length()-1)-'0']+=point;
}
// while(x>0) {
// int rest=(int) (x%10);//0~9
// count[rest]+=point;
// x/=10;
// }
}
}
728x90
반응형
'swexpert' 카테고리의 다른 글
[swexpert] 2115. 벌꿀 채취 (java, 백트래킹, 완전탐색) (0) | 2021.04.22 |
---|---|
[swea] 5643번 키순서 (java, dfs) (0) | 2021.04.22 |
[swexpert] 2112. 보호 필름 (dfs, 완탐, java) (0) | 2021.04.20 |
[swexpert] 5607. 조합 (페르마의 정리, 재귀, java) (0) | 2021.04.19 |
[swexpert] 8382. 방향전환 (구현, java) (0) | 2021.04.19 |
TAGS.