코드업 기초4-1. 단순 반복문

업데이트:


  • 1251 전 문제는 기초 100제 포스팅에 포함되어있다.

[1251] 1 부터 100까지 출력하기

[출력] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		for(int i = 1; i <= 100; i++) {
			System.out.print(i + " ");
		}
	}
}


[1252] 1 부터 n 까지 출력하기

[입력] 5
[출력] 1 2 3 4 5

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 i = 1; i <= num; i++) {
			System.out.print(i + " ");
		}
	}
}


[1253] a 부터 b까지 출력하기

[입력] 3 8
[출력] 3 4 5 6 7 8

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int num1 = sc.nextInt();
		int num2 = sc.nextInt();
		if(num1 > num2) {
			int temp = num1;
			num1 = num2;
			num2 = temp;
		}
		for(int i = num1; i <= num2; i++) {
			System.out.print(i + " ");
		}
	}
}


[1254] 알파벳 출력하기

[입력] d g
[출력] d e f g

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		char a = sc.next().charAt(0);
		char b = sc.next().charAt(0);
		if(a > b) {
			char temp = a;
			a = b;
			b = temp;
		}
		for(char i = a; i <= b; i++) {
			System.out.print(i + " ");
		}
	}
}


[1255] 두 실수 사이 출력하기

[입력] 2.00 2.03
[출력] 2.00 2.01 2.02 2.03

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		double a = sc.nextDouble();
		double b = sc.nextDouble();
		for(double i = a; i <= b; i+=0.01) {
			System.out.printf("%.2f ", i);
		}
	}
}


[1256] 별 출력하기

[입력] 5
[출력] *****

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 i = 1; i <= num; i++) {
			System.out.print("*");
		}
	}
}

[1257] 두 수 사이의 홀수 출력하기

[입력] 2 7
[출력] 3 5 7

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int num1 = sc.nextInt();
		int num2 = sc.nextInt();
		for(int i = num1; i <= num2; i++) {
			if(i % 2 != 0) System.out.print(i + " ");
		}
	}
}


[1259] 1부터 n까지 중 짝수의 합 구하기

[입력] 5
[출력] 6

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		int sum = 0;
		for(int i = 1; i <= num; i++) {
			if(i % 2 == 0) sum += i;
		}
		System.out.println(sum);
	}
}


[1260] 3의 배수의 합

[입력] 3 7
[출력] 9

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int num1 = sc.nextInt();
		int num2 = sc.nextInt();
		int sum = 0;
		for(int i = num1; i <= num2; i++) {
			if(i % 3 == 0) sum += i;
		}
		System.out.println(sum);
	}
}


[1261] First Special Judge (Test)

[입력] 1 2 3 4 5 6 7 8 9 10
[출력] 5

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int[] nums = new int[10];
		int result = 0;
		int check = 0;
		for (int i = 0; i < nums.length; i++) {
			nums[i] = sc.nextInt();
			if (nums[i] % 5 == 0) {
				result = nums[i];
				System.out.println(result);
				break;
			}
		}
		if (result == 0)
			System.out.println(result);
	}
}


[1265] 구구단 출력하기 1

[입력] 3
[출력]
31=3
3
2=6
33=9
3
4=12
35=15
3
6=18
37=21
3
8=24
3*9=27

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int dan = sc.nextInt();
		for(int i = 1; i <= 9; i++) {
			System.out.printf("%d*%d=%d\n", dan, i, dan*i);
		}
	}
}


[1266] n개의 수의 합

[입력]
5
3 5 7 7 2
[출력] 24

import java.util.Scanner;
public class Main {
   public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      int sum = 0;
      int input = sc.nextInt();
      for(int i = 0; i < input; i++) {
         sum += sc.nextInt();
      }
      System.out.println(sum);
   }
}


[1267] n개의 수 중 5의 배수의 합

[입력]
5
3 5 7 15 2
[출력] 20

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int input = sc.nextInt();
		int[] nums = new int[input];
		int sum = 0;
		for(int i = 0; i < input; i++) {
			nums[i] = sc.nextInt();
			if(nums[i] % 5 == 0) sum += nums[i];
		}
		System.out.println(sum);
	}
}


[1268] n개의 수 중 짝수의 개수

[입력]
5
3 5 7 15 2
[출력] 1

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int input = sc.nextInt();
		int[] nums = new int[input];
		int count = 0;
		for(int i = 0; i < input; i++) {
			nums[i] = sc.nextInt();
			if(nums[i] % 2 == 0) count++;
		}
		System.out.println(count);
	}
}


[1269] 수열의 값 구하기

[입력] 2 -1 3 5
[출력] 2

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int n = sc.nextInt();
		for(int i = 1; i < n; i++) {
			a = a * b + c;
		}
		System.out.println(a);
	} 
}


[1270] 1의 개수는? 1

[입력] 35
[출력] 4

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int input = sc.nextInt();
		int cnt = 0;
		for(int i = 0; i < input; i+=10) {
			++cnt;
		}
		System.out.println(cnt);
	} 
}


[1271] 최대값 구하기

[입력]
5
3 1 29 31 21
[출력] 31

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		int[] nums = new int[num];
		int max = 0;
		for(int i = 0; i < nums.length; i++) {
			nums[i] = sc.nextInt();
			if(max < nums[i]) {
				max = nums[i];
			}
		}
		System.out.println(max);
	} 
}


