<Bubble Sort>

==Console==

<주민번호 유효성 검사>

<주민번호 검사 로직>

 

주민번호 검사 로직 )

 

1. 주민번호 13자리중 마지막 자리를 제외하고, 앞에서부터 차례로 [2,3,4,5,6,7,8,9,2,3,4,5]를 각자리에 곱하기.

 

0 0 0 0 0 0 - 0 0 0 0 0 0         0 <=주민번호 마지막자리 숫자, 체크코드

2 3 4 5 6 7 - 8 9 2 3 4 5

 

2. 곱한 각 자리의 수를 모두 더하기.

 

3. 모두 더한 수를 11로 나눈 나머지 구하기.

   ? % 11 -> ?

4. 11 에서 구한 나머지를 빼기.

   단 구한값이 2자리가 되었을 경우(ex. 10, 11)에는 10을 나눈 나머지 값 혹은 10을 빼주어서

   주민번호의 마지막 자리의 숫자와 비교할 숫자값을 구한다.

  11 - ?   -> ?

5. 1~4번을 통해 계산한 값을 주민번호 마지막 자리수와 비교하여 같으면 유효한 주민등록번호이며, 다르면 잘못된 번호이다.

 

==Console==주민등록이 노출이 될수있어 일부러 오류를 냅니다

<2차원 배열>

==Console==

==Console==

 

==Console==

==Console==

<배열을 이용하여 정렬>

==Console==

<Select Sort>

 

==Console==

<성적순으로 나열하기>

//똑같은 것을 퍼가실수 있게 소스코드로 한것임(위에 보시는것 성적순)

package com.day5;

import java.util.Scanner;

public class Test3 {

	public static void main(String[] args) {
		
			//10명 이내의 이름과 점수를 입력받아 점수가 높은 사람에서 낮은 사람 순으로 출력
		
		Scanner sc = new Scanner(System.in);
		// Scanner의 객체를 생성함.//메모리를 할당함 등등...
		
		int i,j,inwon,temp1;
		String temp2;
				
		int[] score;
		String[] name;
		
		do{
			System.out.print("인원수[1~10]? ");//3
			inwon = sc.nextInt();
		}while(inwon<1 || inwon>10);
		
		//객체 생성(메모리 할당)
		score = new int[inwon];
		name = new String[inwon];
		
		//인원수 만큼 이름과 점수 입력
		
		for(i=0;i<inwon;i++){
			
	 		System.out.print((i+1)+"번째 이름?");
			name[i] = sc.next();
			
			System.out.print("점수?");
			score[i] = sc.nextInt();
			
		}	
		
		//정렬
		
		for(i=0;i<inwon-1;i++){
			for(j=i+1;j<inwon;j++){
				
				if(score[i]<score[j]){
					
					temp1 = score[i];
					score[i] = score[j];
					score[j] = temp1;
					
					temp2 = name[i];
					name[i] = name[j];
					name[j] = temp2;
					
				}
			}
		}
		
		//출력
		for(i=0;i<inwon;i++){
		
			System.out.printf("%6s %4d\n",name[i],score[i]);
			
		}
	
		sc.close();
	}

}

==Console==

<로또>

//소스코드

package com.day5;

import java.util.Random;

public class Test4 {

	public static void main(String[] args) {
		
		//1~45까지의 수 중 6개의 난수를 발생시켜 크기 순으로 출력
		
		Random rd = new Random(); //copy의 개념
		
		int[] num = new int[6];
		
		int i,j,su,temp;
		
		su = 0;
		
		// 반복의 횟수를 알 수 없을 때 사용
		while(su<6){
			
			num[su] = rd.nextInt(45)+1; //nextInt(45) : 0에서 44번까지의 45개 숫자를 꺼냄
			
			for(i=0;i<su;i++){
				if(num[i]==num[su]){
					su--;
					break;
				}
			}
			
			su++;

		}
		
		//정렬
		
		for(i=0;i<num.length-1;i++){
			for(j=i+1;j<num.length;j++){
				if(num[i]>num[j]){
					temp = num[i];
					num[i] = num[j];
					num[j] = temp;
											
				}
				
			}
		}
		
		
		//출력
		for(int n : num){
			System.out.printf("%4d",n);
		}
		
		System.out.println();

	}

}

