728x90

1.ex01

package days10;

import java.util.Scanner;

public class Ex01 {


	public static void main(String args[]){
		Scanner scanner = new Scanner(System.in);
		System.out.print("a와 n을 입력하세요: ");
		int a=scanner.nextInt(); //밑을 정수형으로 받겠다.
		int n = scanner.nextInt(); //지수를 정수형으로 받겠다.
		int result = CalculatePower(a, n); //CalculatePower의 함수에 a, n을 넣어 계산함 값을 result라 하겠다.
		System.out.printf("%d^%d=%d",a, n, result);
	}
	public static int CalculatePower (int num, int pow){ 
		if (pow == 0)	//지수가 0이면 n^0은 무조건 1이므로 조건식으로 1값을 반환하도록 함
			return 1;
		else
			return num * CalculatePower(num, pow - 1); 


	}//main

}

 

2.ex02

package days10;

import java.util.Scanner;

public class Ex02 {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		String rrn;
		rrn=getRRN(scanner); //올바른 주민번호인지 유효성 검사를 하는 함수
		
		int year=Integer.parseInt(rrn.substring(0,2));
		int month=Integer.parseInt(rrn.substring(2, 4));
		int days=Integer.parseInt(rrn.substring(4, 6));
		int gender=Integer.parseInt(rrn.substring(7, 8));
		
		System.out.printf("년도:%d, 월:%d, 일:%d, 성별:%d\n", year, month, days, gender);

	}//main

	public static String getRRN(Scanner scanner) {
		String regex="\\d{6}-\\d{7}";
		String rrn;
		int count=0;
		do {
			if(count!=0) {
				System.out.printf("[%d]번 입력 실패\n 다시", count);
			}
			System.out.println(">주민번호 14자리 입력(예 000000-0000000)");
			rrn=scanner.next();
			count++;
		} while (!rrn.matches(regex));
		//[과제]입력 잘못 5번만 허용한다.(코딩)
		return rrn;
	}


	

}

 

3.ex03

package days10;

public class Ex03 {

	public static void main(String[] args) {
		// 1/2+2/3+3/4+ ... +8/9+9/10= ???
		//for/while 반복문/조건반복문 - (반복규칙)
		
		//분자/부모==i/(i+1)
		double sum=0;
		for (int i = 0; i <= 9; i++) {
			System.out.printf("%d/%d+", i, i+1);
			sum+=(double)i/(i+1);
		}
		System.out.printf("\b=%f\n", sum);

	}

}

 

4.ex04

package days10;

import java.util.Scanner;

public class Ex04 {

	public static void main(String[] args) {
		// 수열:
		//자연수(양의 정수)의 나열
		//[규칙O], 규칙X
		//첫번째 값이 1
		//항(term)+항+항
		//1+2+4+7+11+15 계차함수 	1+{(N^2-N)/2}
		//term 1+2+4+7+11+16
		//increase 1 2 3 4 5...
		//출력되는 각 항의 값을 저장할 변수:term
		
		int term =1;
		int increase=0;//증가치
		int sum=0;
		
		for(int i=1; i<=20; i++) {
			term+=increase;
			sum+=term;
			System.out.printf("%d+", term); 
			increase++;
		}//for문
		System.out.printf("\b=%d\n", sum);
		
		/*
		int term =1;
		int increase=1;//증가치
		
		//첫번째 항
		System.out.printf("%d+", term); //1+
		
		//두번째 항
		term+=increase;
		System.out.printf("%d+", term); //1+2+
		increase++;//2
		
		//세번째 항
		term+=increase;
		System.out.printf("%d+", term); //1+2+
		increase++;//2
		
		term+=increase;
		System.out.printf("%d+", term); //1+2+
		increase++;//2
		
		term+=increase;
		System.out.printf("%d+", term); //1+2+
		increase++;//2
		*/


	}
}
/*
 * int term  ;
      int sum = 0 ;
      for (int i = 1; i <=20; i++) {
          term = (int) (1+(  (Math.pow( i, 2 )-i)/2 ));
          sum  += term;
          System.out.printf("%d+", term);
      }
      System.out.printf("\b=%d\n", sum);
 */
