๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์‹œ๋„/๊ตญ๋น„์ง€์›

[D+06] ์กฐ๊ฑด๋ฌธ & ๋ฐ˜๋ณต๋ฌธ ์˜ˆ์ œ

by ๐Ÿ‡๋ฐ•๋ด‰๋ด‰๐Ÿ‡ 2022. 9. 27.

 

 

Algorithm

-  ์กฐ๊ฑด๋ฌธ & ๋ฐ˜๋ณต๋ฌธ ์˜ˆ์ œ -

 


 

์กฐ๊ฑด๋ฌธ & ๋ฐ˜๋ณต๋ฌธ ์˜ˆ์ œ

• ์กฐ๊ฑด๋ฌธ

Q. ์ž…๋ ฅํ•œ ๊ฐ’์ด 50์ธ์ง€ ์•„๋‹Œ์ง€ ํ™•์ธํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“œ์‹œ์˜ค.

public class Hello {

	public static void main(String[] args) {
		
		int value = 50;
		
		/*
		 ๋น„๊ต ์—ฐ์‚ฐ์ž
		 > , <, <=, >=, ==, !=
		 */
		
		if(value == 50) { //๊ด„ํ˜ธ์•ˆ์— ์žˆ๋Š” ๊ฒƒ์ด ์ฐธ์ด๋ผ๋ฉด (์กฐ๊ฑด๊ด„ํ˜ธ์•ˆ์—” ์ง„๋ฆฌ๊ฐ’๋งŒ ๋“ค์–ด๊ฐ€์•ผํ•œ๋‹ค)
			System.out.println("value๋Š” 50์ž…๋‹ˆ๋‹ค.");
		}
		else { //๊ด„ํ˜ธ์•ˆ์— ์žˆ๋Š” ๊ฒƒ์ด ๊ฑฐ์ง“์ด๋ผ๋ฉด
			System.out.println("value๋Š” 50์ด ์•„๋‹™๋‹ˆ๋‹ค.");
		}
		//์กฐ๊ฑด๋ฌธ ์™„๋ฃŒํ›„ ๋งˆ์ง€๋ง‰์— ์ถœ๋ ฅ
		System.out.println("ํ”„๋กœ๊ทธ๋žจ ๋");
	}

}

 

Q. 20์‚ด๊นŒ์ง€๋Š” A๋ฐฑ์‹ ์„ ์ ‘์ข…ํ•˜๊ณ , 21์‚ด๋ถ€ํ„ฐ 50์‚ด๊นŒ์ง€๋Š” B๋ฐฑ์‹ ์„ ์ ‘์ข…ํ•˜๊ณ  ๋‚˜๋จธ์ง€๋Š” C๋ฐฑ์‹ ์„ ์ ‘์ข…ํ•˜์‹œ์˜ค.

public class Hello {

	public static void main(String[] args) {
		
		/*
		 * 10์‚ด๋ถ€ํ„ฐ 20์‚ด๊นŒ์ง€๋Š” A๋ฐฑ์‹ ์„ ์ ‘์ข….
		 * 21์‚ด๋ถ€ํ„ฐ 50์‚ด๊นŒ์ง€๋Š” B๋ฐฑ์‹ ์„ ์ ‘์ข….
		 * ๋‚˜๋จธ์ง€๋Š” C๋ฐฑ์‹ ์„ ์ ‘์ข…ํ•˜์‹œ์˜ค.
		 */
		
		int age = 23;
		
		 if(age <= 20) {
			 System.out.println("A๋ฐฑ์‹  ์ ‘์ข…");
		 }
		 else if((age >= 21) && (age <= 50)) {//21์‚ด๋ถ€ํ„ฐ 50์‚ด ์ดํ•˜
			 System.out.println("B๋ฐฑ์‹  ์ ‘์ข…");
		 }
		 else {
			 System.out.println("C๋ฐฑ์‹  ์ ‘์ข…");
		 }
		 System.out.println("ํ”„๋กœ๊ทธ๋žจ ๋");
	}
}

 

Q. ์„ฑ์ ํ‘œ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“œ์‹œ์˜ค.

public class Hello {

