코드업 기초6. 함수

업데이트:


[1526] 함수로 hello 문자열 출력하기

[입력]
[출력] hello

public class Main {
	public static void main(String[] args) {
		f();
	}
	
	public static void f() {
		System.out.println("hello");
	}
}


[1527] 함수로 123 값 출력하기

[입력]
[출력] 123

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		f();
	}
	public static void f() {
		System.out.println("123");
	}
}


[1528] 함수로 *문자 출력하기

[입력]
[출력] *

public class Main {
	public static void main(String[] args) {
		f();
	}
	public static void f() {
		System.out.println("*");
	}
}


[1529] 함수로 **문자 출력하기

[입력]
[출력] **

public class Main {
	public static void main(String[] args) {
		f();
	}
	public static void f() {
		System.out.println("**");
	}
}


[1531] 함수로 정수(int) 1 리턴하기

[입력]
[출력] 1

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		System.out.println(f());
	}
	public static int f() {
		return 1;
	}
}


[1532] 함수로 정수(long long int) -2147483649 리턴하기

[입력]
[출력] -2147483649

public class Main {
	public static void main(String[] args) {
		System.out.println(f());
	}
	public static long f() {
		return -2147483649L;
	}
}


[1533] 함수로 실수(float) 3.14 리턴하기

[입력]
[출력] -2147483649

public class Main {
	public static void main(String[] args) {
		System.out.printf("%f", f());
	}
	public static float f() {
		return 3.140000f;
	}
}


[1534] 함수로 실수(double) 3.1415926535897 리턴하기

[입력]
[출력] 3.1415926535897

public class Main {
	public static void main(String[] args) {
		System.out.printf("%.13f", f());
	}
	public static double f() {
		return 3.1415926535897;
	}
}


[1535] 함수로 가장 큰 값 위치 리턴하기

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

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.println(f(arr));
		
	}
	public static int f(int[] arr) {
		int max = 0;
		max = arr[0];
		for(int i = 1; i < arr.length; i++) {
			 if(max < arr[i]) {
				 max = arr[i];
			 }
		}
		
		for(int i = 0; i < arr.length; i++) {
			if(max == arr[i]) {
				return i+1;
			}
		}
		return 0;
	}
}


[1536] 함수로 가장 작은 값 리턴하기

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

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.println(f(arr));
		
	}
	public static int f(int[] arr) {
		int min = 0;
		min = arr[0];
		for(int i = 1; i < arr.length; i++) {
			 if(min > arr[i]) {
				 min = arr[i];
			 }
		}
		return min;
	}
}


[1537] 함수로 hello 또는 world 출력하기

[입력] 2
[출력] world

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		
		System.out.println(f(num));
		
	}
	public static String f(int num) {
		String str = null;
		if(num == 1) str = "hello";
		else str = "world";
		return str;
	}
}


[1538] 함수로 odd 또는 even 출력하기

[입력] 2
[출력] even

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		
		System.out.println(f(num));
		
	}
	public static String f(int num) {
		String str = null;
		if(num % 2 == 0) str = "even";
		else str = "odd";
		return str;
	}
}


[1539] 함수로 false 또는 true 출력하기

[입력] -1
[출력] true

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		
		System.out.println(f(num));
		
	}
	public static String f(int num) {
		String str = null;
		if(num == 0) str = "false";
		else str = "true";
		return str;
	}
}


[1540] 함수로 zero 또는 non zero 출력하기

[입력] -1
[출력] non zero

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		
		System.out.println(f(num));
		
	}
	public static String f(int num) {
		String str = null;
		if(num == 0) str = "zero";
		else str = "non zero";
		return str;
	}
}


[1541] 함수로 negative/zero/positive 출력하기

[입력] 2
[출력] positive

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		
		System.out.println(f(num));
		
	}
	public static String f(int num) {
		String str = null;
		if(num == 0) str = "zero";
		else if(num > 0) str = "positive";
		else str = "negative";
		return str;
	}
}


[1542] 함수로 prime 또는 composite 출력하기

[입력] 997
[출력] prime

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


[1543] 함수로 love 출력하기

[입력] 3
[출력]
love
love
love

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 = 0; i < num; i++) {
			System.out.println(f());
		}
	}
	public static String f() {
		return "love";
	}
}


[1544] 함수로 * n개 출력하기

[입력] 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 = 0; i < num; i++) {
			System.out.print(f());
		}
	}
	public static String f() {
		return "*";
	}
}


[1545] 함수로 true(1) / false(0) 리턴하기

[입력] -1
[출력] non zero

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		if(f(num) == true) System.out.println("non zero");
		else System.out.println("zero");
	}

	public static boolean f(int num) {
		boolean yn = false;
		if(num != 0) yn = true ;
		return yn;
	}
}


[1546] 함수로 plus/minus/0 판별하기

[입력] -1
[출력] minus

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		if(zero(num)) System.out.println("zero");
		else System.out.println(plus(num) ? "plus" : "minus");
	
	}
	public static boolean zero(int num) {
		boolean yn = false;
		if(num == 0) yn = true;
		return yn;
	}
	public static boolean plus(int num) {
		boolean yn = false;
		if(num > 0) yn = true;
		return yn;
	}
}


[1547] 함수로 prime/composite 판별하기

[입력] -1
[출력] minus

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


[1548] 함수로 학점 리턴하기

[입력] 89
[출력] B

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int score = sc.nextInt();
		System.out.println(grade(score));
	}
	public static char grade(int score) {
		char judgment  = ' ';
		switch(score/10) {
		case 10 :
		case 9 : judgment = 'A'; break;
		case 8 : judgment = 'B'; break;
		case 7 : judgment = 'C'; break;
		case 6 : judgment = 'D'; break;
		default : judgment = 'F'; break;
		}
		return judgment;
	}
}


태그:

카테고리:

업데이트:

댓글남기기