==Console==난수를 발생시켜 로또번호가 나옴

<성적으로 석차를 구하기>

//소스코드

package com.day5;

import java.util.Scanner;

public class Test5 {

	public static void main(String[] args) {
		
			//10명 이내의 이름과 점수를 입력받아 석차(rank)를 구하시오.
		    //석차가 높은 사람에서 낮은 사람 순으로 출력
		
		Scanner sc = new Scanner(System.in);
		
		int i,j,inwon;
				
		int[] score;
		String[] name;
		int[] rank;
		
		do{
			System.out.print("인원수[1~10]? ");//3
			inwon = sc.nextInt();
		}while(inwon<1 || inwon>10);
		
		//객체 생성(메모리 할당)
		score = new int[inwon];
		name = new String[inwon];
		rank = new int[inwon];
		//인원수 만큼 이름과 점수 입력
		
		for(i=0;i<inwon;i++){
			rank[i]++;
		}
		
		for(i=0;i<inwon;i++){
			
	 		System.out.print((i+1)+"번째 이름?");
			name[i] = sc.next();
			
			System.out.print("점수?");
			score[i] = sc.nextInt();
			
		}	
		
		//석차계산
		
		for(i=0;i<inwon-1;i++){
			for(j=i+1;j<inwon;j++){
				if(score[i]>score[j]){
					rank[j]++;
				}else if(score[i]<score[j]){
					rank[i]++;
				}
			}
		}
		
		
		//출력
		for(i=0;i<inwon;i++){
		
			System.out.printf("%6s %4d %4d\n",name[i],score[i],rank[i]);
			
		}
	
		sc.close();
	}

}

==Console==

<앞의 방법과 다르게 1등 순서대로 나오게 하기>

package com.day5;

import java.util.Scanner;

public class Test6 {

	public static void main(String[] args) {
		
			//10명 이내의 이름과 점수를 입력받아 석차(rank)를 구하시오.
			//석차가 높은 사람에서 낮은 사람 순으로 출력
		
		Scanner sc = new Scanner(System.in);
		
		int i,j,inwon,temp1;
		String temp2;
				
		int[] score;
		String[] name;
		int[] rank;
		
		do{
			System.out.print("인원수[1~10]? ");//3
			inwon = sc.nextInt();
		}while(inwon<1 || inwon>10);
		
		//객체 생성(메모리 할당)
		score = new int[inwon];
		name = new String[inwon];
		rank = new int[inwon];
		
		for(i=0;i<inwon;i++){
			rank[i] = 1;
		}
		
		//인원수 만큼 이름과 점수 입력
		
		for(i=0;i<inwon;i++){
			
	 		System.out.print("이름?");
			name[i] = sc.next();
			
			System.out.print("점수?");
			score[i] = sc.nextInt();
			
		}	
		
		//정렬
		
		for(i=0;i<inwon-1;i++){
			for(j=i+1;j<inwon;j++){
				
				if(score[i]<score[j]){
					
					temp1 = score[i];
					score[i] = score[j];
					score[j] = temp1;
					
					temp2 = name[i];
					name[i] = name[j];
					name[j] = temp2;
										
				}
			}
		}
		
		for(i=0;i<inwon-1;i++){
			for(j=i+1;j<inwon;j++){
				
				if(score[i]>score[j]){
					
					rank[j] += 1;
				}
			}
		}
		
		//출력
		
		
		for(i=0;i<inwon;i++){
		
			System.out.printf("%6s %4d %4d등\n",name[i],score[i],rank[i]);
			
		}
	
		sc.close();
	}

}

==Console==

<입력된 값에서 가장 큰수와 작은 수 >

//소스

package com.day5;

import java.util.Scanner;

public class Test7 {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		int i,max=0,min=0;
		int num[] = new int[5];
		
		System.out.println("5개의 정수를 입력하시오. ");
		for(i=0;i<num.length;i++){
			num[i] = sc.nextInt();
		}
		
		max = num[0];
		min = num[0];
			
		for(i=0;i<num.length;i++){
			if(max<num[i]){
				max = num[i];
			}
			if(min>num[i]){
				min = num[i];
			}
		}

		System.out.printf("가장 큰수 : %d, 가장 작은 수 : %d",max,min);
		
		sc.close();
		
	}

}

==Console==

==소스==