	public static void main(String[] args) {
		
		/* ๋ฌธ์ œ ***
		 ์ ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. score
		 0 ~ 20 Fํ•™์ 
		 21 ~ 40 Dํ•™์ 
		 41 ~ 60 Cํ•™์ 
		 61 ~ 80 Bํ•™์ 
		 80 ์ด์ƒ์ด๋ฉด Aํ•™์ 
		 A ํ•™์ ์ค‘ 100์ ์ด ์žˆ๋‹ค๋ฉด A+ ํ•™์ .
		 */
		
		int score = 20;
		
		if((score >= 0) && (score <= 20)) {
			System.out.println("๋‹น์‹ ์˜ ํ•™์ ์€ F์ž…๋‹ˆ๋‹ค.");
		}
		else if((score >= 21) && (score <= 40)) {
			System.out.println("๋‹น์‹ ์˜ ํ•™์ ์€ D์ž…๋‹ˆ๋‹ค.");
		}
		else if((score >= 41) && (score <= 60)) {
			System.out.println("๋‹น์‹ ์˜ ํ•™์ ์€ C์ž…๋‹ˆ๋‹ค.");
		}
		else if((score >= 61) && (score <= 80)) {
			System.out.println("๋‹น์‹ ์˜ ํ•™์ ์€ B์ž…๋‹ˆ๋‹ค.");
		}
		else if(score >= 81) {
			if(score == 100) {
				System.out.println("๋‹น์‹ ์˜ ํ•™์ ์€ A+์ž…๋‹ˆ๋‹ค.");
			}
			else {
				System.out.println("๋‹น์‹ ์˜ ํ•™์ ์€ A์ž…๋‹ˆ๋‹ค.");
			}
		}
		else {
			System.out.println("์ ์ˆ˜๊ฐ€ ์ž˜๋ชป๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
		}
		System.out.println("ํ”„๋กœ๊ทธ๋žจ ๋");
	}
}

 

Q. ํŠน์ • ์ถœ์ƒ๋…„๋„์—๊ฒŒ ๋‹น์ฒจ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ด๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“œ์‹œ์˜ค.

public class Hello {

	public static void main(String[] args) {
		
		int year = 2000;
		
		switch(year) {
		case 1980:
			System.out.println("1980๋…„ ์ด์‹œ๋„ค์š”.");
			System.out.println("์ถ•ํ•˜ํ•ฉ๋‹ˆ๋‹ค.");
			break;
			
		case 1990:
			System.out.println("1990๋…„ ์ด์‹œ๋„ค์š”.");
			System.out.println("์ถ•ํ•˜ํ•ฉ๋‹ˆ๋‹ค.");
			break;
			
		case 2000:
			System.out.println("2000๋…„ ์ด์‹œ๋„ค์š”.");
			System.out.println("์ถ•ํ•˜ํ•ฉ๋‹ˆ๋‹ค.");
			break;
			
		default:
			System.out.println("ํƒˆ๋ฝ!");
			break;
		}
	}
}
public class Hello {

	public static void main(String[] args) {
		
		int year = 2000;
		
		switch(year) {
		case 1970:
		case 1980:
		case 1990:
		case 2000:
			System.out.println("์ถ•ํ•˜ํ•ฉ๋‹ˆ๋‹ค. ๋‹น์ฒจ!!");
			break;
			
		default:
			System.out.println("ํƒˆ๋ฝ!");
			break;
		
		}
	}
}

 

 

• ๋ฐ˜๋ณต๋ฌธ

Q. 3๋‹จ ๊ตฌ๊ตฌ๋‹จ์„ ์ถœ๋ ฅํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์‹œ์˜ค.

public class Hello {

	public static void main(String[] args) {

		//3๋‹จ์„ ์ถœ๋ ฅํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์‹œ์˜ค. 
		int cnt = 1;
		
		for(int i = 0; i < 9; i++) {

			System.out.println("3 x " + cnt + " = " + (3 * cnt));
			cnt = cnt + 1;
		}
		
		System.out.println("3๋‹จ ๊ตฌ๊ตฌ๋‹จ ์ข…๋ฃŒ");
	}
}

 

Q. ๊ฐ’์„ ์ž…๋ ฅ๋ฐ›์€ ์ˆซ์ž๋งŒํผ Hello๋ฅผ ์ถœ๋ ฅํ•˜์‹œ์˜ค.

import java.util.Scanner;

public class Hello {