/*
 * int n, first=1, second=0;
		int sum=0;
		Scanner scanner = new Scanner(System.in);
		System.out.println("몇개의합:");
		n=scanner.nextInt();
		for(int i=0; i<n; i++) {
			first=first+second;
			System.out.print(first+" ");
			second++;
		}
 */

 

5.ex04_02

package days10;

public class Ex04_02 {

	public static void main(String[] args) {
		// 피보나치(Fibonacci)수열 : 첫번째, 두번째 -1,1
		//1+1+2+3+5+8+13+21+34+....
		//[문제] <=100

		int firstTerm=1;
		int secondTerm=1;
		int nextTerm;
		int sum=firstTerm+secondTerm; //2

		System.out.printf("%d+%d+",firstTerm, secondTerm);
		
		while(true) { //무한루프를 돌때에는 while(true)를 쓴다! 
			nextTerm=firstTerm+secondTerm;
			if(nextTerm>100) break; //그래서 조건이 100보다 작아야 된다고 했는데, 그래서 100보다 클때 무한루프를 빠져 나와라
			System.out.printf("%d+", nextTerm);
			sum +=nextTerm;
			firstTerm=secondTerm;
			secondTerm=nextTerm;
		}
		System.out.printf("\b=%d\n", sum);
		/*
		//3번째 항
		nextTerm=firstTerm+secondTerm;
		System.out.printf("%d+", nextTerm);
		sum +=nextTerm;
		firstTerm=secondTerm;
		secondTerm=nextTerm;

		//4번째 항
		nextTerm=firstTerm+secondTerm;
		System.out.printf("%d+", nextTerm);
		sum +=nextTerm;
		firstTerm=secondTerm;
		secondTerm=nextTerm;

		//5번째 항
		nextTerm=firstTerm+secondTerm;
		System.out.printf("%d+", nextTerm);
		sum +=nextTerm;
		firstTerm=secondTerm;
		secondTerm=nextTerm;
		*/
		
		

	}

}

 

6.ex05

package days10;

import java.sql.Date;
import java.util.Scanner;

public class Ex05 {

	public static void main(String[] args) {
		//주민등록번호를 입력받아서...
		Scanner scanner = new Scanner(System.in); //870412-1700001
		String rrn = Ex02.getRRN(scanner);
		System.out.println(getGender(rrn)?"남자":"여자");
		//1.생년월일 "1983-04-12"
		System.out.println(getBirth(rrn));
		//2.성별-함수(메서드) 선언 1.기능:주민등록번호를 입력받아서 성별을 구해주는 함수
		//						   2.매개변수(주민번호) 3.리턴값
		//getGender
		
		//3.나이 계산
		//세는 나이 CountingAge-태어나자마자 1살을 먹고, 새해 1월 1일이 되면 1살이 먹는다.
		//만나이 AmericanAge-태어나는 일을 기준으로 1년이 지나야지 1살을 먹는다.
		
		//세는나이==2020(현재연도)-2019(생일연도)-1
		//만나이=2020(현재연도)-2019(생일년도)-(생일이 지나지 않았을 경우-1)
		int Count;
		//4.출생지역
		//5.검증
		

	}//main
	//"1983-04-12"
	public static String getBirth(String rrn) {
		//830412
		//문제)18/19/20 +83
		//성별-1,2,5,6 1900
		//	   3,4,7,8 2000
		//	   0,9	   1800
		String year=rrn.substring(0,2);
		String month=rrn.substring(2,4);
		String day=rrn.substring(4,6);
		
		String gender=rrn.substring(7,8);
		switch(Integer.parseInt(gender)) {
		case 1: case2: case5: case6:
			year="19"+year;
			break;
		case 3: case4: case7: case8:
			year="20"+year;
			break;
		case 0: case9:
			year="18"+year;
			break;
		}
		return String.format("%s-%s-%s", year, month, day);
	}
	
