[SWEA] 5604. 구간합 (java, 수학)

728x90
반응형

 

 

풀이 참고

mygumi.tistory.com/180

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
반응형
TAGS.

Comments