A씨는 게시판 프로그램을 작성하고 있다.
A씨는 게시물의 총 건수와 한 페이지에 보여줄 게시물수를 입력으로 주었을 때 총 페이지수를 리턴하는 프로그램이 필요하다고 한다.
입력 : 총건수(m), 한페이지에 보여줄 게시물수(n) (단 n은 1보다 크거나 같다. n >= 1)
출력 : 총페이지수
A씨가 필요한 프로그램을 작성하시오.
예) 프로그램 수행 시 다음과 같은 결과값이 나와야 함.
| m | n | 출력 |
|---|---|---|
| 0 | 1 | 0 |
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 1 | 10 | 1 |
| 10 | 10 | 1 |
| 11 | 10 | 2 |
자바로 풀어봤습니다.
package Level_1;
import java.util.Scanner;
public class Board {
public static void main(String[] args) {
int total, post, page; // total(게시물의 총합), post(한 페이지당 보여줄 게시물 갯수), page(게시판의 페이지 갯수)
Scanner input = new Scanner(System.in); // total과 post를 입력받기 위한 명령어.
System.out.print("게시물이 몇 건 올라왔는지 입력하시오: "); // 게시물의 총합을 입력받음
total = input.nextInt();
System.out.print("한 페이지당 게시물을 몇 건 담을지 입력하시오: "); // 한 페이지당 올릴 게시물의 갯수를 입력받음
post = input.nextInt();
if(total % post == 0) // 게시물과 페이지당 게시물의 갯수를 나눈 값이 일치할 경우
System.out.printf("필요한 게시판 페이지 갯수: %d\n", total/post);
else // 일치하지 않을 경우
System.out.printf("필요한 게시판 페이지 갯수: %d\n", (total/post)+1);
}
}
import java.util.Scanner;
public class paging {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("총 게시글 수 ");
int m = sc.nextInt();
System.out.println("페이지당 게시글 수 ");
int n = sc.nextInt();
if (m % n == 0)
System.out.println(m / n + " 페이지 필요");
else
System.out.println(m / n + 1 + " 페이지 필요");
}
}
자바입니다. 위에는 입력부 아래는 계산입니다.
package algorithms.level1;
import java.util.Scanner;
public class CountPageBoard {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int totalCount = scanner.nextInt();
int page = scanner.nextInt();
if(page < 1) return;
int totalPages = getTotalPages(totalCount, page);
System.out.println(totalPages);
}
private static int getTotalPages(int totalCount, int page) {
int result = 0;
result = totalCount / page;
double quotient = totalCount % page;
if(quotient >= 1) result += quotient;
return result;
}
}
자바로 올림을 사용하여 구현했습니다~
public static void main(String[] args) {
int m = Integer.parseInt(args[0]); //전 게시물 갯수
int n = Integer.parseInt(args[1]); //한페이지에 보여줄 게시물 갯수
int page = 0;
if (m > 0 && n > 0)
page = (int) Math.ceil((double) m / n);
System.out.println(page);
}
2014年08月12日 21:32
public class PagingMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(getPage(0, 1));
System.out.println(getPage(1, 1));
System.out.println(getPage(2, 1));
System.out.println(getPage(1, 10));
System.out.println(getPage(10, 10));
System.out.println(getPage(11, 10));
}
public static long getPage(long totalCnt, long pageRow) {
if (pageRow < 1)
return 0;
long cnt = (long) (totalCnt / pageRow + Math.ceil(totalCnt % pageRow));
return cnt;
}
}
2014年11月14日 21:47
package board_page_count;
import java.util.Scanner; //표준입력필수1
public class countpage {
public static void main(String[] args)
{
//m = num_all_posts , n = num_per_page, output = num_of_all_pages;
Integer m , n , output;
Scanner scanner = new Scanner(System.in); //표준입력필수2
System.out.print("총 게시물 수를 입력하세요: ");
m = scanner.nextInt();
System.out.print("한 페이지에 보여줄 게시물 수를 입력해주세요: ");
n = scanner.nextInt();
output = m/n;
//나머지가 남는다면 1페이지 할당해야함
if( m%n > 0 ){
output++;
}
System.out.print("한 페이지당 보여질 게시물 수: " + output);
}
}
public static int sizeOfPage(int m, int n){
int result;
result = m / n;
if (m % n != 0)
result += 1;
return result;
}
2015年03月07日 15:16
풀이 작성