출처: https://www.acmicpc.net/problem/12779
정보통신처에서는 2016년 6월 4일 인하 광장에서 이벤트를 진행하려고 한다. 정보통신처에서 인하 광장에 올린 게시글에 N번째로 댓글을 단 모든 학생에게 상품을 지급하기로 하였다. 단, N은 약수의 개수가 홀수여야 한다. 인하 광장을 즐겨보는 찬미는 이 이벤트에 참가하기로 하였다. 찬미는 댓글을 작성한 후 자신이 상품을 받을 확률이 얼마나 되는지 궁금해졌다. 찬미가 댓글을 작성하기 전의 총 댓글 수가 a개이고, 댓글을 작성 후의 총 댓글 수가 b개일 때 찬미의 댓글은 a보다 크고 b보다 작거나 같은 범위 안에 존재한다고 한다. 예를 들어 a가 1이고, b가 4인 경우 [2, 3, 4] 중 한 곳에 댓글이 존재한다. 이 중 약수의 개수가 홀수인 숫자는 4, 한 개이므로 상품을 받을 확률은 1/3이다. 찬미를 도와 찬미가 상품을 받을 확률을 구하는 프로그램을 작성하라.
입력
입력의 첫 줄에는 정수 a와 b가 주어진다. (1 ≤ a, b ≤ 2^60) b는 a보다 항상 크다
출력
찬미가 상품을 지급받을 확률을 기약분수 형태로 출력한다. 만약 확률이 0인 경우 0을 출력한다.
예제 입력
1 4
예제 출력
1/3
public class Anything {
public static void main(String[] args) {
Scanner sc = new Scanner(in);
BigDecimal aa = new BigDecimal(sc.next());
BigDecimal bb = new BigDecimal(sc.next());
double aaa = aa.doubleValue();
double bbb = bb.doubleValue();
double ccc = bbb - aaa;
int ddd = (int) Math.sqrt(bbb) - (int) Math.sqrt(aaa);
if (ddd == 0L) {
System.out.println(0);
} else {
System.out.printf("%d / %.0f\n", ddd, ccc);
}
}
}
2017年03月30日 11:34
package coding_2017_5;
/*
* 약수의 갯수가 홀 수일 경우 -> 제곱수
* 그러니까 (sqrt(n))^2 = n이 되는 수를 찾습니다.
* 하지만 1부터 시작해서 a~b 사이를 찾고자 할 때 a가 너무 크면 시간이 오래걸리니까
* sqrt(a)를 해 a와 가장 가까운 제곱수부터 찾기 시작합니다.
*
* 확률의 기약분수는 유클리드 호제법을 이용했습니다.
* (출처 : https://ko.wikipedia.org/wiki/%EC%9C%A0%ED%81%B4%EB%A6%AC%EB%93%9C_%ED%98%B8%EC%A0%9C%EB%B2%95)
*
* 1. 입력으로 두 수 m,n(m>n)이 들어온다.
* 2. n이 0이라면, m을 출력하고 알고리즘을 종료한다.
* 3. n이 m을 나누어떨어지면, n을 출력하고 알고리즘을 종료한다.
* 4. 그렇지 않으면, m을 n으로 나눈 나머지를 새롭게 m에 대입하고, m과 n을 바꾸고 3으로 돌아온다.
*/
public class problem_110 {
private static StringBuffer buffer;
public static void main(String[] args) {
double a = 1, b = 11;
buffer = new StringBuffer();
int power_count = 0;
int i = (int) Math.sqrt(a);
double power = i * i;
for (; power <= b; power = i * (i++)) {
if (power > a && power <= b)
power_count++;
}
if (power_count == 0) {
buffer.append("0");
} else {
double child = power_count;
double parent = b - a;
double gcd = euclidean(parent, child);
parent = parent/gcd;
child = child/gcd;
System.out.println(gcd);
buffer.append(child);
buffer.append("/");
buffer.append(parent);
}
System.out.println(buffer.substring(0));
}
// b >= a
public static double euclidean(double b, double a) {
double r = 0;
double temp = 0;
while (true) {
r = b % a;
if (a == 0)
return b;
else if(r == 0)
return a;
else {
temp = b;
b = a;
a = temp % r;
}
}
}
}
2017年05月19日 17:41
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int a = s.nextInt();
int b = s.nextInt();
int cnt=0;
for(int i=a+1;i<=b;i++) {
if(IsOdd(divisor(i)))
cnt++;
}
System.out.println(cnt+"/"+(b-a));
}
public static int divisor(int n) {
int cnt=0 ;
for(int i=1;i<=n;i++) {
if(n%i==0)
cnt++;
}
return cnt;
}
public static boolean IsOdd(int n) {
boolean tf=false;
if(n%2==1)
tf=true;
return tf;
}
}
자바입니다.
public class itemis {
public static void main(String[] args) {
//단, N은 약수의 개수가 홀수여야 한다.
Scanner sc = new Scanner(System.in);
int a= sc.nextInt();// 댓글 작성전 총 댓글 수
int b = sc.nextInt();//댓글 단 후 총 댓글 수
//찬미 댓글은 a< <=b
int denominator = b-a;
int numerator = 0;
int GD=0;
String result="";
for(int i=a+1;i<=b;i++){
if(divisorCnt(i)%2==1){
numerator++;
};
}
if(numerator==0){
result ="0";
}else{
GD = GD(numerator,denominator);
int denominator2= denominator/GD;
int numerator2 = numerator/GD;
result = String.valueOf(numerator2)+"/"+String.valueOf(denominator2);
}
System.out.println(result);
}
//약수의 갯수 구하는 함수
static int divisorCnt(int x){
int cnt=0;
for(int i=1;i<=x;i++){
if(x%i==0) cnt++;
}
return cnt;
}
//최대공약수로 나눠주는 함수
static int GD(int a, int b){
int tmp=0;
if(a <b){ //b가 크다면 자리바꿈
tmp=b;
b=a;
a=tmp;
}
//이제 항상 a가 b보다 크다.
while(b!=a){
a -=b;
if(b>=a){
while(b!=a){
b -=a;
}
}
}
return a;
}
}
public class test {
public static void main(String[] args) {
Scanner sc = new Scanner(in);
BigDecimal aa = new BigDecimal(sc.next());
BigDecimal bb = new BigDecimal(sc.next());
double aaa = aa.doubleValue();
double bbb = bb.doubleValue();
double ccc = bbb - aaa;
int ddd = (int) Math.sqrt(bbb) - (int) Math.sqrt(aaa);
if (ddd == 0L) {
System.out.println(0);
} else {
System.out.printf("%d / %.0f\n", ddd, ccc);
}
}
}
public class 상품is뭔들_2016_인하대_프로그래밍_경진대회_B번 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int 전체 = b-a;
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int count=0;
for(int i=a+1; i<=b; i++) {
for(int j=1; j<i; j++) {
if(j*j==i) {
count++;
}
}
}
if(count==0) {
bw.write(0);
bw.close();
}
else if(count==1) {
bw.write(count+"/"+전체);
bw.close();
}
else {
for(int i=2; i<=count; i++) {
if(count%i==0&&(전체)%i==0) {
count/=i;
전체/=i;
}
}
bw.write(count+"/"+전체);
bw.close();
}
}
}
버퍼를 사용해서 풀어봤습니다.
package Calculator;
import java.util.Scanner;
import java.util.LinkedList;
import java.util.ListIterator;
public class CalculatorProblem {
static LinkedList<Integer> list;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
list = new LinkedList<>();
CalculatorProblem calc = new CalculatorProblem();
while(true) {
System.out.print("$ Input: ");
int before = scan.nextInt();
int after = scan.nextInt();
// Check the bound.
if(before<1 || before>after || after<1) {
System.out.println("Out of bound.");
continue;
}
// Initialization.
for(int i=before+1; i<=after; i++) {
list.add(i);
}
// Print list.
ListIterator iter = list.listIterator();
calc.printList("Before elimination", iter);
// Removes elements with an even number of divisor.
calc.removeElements(iter);
calc.printList("After elimination", iter);
// Calculate probability with winning.
System.out.printf(" -> Probability: 1/%d\n", list.size());
}
}
// Print the list.
private void printList(String contents, ListIterator iter) {
moveFirst(iter);
System.out.printf(" -> %s: ", contents);
while(iter.hasNext()) {
System.out.printf("%d ", iter.next());
}
System.out.println();
}
// Move front of first node.
private void moveFirst(ListIterator iter) {
while(iter.hasPrevious()) {
iter.previous();
}
}
// Removes elements with an even number of divisor.
private void removeElements(ListIterator iter) {
moveFirst(iter);
while(iter.hasNext()) {
int index = (int)iter.next();
if(isDivisorEven(index)%2==0) {
iter.remove();
}
}
}
// Check elements with an even number of divisor.
private int isDivisorEven(int index) {
int count = 0;
for(int i=1; i<= index; i++) {
if(index%i ==0)
count++;
}
return count;
}
}
리스트로 풀어봤습니다.
풀이 작성
코딩도장은 프로그래밍 문제풀이를 통해서 코딩 실력을 수련(Practice)하는 곳입니다.