	public static boolean getGender(String rrn) {
		String check = rrn.substring(7, 8);
		if(Integer.parseInt(check)%2!=0) return true;//남자
		else return false; //여자
	}
//	private static int getAmericanAge(String rrn) {
//	      // 만 나이  =  2020(현재년도) - 2019(생일년도)    ( 생일지났지 않았을 경우 -1)       
//	      int americanAge;
//	      //Date d = new Date(); 
//	      //int nowYear = d.getYear() + 1900;  // 2023
//	     // int birthYear =   Integer.parseInt(   getBirth(rrn).substring(0, 4)   );
//	      
//	      //americanAge = nowYear - birthYear;
//	      
//	      // 년도 X
//	      // 현재월 현재일              생일월  생일일
//	      // 현재월 < 생일월                                           생일이 지난것.
//	      // 현재월 == 생일월      &&  현재일 >= 생일일  생일이 지난것
//	      // 그외 생일지나지않는 경우.
//	      
//	   
//	      int nowMonth =   2 ;  // d.getMonth() + 1;  // 1월(0)~12월(11)              2
//	      int birthMonth =  Integer.parseInt( getBirth(rrn).substring(5, 7)  ) ;   //  "1983-01-23"
//	      int nowDay =  8;        // d.getDate();  // 8
//	      int birthDay =  Integer.parseInt( getBirth(rrn).substring(8)  ) ; 
//	      //현재월  < 생일월    ||     ( 현재월 == 생일월  &&  현재일  <  생일일)
//	      //      2          3
//	      /*
//	      if(   (nowMonth < birthMonth) ||   ( nowMonth == birthMonth  &&  nowDay  <  birthDay)) {
//	         americanAge--;
//	      }
//	      */
//	      
//	      int nowMD =  nowMonth * 100 +nowDay ; //   208;
//	         //  "1983-01-  23"           "01-23"          "0123"
//	      int birthMD=  Integer.parseInt(  getBirth(rrn).substring(5).replaceAll("-", "") );   // 123
//	      //if(  nowMD  < birthMD)  americanAge--;
//	      
//	      //return americanAge;
//	   }
}//class

 

7.ex06

package days10;

import java.sql.Date;

public class Ex06 {
	public static void main(String[] args) {
		// 4:05 수업 시작~  
		String rrn  =  "830412-1700001";
		/*
	      [ 주민등록번호 검증 규칙 ]
	      ‘ㅍ’은 주민등록번호에 오류가 없는지 확인하는 검증번호로, 아래와 같은 특수한 규칙으로 만든다.
	      ㅍ = 11-{(2×ㄱ+3×ㄴ+4×ㄷ+5×ㄹ+6×ㅁ+7×ㅂ+8×ㅅ+9×ㅇ+2×ㅈ+3×ㅊ+4×ㅋ+5×ㅌ) mod 11}
	      즉, 소괄호 안에 있는 것을 계산한 값을 11로 나눠서 나온 나머지를 11에서 뺀 값이 ㅍ이다. 
	      (단, 10은 0, 11은 1로 표기한다.)
		 */

		// 올바른 주민등록번호
		// 잘못된 주민등록번호      
		int ㄱ =   Integer.parseInt(  rrn.substring(0, 1) );
		int ㄴ =   Integer.parseInt(  rrn.substring(1, 2) );
		int ㄷ =   Integer.parseInt(  rrn.substring(2, 3) );
		int ㄹ =   Integer.parseInt(  rrn.substring(3, 4) );
		int ㅁ =   Integer.parseInt(  rrn.substring(4, 5) );
		int ㅂ =   Integer.parseInt(  rrn.substring(5, 6) );
		//                                                                      6, 7       -
		int ㅅ =   Integer.parseInt(  rrn.substring(7, 8) );
		int ㅇ =   Integer.parseInt(  rrn.substring(8, 9) );
		int ㅈ =   Integer.parseInt(  rrn.substring(9, 10) );
		int ㅊ =   Integer.parseInt(  rrn.substring(10, 11) );
		int ㅋ =   Integer.parseInt(  rrn.substring(11, 12) );
		int ㅌ =   Integer.parseInt(  rrn.substring(12, 13) );

		int checksum = 11-((2 *ㄱ+3 *ㄴ+4 *ㄷ+5 *ㄹ+6 *ㅁ+7 *ㅂ+8 *ㅅ+9 *ㅇ+2 *ㅈ+3 *ㅊ+4 *ㅋ+5 *ㅌ) % 11);

		if ( checksum == 11 ) {
			checksum = 1;
		}else if( checksum == 10) {
			checksum = 0;
		}

		int ㅍ =   Integer.parseInt(  rrn.substring(13, 14) );
		if ( ㅍ == checksum) {
			System.out.println("올바른 주민등록번호.");
		} else {
			System.out.println("잘못된 주민등록번호.");
		}

	} //int ㅌ = main
}

 

