출처: 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
``````{.cs}
using System;
class Solution
{
static void Main(String[] args)
{
string[] str = Console.ReadLine().Split(' ');
int[] arr = new int[str.Length];
for (int i = 0; i < arr.Length; i++)
arr[i] = int.Parse(str[i]);
int NumOfsummary = 0;
for(int i = arr[0] + 1; i <= arr[1]; i++)
{
int count = 0;
for(int j = 1; j <= i; j++)
{
if(i % j == 0) //약수이면 카운트
{
count++;
}
}
if(count % 2 == 1) //약수의 갯수가 홀수라면
{
NumOfsummary++;
}
}
Console.WriteLine(NumOfsummary);
int s = arr[1] - arr[0]; //분모에 쓸 수 (총 댓글수)
//최대 공약수 구해서 기약분수로 나타내기
for (int i = arr[1] - 1; i > 1; i--)
{
if(NumOfsummary % i == 0 && arr[1] % i == 0)
{
NumOfsummary /= i;
s /= i;
}
}
Console.WriteLine("{0} / {1}",NumOfsummary , s);
}
}
using System;
using System.Collections.Generic;
namespace CSharp_codingTest
{
class Program
{
static void Main(string[] args)
{
int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
int hcount = 0; //약수의 갯수가 홀수인 수의 갯수
//a + 1에서 b사이의 수중에서 약수의 갯수가 홀수인 수의 확률 구하기
for(int i = a + 1; i <= b; i++)
{
int count = 0; //약수의 갯수 count
for(int j = 1; j <= i; j++)
{
if(i % j == 0)
{
count++;
}
}
if (count % 2 == 1) //약수의 갯수가 홀수라면
{
Console.WriteLine("Debug : " + i); //디버깅
hcount++;
}
}
if (hcount == 0)
Console.WriteLine("0");
else
{
//분모 분자로 쓸 변수
int m = b - a;
int n = hcount;
//기약분수로 바꾸기
for(int i = n; i > 1; i--)
{
if (m % i == 0 && n % i == 0)
{
m /= i;
n /= i;
if (i == n)
break;
}
}
Console.WriteLine(m + " / " + n);
}
}
}
}
using System;
using System.Collections.Generic;
namespace FirstProgram
{
class Program
{
static int divisor(int n)
{
int get = 0;
for(int i=1; i<=n; i++)
{
if (n % i == 0)
get++;
}
return get;
}
static void Main(string[] args)
{
string[] str = Console.ReadLine().Split(' ');
List<int> arr = new List<int>();
int oddCount = 0;
for(int i=int.Parse(str[0])+1; i<=int.Parse(str[1]); i++)
{
arr.Add(i);
}
for(int i=0; i<arr.Count; i++)
{
if (divisor(arr[i]) % 2 == 1)
oddCount++;
}
Console.WriteLine(oddCount + "/" + arr.Count);
}
}
}
C#
풀이 작성
코딩도장은 프로그래밍 문제풀이를 통해서 코딩 실력을 수련(Practice)하는 곳입니다.