package com.day5;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;
public class Test9 {
	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		
		Random rd = new Random();
		
		int num=0, nansu;
		char ch;
		String[] str = {"가위","바위","보"}; 	
		
		
		System.out.println("---------가위바위보 게임---------");
		
		while(true){
			nansu = rd.nextInt(3);
			
			do{
				System.out.println("1: 가위, 2: 바위, 3: 보");
				System.out.println("너는 어떤거 낼래? ");
				num = sc.nextInt();
			}while(num<1||num>3);
				
			num -= 1;
			System.out.printf("컴퓨터 : %s, 사람 : %s\n"
			,str[nansu],str[num]);
			System.out.println("-------------------------------");
			
			if(nansu==num){
				System.out.println("컴퓨터와 비겼습니다.");
			}else if((num+2)%3==nansu){
				System.out.println("당신이 이겼습니다.");
			}else{
				System.out.println("컴퓨터가 이겼습니다..");
			}
			
			System.out.println("계속 하시겠습니까? ");
			ch = (char)System.in.read();
			if(ch != 'Y' && ch!= 'y'){
				break;
			}
			System.out.println("----------------------------------------");
		}
		
		sc.close();
	}
}

==Console==

<공포의 별찍기>

//똑같은 코드입니다 퍼가실수 있게 해놓았습니다.

package com.day4;

public class Test1 {

	public static void main(String[] args) {

		int i,j;
		
/*
		for(i=1;i<=5;i++){
			
			for(j=1;j<=5-i;j++){
				
				System.out.print(" ");//공백한칸				
								
			}
				
			for(j=1;j<=i;j++){
				
				System.out.print("*");
			}
			
			System.out.println();//줄바꿈
			
		}
		
	}

*/

/*
 		for(i=1;i<=5;i++){
			
			for(j=1;j<=5-i;j++){
				
				System.out.print(" ");//공백한칸				
								
			}
				
			for(j=1;j<=i*2-1;j++){
				
				System.out.print("*");
			}
			
			System.out.println();//줄바꿈
			
		}
		
	}
	*/
/*		
 		for(i=5;i>=1;i--){
			         //i=i-1
 			
			for(j=1;j<=5-i;j++){
				
				System.out.print(" ");//공백한칸				
								
			}
				
			for(j=1;j<=i*2-1;j++){
				
				System.out.print("*");
			}
			
			System.out.println();//줄바꿈
			
		}
		
	}
} // 영역은 중괄호를 더블클릭하면 알 수 있음
		*/
		
		for(i=5;i>=1;i--){
			
			for(j=1;j<=5-i;j++){
				
				System.out.print(" ");
				
				}
			
			for(j=1;j<=i * 2 -1;j++){ // j(10)<=9
				
				System.out.print("*");
				
			}
			
			System.out.println();
			
		}
		
		for(i=2;i<=5;i++){
			
			for(j=1;j<=5-i;j++){
				
				System.out.print(" ");
				
			}
			
			for(j=1;j<=i * 2 -1;j++){
				
				System.out.print("*");
				
			}
			
			System.out.println();
			
		}
		
	}
}

 

==Console==

==Console==

 

==Console==

 

<계산기>

==Console==

<배열>

 

==추가 설명 ==

// 명령어 ~readLine(): ()로 사용해야 함. // 배열의 length만 ()를 사용하지 않음.

==Console==

 

<달력만들기>

//달력만들기 소스

package com.day4;

import java.util.Scanner;

public class Test5 {