8.ex06_02

package days10;

public class Ex06_02 {
	public static void main(String[] args) {
		// 4:05 수업 시작~  
		String rrn  =  "830412-1700001";
		/*
	      [ 주민등록번호 검증 규칙 ]
	      ‘ㅍ’은 주민등록번호에 오류가 없는지 확인하는 검증번호로, 아래와 같은 특수한 규칙으로 만든다.
	      ㅍ = 11-{(2×ㄱ+3×ㄴ+4×ㄷ+5×ㄹ+6×ㅁ+7×ㅂ+8×ㅅ+9×ㅇ+2×ㅈ+3×ㅊ+4×ㅋ+5×ㅌ) mod 11}
	      즉, 소괄호 안에 있는 것을 계산한 값을 11로 나눠서 나온 나머지를 11에서 뺀 값이 ㅍ이다. 
	      (단, 10은 0, 11은 1로 표기한다.)
		 */

		// 올바른 주민등록번호
		// 잘못된 주민등록번호      
		//		int ㄱ =   Integer.parseInt(  rrn.substring(0, 1) );
		//		int ㄴ =   Integer.parseInt(  rrn.substring(1, 2) );
		//		int ㄷ =   Integer.parseInt(  rrn.substring(2, 3) );
		//		int ㄹ =   Integer.parseInt(  rrn.substring(3, 4) );
		//		int ㅁ =   Integer.parseInt(  rrn.substring(4, 5) );
		//		int ㅂ =   Integer.parseInt(  rrn.substring(5, 6) );
		//		//                                                                      6, 7       -
		//		int ㅅ =   Integer.parseInt(  rrn.substring(7, 8) );
		//		int ㅇ =   Integer.parseInt(  rrn.substring(8, 9) );
		//		int ㅈ =   Integer.parseInt(  rrn.substring(9, 10) );
		//		int ㅊ =   Integer.parseInt(  rrn.substring(10, 11) );
		//		int ㅋ =   Integer.parseInt(  rrn.substring(11, 12) );
		//		int ㅌ =   Integer.parseInt(  rrn.substring(12, 13) );
		int [] m = {2,3,4,5,6,7,0,8,9,2,3,4,5};
		int tot=0;
		for(int i=0; i<=12; i++) {
			if(i==6) continue; //-
			tot+=Integer.parseInt(rrn.substring(i,i+1))*m[i];
		}
		int checksum=11-(tot%11);


		if ( checksum == 11 ) {
			checksum = 1;
		}else if( checksum == 10) {
			checksum = 0;
		}

		int ㅍ =   Integer.parseInt(  rrn.substring(13, 14) );
		if ( ㅍ == checksum) {
			System.out.println("올바른 주민등록번호.");
		} else {
			System.out.println("잘못된 주민등록번호.");
		}

	} //int ㅌ = main
}

 

9.ex06_03

package days10;