[1272] 기부

[입력] 1 2
[출력] 11

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		int h = sc.nextInt();
		int sum = 0;
		int max = 0;
		if(k > h) max = k;
		else max = h;
		int[] arr = new int[max+1];
		int num = 1;
		for(int i = 1; i < arr.length; i++) {
			if(i % 2 == 1) {
				arr[i] = num;
				num++;
			} else {
				arr[i] = arr[i - 1] * 10;
			}
		}
		sum = arr[k] + arr[h];
		System.out.println(sum);
	} 
}


[1273] 약수 구하기

[입력] 6
[출력] 1 2 3 6

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int input = sc.nextInt();
		for(int i = 1; i <= input; i++) {
			if(input % i == 0) {
				System.out.print(i + " ");
			}
		}
	} 
}


[1274] 소수 판별

[입력] 7
[출력] prime

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		int count = 0;
		for(int i=1; i<=num; i++) {
			if(num % i == 0 ) { 
				count++;
			}
		}
		if(count == 2 ) { 
			System.out.print("prime");
		} else {
			System.out.print("not prime");
		}
	} 
}


[1275] k 제곱 구하기

[입력] 3 3
[출력] 27

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int num1 = sc.nextInt();
		int num2 = sc.nextInt();
		int result = 1;
		for(int i = 1; i <= num2; i++) {
			result *= num1;
		}
		System.out.println(result);
	} 
}


[1276] 팩토리얼 계산

[입력] 5
[출력] 120

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		int result = 1;
		for(int i = 1; i <= num; i++) {
			result = i * result;
		}
		System.out.println(result);
	} 
}


[1277] 몇 번째 데이터 출력하기

[입력]
7
2 4 7 3 1 2 5
[출력] 2 3 5

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[num];
		for(int i = 0; i < arr.length; i++) {
			arr[i] = sc.nextInt();
		}
		System.out.print("" + arr[0] + " " + arr[num/2] + " " + arr[arr.length-1]);
	} 
}


[1278] 자릿수 계산

[입력] 932
[출력] 3

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String num = sc.nextLine();
		System.out.println(num.length());
	} 
}


[1279] 홀수는 더하고 짝수는 빼고 1

[입력] 5 10
[출력] -3

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int num1 = sc.nextInt();
		int num2 = sc.nextInt();
		int result = 0;
		
		for(int i = num1; i <= num2; i++) {
			if(i % 2 == 1) {
				result += i;
			}else {
				result -= i;
			}
		}
		System.out.println(result);
	} 
}


[1280] 홀수는 더하고 짝수는 빼고 2

[입력] 5 7
[출력] +5-6+7=6

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int num1 = sc.nextInt();
		int num2 = sc.nextInt();
		int result = 0;
		for(int i = num1; i <= num2; i++) {
			if(i % 2 == 1) {
				result += i;
				System.out.print("+" + i);
			}else {
				result -= i;
				System.out.print("-" + i);
			}
		}
		System.out.print("=" + result);
	} 
}


[1281] 홀수는 더하고 짝수는 빼고 3

[입력] 5 7
[출력] 5-6+7=6

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int num1 = sc.nextInt();
		int num2 = sc.nextInt();
		int result = 0;
		
		for(int i = num1; i <= num2; i++) {
			if(i % 2 == 1) {
				result += i;
				if(i == num1) {
					System.out.print(i);
				} else {
					System.out.print("+" + i);
				}
			}else if(i % 2 == 0){
				result -= i;
				System.out.print("-" + i);
			}
		}
		System.out.print("=" + result);
	}
}


[1282] 제곱수 만들기

[입력] 34
[출력] 9 5

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		int min = 1;
		for(int i = num; i >= 0; i--) {
			if(min*min >= num) {
				min--;
				break;
			}
			min++;
		}
		System.out.print(num - min * min  + " ");
		System.out.print(min);
	}
}


[1283] 주식 투자

[입력]
10000
4
10 -10 5 -5
[출력]
-125
bad

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int money = sc.nextInt();
		int day = sc.nextInt();
		double result = money;
		for(int i = 0; i < day; i++) {
			double variance = sc.nextInt();
			if(variance > 0)
				result = result * (1 + (variance /100));
			else
				result = result * (1 - (-variance/ 100));
		}
		double netProfit = result - money;
		System.out.printf("%.0f\n", netProfit);
		if(netProfit < 0) System.out.println("bad");
		else if(netProfit == 0) System.out.println("same");
		else System.out.println("good");
	}
}


[1284] 암호 해독

[입력] 21
[출력] 3 7

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		boolean flag = true;
		int input = sc.nextInt();
		for(int i = 2; i <= input; i++) {
			if(i != 2 && i % 2 == 0)continue;
			for(int j = 2; j <= i; j++) {
				if(a(i) == 1 && a(j) == 1) {
					if(i * j == input) {
						System.out.println(j + " " + i);
						flag = false;
					}
				}
			}
		}
		if(flag) System.out.println("wrong number");
	}
	public static int a(int num) {
		for(int i = 2; i * i <= num; i++) {
			if(num % i == 0) return 0;
		}
		return 1;
	}
}

코드는 이클립스에서 완벽하게 돌아가지만.. 코드업 제출하면 시간초과가 계속 떠서 아직 어떻게 해결해야할지 고민 중이다…..!

태그:

카테고리:

업데이트:

댓글남기기