1 | 15596 | 정수 N개의 합 |
public class Test {
long sum(int[] a) {
long ans = 0;
for(int i=0; i<a.length; i++){
ans += a[i];
}
return ans;
}
}
2 | 4673 | 셀프 넘버 |
class lol{
lol(){
int a = 1;
System.out.println(a);
for(int n=2; n<10001; n++){
for(int j=1; j<n; j++){
if(0<j && j < 100){
if(n == j + (j/10) + (j%10)) break;
if(j == n-1) System.out.println(n);
}
if(100<j && 1000>j){
if(n== j + (j/100) + ((j%100)/10) + (j%10)) break;
if(j == n-1) System.out.println(n);
}
if(1000<j && j<10000){
if(n== j + (j/1000) + ((j%1000)/100) + ((j%100)/10) + j%10) break;
if(j == n-1) System.out.println(n);
}
}
}
}
}
public class Main{
public static void main(String []args){
new lol();
}
}
3 | 1065 | 한수 |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
scan.close();
int count =0;
if(n<100) { //n이 100이 안되면.
for(int i = 0; i<n; i++) count++;
System.out.println(count);
}
if(n>=100 && n<1000) { //n이 3자리수 이면.
count = 99; //1~100까지의 한수 미리 저장.
for(int i=100; i<=n; i++) {
int x = i;
int a = x/100;
int b = x/10 - a*10;
int c = x%10;
if(a==b && b==c) {
count++;
continue;
}
if(a+1 == b && b+1 ==c) {
count++;
continue;
}
if(a+2 == b && b+2 ==c) {
count++;
continue;
}
if(a+3 == b && b+3 ==c) {
count++;
continue;
}
if(a+4 == b && b+4 ==c) {
count++;
continue;
}
if(c+1 == b && b + 1 == a) {
count++;
continue;
}
if(c+2 == b && b + 2 == a) {
count++;
continue;
}
if(c+3 == b && b + 3 == a) {
count++;
continue;
}
if(c+4 == b && b + 4 == a) {
count++;
continue;
}
}
System.out.println(count);
}
if(n==1000)
System.out.println(144);
}
}
728x90
반응형