본문 바로가기

프로그래밍/자바

[자바] 정올 549 ~ 553 && 634 : 반복제어문3 - 자가진단 1 ~ 7

728x90
반응형
SMALL

자가진단1

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int sum = 0, count = 0, n = sc.nextInt();
        for(int i = 1; i <= n; i++){
            if(sum >= n) break;

            if(i%2 != 0) {
                count++;
                sum += i;
            }
        }//end for
        System.out.println(count + " " + sum);
    }
}

자가진단2

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

자가진단3

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

자가진단4

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String star = "*";
        String blank = " ";
        for(int i=0; i<n; i++){
            System.out.print(blank.repeat(i));
            System.out.println(star.repeat(n-i));
        }
    }
}

자가진단5

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int d = 2*n -1;
        String star = "*";
        String blank = " ";
        for(int i=0; i<n; i++){
            System.out.print(blank.repeat(i));
            System.out.print(star.repeat(d- 2*i));
            System.out.println(blank.repeat(i));
        }
    }
}

자가진단6

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int init = 64;
        String str = "";
        for(int i = n; i >= 1; i--){
            for(int j = 1; j <= i; j++){
                str += (char)(init + j);
            }
            System.out.println(str);
            str = "";
            init += i;
        }
    }
}

자가진단7

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int init_char = 64;
        int init = 0;
        String str = "";
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n + 1 -i; j++) {
                init++;
                System.out.print((init) + " ");
            }

            for(int k = 1; k <= i; k++){
                init_char++;
                System.out.print((char)(init_char) + " ");
            }
            System.out.println();

        }

    }
}
728x90
반응형
LIST