	public static void main(String[] args) {

		
		//만년 달력
				
		Scanner sc = new Scanner(System.in);	
		
		int months[] = {31,28,31,30,31,30,31,31,30,31,30,31}; // 초기화 및 방 형성
		int y,m,nalsu,i,week;
		
		do{
			System.out.print("년도? ");//2018
			y=sc.nextInt();
		}while(y<1);
		
		do{
			System.out.print("월은? ");//12
			m=sc.nextInt();
		}while(m<1||m>12);
		
		//윤년에 따른 2월의 날수 계산
		if(y%4==0 && y%100!=0 ||y%400==0){
			months[1] = 29;
		}
		
		//1년 1월 1일부터 y-1년 12월 31일까지의 날수
		
		nalsu = (y-1) * 365 + (y-1)/4-(y-1)/100+(y-1)/400;

/*		
		int yy = (y-1)/4-(y-1)/100+(y-1)/400;
		System.out.println(nalsu);
		System.out.println(yy);
*/
		
		//월   : 1  2  3  4  5  6  7  8  9  10 11 12
		//배열 : 31,28,31,30,31,30,31,31,30,31,30,31
		//idx  : 0  1  2  3  4  5  6  7  8  9  10 11
		for(i=0;i<(m-1);i++){
			
			//nalsu = nalsu + months[i];
			nalsu += months[i];
			
		}
						
		//System.out.println(nalsu);
	
		//1년 1월 1일부터 y년 m월 1일까지의 날수
		nalsu += 1; //nalsu = nalsu + 1;
		
		//y년 m월 1일의 주의 수 계산
		
		week = nalsu%7;
		
		//System.out.println(week);
		
		System.out.println("            " + y +"-"+ m);
		
		System.out.println("\n  일  월  화  수  목  금  토");
		System.out.println("------------------------------");
		
		//공백지정
		for(i=0;i<week;i++){
			System.out.print("    ");//공백4칸
		}
		
		//월   : 1  2  3  4  5  6  7  8  9  10 11 12
		//배열 : 31,28,31,30,31,30,31,31,30,31,30,31
		//idx  : 0  1  2  3  4  5  6  7  8  9  10 11
		//해당 월의 날짜 출력
		for(i=1;i<=months[m-1];i++){
			
			System.out.printf("%4d",i);//%4d : 자릿수 4자리(4byte) 만들어줌.
			
			week++;
			if(week%7==0)
				System.out.println();
						
		}
		if(week%7!=0)
			System.out.println();
		
		System.out.println("------------------------------");

		sc.close();
		
	}
	
}

==Console==

<사용자에게 입력받아 요일까지 출력하기>

//소스코드

package com.day4;

import java.util.Scanner;

public class Test6 {

	public static void main(String[] args) {

		//년,월,일을 입력받아 요일을 출력하시오.
		//2018년 12월 3일 월요일
		
		Scanner sc = new Scanner(System.in);
		
		int months[] = {31,28,31,30,31,30,31,31,30,31,30,31};
		int y,m,d,week,nalsu,i;
		char ch[] = {'일','월','화','수','목','금','토'};
				
		do{
			System.out.println("년도를 입력하시오. ");
			y = sc.nextInt();
		}while(y<1);
		
		do{
			System.out.println("월을 입력하시오. ");
			m = sc.nextInt();
		}while(m<1 || m>12);
		
		do{
			System.out.println("일을 입력하시오. ");
			d = sc.nextInt();
		}while(d<1 || d>months[m-1]);
		
		if(y%4 ==0 && y%100!=0 || y%400==0){
			months[1] = 29;
		}
		
		nalsu =  (y-1) * 365 + (y-1)/4-(y-1)/100+(y-1)/400;
		
		for(i=0;i<(m-1);i++){
			
			nalsu += months[i];
			
		}
		
		nalsu += d;
			
		week = nalsu%7;
		
		System.out.println(y + "년 " + m + "월 " + d + "일 " + ch[week]+"요일");
		
		sc.close();
	}

}

 

==Console==

<구구단>

//똑같은 코드입니다. 가져가실수 있게 해 놓았습니다.

package com.day3;

import java.util.Scanner;


class Test1 {

	public static void main(String[] args) {

		//반복문(for,while,do~while)

		//for : 시작값과 끝값이 정해져 있을 때 사용
		//while : 끝값이 정해져 있지 않거나 끝값을 모를 때 사용
		//do~while : 일단 실행해 봐야 할 때 사용(사용 후 재실행을 고민해 봐야할 때 사용)

		Scanner sc = new Scanner(System.in);

		int dan;

		System.out.print("단을 입력하시오. "); //7
		dan = sc.nextInt();

		//for(시작값;최대값;증가값)

		for (int i=1;i<=9;i++) {

			System.out.println(dan + " * " + i + " = " + (dan*i));

		}

		System.out.println("--------------------------------------");

		//while(조건)

		int j=0; //while문은 초기값을 0으로 시작함.
		while(j<9) {
			// 조건 작성 시 = 을 잘 안씀
			j++;
			System.out.println(dan + " * " + j + " = " + (dan*j));

		}

		System.out.println("---------------------------------------");

		//do{~}while(조건문);

		int k=0;
		do	{

			k++;
			System.out.println(dan + " * " + k + " = " + (dan*k));

		}
		while (k<9);
		// 1번 실행 후 조건 만족 결과 확인

		System.out.println("---------------------------------------");

		/*		//무한루프
		while (true) {

			System.out.println("나 돌아간다~~");

		}

		 */

		sc.close();
		// stream 안의 데이터를 제거하기 위해 사용(close로 닫아주거나 null로 초기화 시켜줘야 함.)
	}
}

 