	public static void main(String[] args) 
	{
		//value๋ผ๋Š” ๋ณ€์ˆ˜์— ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์ •์ˆ˜๋ฅผ ๋ฐ›์•„๋“ค์ด๋Š” ์ฝ”๋“œ
		Scanner sc = new Scanner(System.in);
		int value = sc.nextInt();
		
		
		for(int i = 0; i < value; i++) {
			System.out.println("Hello");
		}
		
	}
}
import java.util.Scanner;

public class Hello {

	public static void main(String[] args) 
	{
		//value๋ผ๋Š” ๋ณ€์ˆ˜์— ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์ •์ˆ˜๋ฅผ ๋ฐ›์•„๋“ค์ด๋Š” ์ฝ”๋“œ
		Scanner sc = new Scanner(System.in);
		int value = sc.nextInt();
		
		int i = 0; 
		while(i < value) {
			System.out.println("Hello");
			i++;
		}
		
	}
}

 

Q. ์‚ฌ์šฉ์ž์—๊ฒŒ ์ˆซ์ž๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ 1~์ˆซ์ž๊นŒ์ง€ ๋”ํ•œ ์ดํ•ฉ์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“œ์‹œ์˜ค.

import java.util.Scanner;

public class Hello {

	public static void main(String[] args) 
	{
		//์‚ฌ์šฉ์ž๊ฐ€ 10์„ ์ž…๋ ฅํ•˜๋ฉด 1๋ถ€ํ„ฐ 10๊นŒ์ง€ ๋”ํ•ด
		//์‚ฌ์šฉ์ž๊ฐ€ 20์„ ์ž…๋ ฅํ•˜๋ฉด 1๋ถ€ํ„ฐ 20๊นŒ์ง€ ๋”ํ•ด
		Scanner sc = new Scanner(System.in);
		int value = sc.nextInt();
		
		int total = 0; //์ดํ•ฉ
		int hap = 1; //๋”ํ•ด์ง€๋Š” ๊ฐ’
		int i = 0; //while๋ฌธ loop
		
		while(i < value) {
			total = total + hap;
			hap = hap + 1;
			i++;
		}
		System.out.println("์ดํ•ฉ: " + total);
	}
}

 

Q. *์„ ํ™œ์šฉํ•ด ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

public class Hello {

	public static void main(String[] args) 
    
    /*
    ์ถœ๋ ฅ : 
    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********
    **********
    */
    
    
    
	{
		int starCnt = 1; //๋ณ„์˜ ๊ฐœ์ˆ˜
		
		for(int i = 0; i < 10; i++) // ์ค„์— ๋Œ€ํ•œ ๋ฐ˜๋ณต๋ฌธ
		{
			
			for(int j = 0; j < starCnt; j++) //ํ•œ์ค„์— ์žˆ๋Š” ๋ณ„์˜ ๊ฐœ์ˆ˜์— ๋Œ€ํ•œ ๋ฐ˜๋ณต๋ฌธ
			{
				System.out.print("*");
			}
			
			System.out.println(""); // ์ค„๋ฐ”๊ฟˆ
			starCnt = starCnt + 1;
		}
	}
}
public class Hello{
    public static void main(String args[]) {
        
        /*
        ์ถœ๋ ฅ :        
                 *
                **
               ***
              ****
             *****
            ******
           *******
          ********
         *********
        **********
        
        */
        int starCnt = 1;
        
        for(int i = 0; i < 10; i++){ //์ค„์—๋Œ€ํ•œ ๋ฐ˜๋ณต๋ฌธ
            for(int j = 0; j < (10 - starCnt); j++) { //๊ณต๋ฐฑ์—๋Œ€ํ•œ ๋ฐ˜๋ณต๋ฌธ
                System.out.print(" ");
            }
            for(int k = 0; k < starCnt; k++) { // ๋ณ„์—๋Œ€ํ•œ ๋ฐ˜๋ณต๋ฌธ
                System.out.print("*");
            }
            System.out.println("");
            starCnt = starCnt + 1;
        }

    }
}

 

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€