public class Ex06_03 {
	public static void main(String[] args) {
		// 4:05 수업 시작~  
		String rrn  =  "830412-1700001";
		/*
	      [ 주민등록번호 검증 규칙 ]
	      ‘ㅍ’은 주민등록번호에 오류가 없는지 확인하는 검증번호로, 아래와 같은 특수한 규칙으로 만든다.
	      ㅍ = 11-{(2×ㄱ+3×ㄴ+4×ㄷ+5×ㄹ+6×ㅁ+7×ㅂ+8×ㅅ+9×ㅇ+2×ㅈ+3×ㅊ+4×ㅋ+5×ㅌ) mod 11}
	      즉, 소괄호 안에 있는 것을 계산한 값을 11로 나눠서 나온 나머지를 11에서 뺀 값이 ㅍ이다. 
	      (단, 10은 0, 11은 1로 표기한다.)
		 */

		// 올바른 주민등록번호
		// 잘못된 주민등록번호      
//		int ㄱ =   Integer.parseInt(  rrn.substring(0, 1) );
//		int ㄴ =   Integer.parseInt(  rrn.substring(1, 2) );
//		int ㄷ =   Integer.parseInt(  rrn.substring(2, 3) );
//		int ㄹ =   Integer.parseInt(  rrn.substring(3, 4) );
//		int ㅁ =   Integer.parseInt(  rrn.substring(4, 5) );
//		int ㅂ =   Integer.parseInt(  rrn.substring(5, 6) );
//		//                                                                      6, 7       -
//		int ㅅ =   Integer.parseInt(  rrn.substring(7, 8) );
//		int ㅇ =   Integer.parseInt(  rrn.substring(8, 9) );
//		int ㅈ =   Integer.parseInt(  rrn.substring(9, 10) );
//		int ㅊ =   Integer.parseInt(  rrn.substring(10, 11) );
//		int ㅋ =   Integer.parseInt(  rrn.substring(11, 12) );
//		int ㅌ =   Integer.parseInt(  rrn.substring(12, 13) );
		int [] m = {2,3,4,5,6,7,0,8,9,2,3,4,5};
		int tot=0;
		for(int i=0; i<=12; i++) {
			if(i==6) continue; //-
			tot+=Integer.parseInt(rrn.substring(i,i+1))*m[i];
		}
		int checksum=11-(tot%11);


		if ( checksum == 11 ) {
			checksum = 1;
		}else if( checksum == 10) {
			checksum = 0;
		}

		int ㅍ =   Integer.parseInt(  rrn.substring(13, 14) );
		if ( ㅍ == checksum) {
			System.out.println("올바른 주민등록번호.");
		} else {
			System.out.println("잘못된 주민등록번호.");
		}

	} //int ㅌ = main
}

 

10.test01

package days10;

import java.util.Scanner;

public class Test01 {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.print("1부터 몇의 합을 원하시나요?:");
		int n = scanner.nextInt();
		int result = CalculateSum(n);
		System.out.printf("1부터 %d까지의 합은 %d입니다.", n, result);

	}
	public static int CalculateSum(int num){
		if(num==1) return 1;
		else return num + CalculateSum(num-1);
	}
}

 

11.test02)

package days10;

import java.util.Random;

public class Test02 {



	public static void main(String[] args) {

		int [] lotto = new int[6];

		fillLotto( lotto );

		dispLotto( lotto );

	}
	public static void fillLotto(int[] lotto) {
		int index = 0 , n;
		Random rnd = new Random(); 

		lotto[index] = rnd.nextInt(45)+1; //lotto[index++] = rnd.nextInt(45)+1;
		index++; 


		while( index <= 5 ) {
			n  = rnd.nextInt(45)+1;
			if( !isDuplicateLotto(  lotto, index, n  ) ) {
				lotto[index++] = n;
			} 
		}  


	}
	public static boolean isDuplicateLotto(int[] lotto, int index, int n) {
		for (int i = 0; i < index; i++) {
			if( lotto[i] == n) {
				return true; 
			}
		} 
		return false; 
	}
	public static void dispLotto(int[] lotto) {
		for (int i = 0; i < lotto.length; i++) {
			System.out.printf("[%d]", lotto[i]);
		}
		System.out.println(); 
	}
}
728x90