==Console==

<입력된 수까지의 합을 구하자!>

==추가 설명==

//InputStreamReader : System.in으로 입력받은 1byte자료를 2byte로 전환해줌

//readLine은 엔터(e) 앞까지 읽어들임

//false가 되어야 do while 조건문에서 빠져나감

////ye 시 System.in.read에서 y만 읽어 냄 e가 남은 상태에서 위의 수입력으로 이동
//System.out.print("수 입력? "); //e 가 입력되어 있는 상태에서 숫자 입력 상태가 됨
//su = Integer.parseInt(br.readLine()); -> e 앞부분만 입력되기 때문에 읽혀진 것은 null

// skip(2->이유 : 1. BufferedReader는 공백을 못 읽음. 2. 엔터의 아스키값(10,13) 때문에)

 

==Console==

<다중 for문을 이용한 구구단>

==Console==

이렇게 해서 9단까지 올라갑니다.

'Java' 카테고리의 다른 글

Java Day5 : 배열,난수(로또)  (0) 2019.06.12
Java Day4 : 반복문을 통해 별찍기,배열,만년달력  (0) 2019.06.11
Java Day1 : Method 메소드  (0) 2019.06.06
Java Day2 : 삼항연산자,조건문(if)  (0) 2019.06.03
Day13 List 1  (0) 2019.02.02

 

이렇게 바뀌신것을 볼수 있습니다.

'Dev > Git' 카테고리의 다른 글

Day 5 : Git 로그(Log) 다루기  (0) 2019.06.17
Day4 : Git 브랜치(Branch)의 개요 및 사용해보기  (0) 2019.06.10
GitHub Day2 : GitHub 이론  (0) 2019.06.09
GitHub Day1 : GitHub 기본파일 올리기,다운  (0) 2019.06.05
GitHub 첫걸음!  (0) 2019.06.04

이부분을 풀어주세요

 

 

선택정렬의 한계 : 시간이 효율적이지 않다!

 

문제1 : 다음의 숫자들을 오름차순으로 정렬하는 프로그램을 작성하세요.

1 10 5 8 7 6 4 3 2 9

 

가장 작은 것을 선택해서 제일 앞으로 보내는 알고리즘 == 선택 알고리즘

1 2 5 8 7 6 4 3 10 9

1 2 3 8 7 6 4 5 10 9

1 2 3 4 7 6 8 5 10 9

1 2 3 4 5 6 8 7 10 9

1 2 3 4 5 6 7 8 10 9

 

=>1 2 3 4 5 6 7 8 9 10

 

 

  • 선택 정렬의 시간복잡도 O(N^2)

 

아까 문제를 확인해 볼때

10 + 9 + + …+ 1

=> 10*(10+1)/2

=> 55번의 비교연산을 한 것임

=> N *( N+1) / 2

=>O(N*N)

 

특정 알고리즘의 연산횟수를 나타낸다.

 

'알고리즘' 카테고리의 다른 글

[스택/큐 : 기능개발]  (0) 2019.11.08
위장  (0) 2019.11.06
전화번호 목록  (0) 2019.11.05
완주하지 못한 선수  (0) 2019.11.04
Day1 - 알고리즘의 개요와 실습환경 구축  (0) 2019.06.06

참고 : https://youtu.be/qQ5iLNjpxSk

* 참고

 

'알고리즘' 카테고리의 다른 글

[스택/큐 : 기능개발]  (0) 2019.11.08
위장  (0) 2019.11.06
전화번호 목록  (0) 2019.11.05
완주하지 못한 선수  (0) 2019.11.04
Day2 : 정렬알고리즘의 개요와 선택정렬 (Selection Sort)  (0) 2019.06.07

+ Recent posts