문제

혹시 2007학년도 대학수학능력시험 수리영역 가형 이산수학 30번 문제를 아는가? 여러분은 수능을 치는 수험생의 마음으로 이 문제를 해결해야만 한다.

하지만 우리는 저작권 위반으로 판사님을 뵙고 싶지 않았기 때문에 이 문제를 직접 수록할 수는 없었다. 아래 링크 중 하나를 클릭해서 pdf 파일을 내려받아 가장 마지막 페이지를 보면, 위의 그림과 아주 유사한 문제가 하나 있을 것이다. 여러분은 바로 그 문제를 해결해야만 한다.

문제를 그대로 내면 재미없기 때문에, 우리는 위 그림과 같이 33과 79가 적혀 있던 부분을 하얀색 직사각형으로 가렸다. 그림에서 흐릿하게 보이는 모든 부분은 원래 문제와 다르지 않다.

빈 칸에 들어갈 두 자연수가 주어졌을 때 문제를 해결하는 프로그램을 작성하자.

입력

첫 번째 줄에 테스트 케이스의 수 T (1 ≤ T ≤ 50 000)가 주어진다. 이후 T개의 테스트 케이스가 주어진다. 각 테스트 케이스는 한 줄로 구성되어 있으며, 각 줄에는 두 개의 정수 A와 B (1 ≤ A, B ≤ 1 023, A ≠ B)가 공백을 사이로 두고 주어진다. 이는 첫 번째 빈 칸에는 A를, 두 번째 빈 칸에는 B를 넣었을 때 답을 구하라는 의미이다

출력

각 테스트 케이스에 대해 답을 한 줄에 하나씩 출력한다.

예제 입력 1

3
33 79
9 15
1022 1023

예제 출력 1

40
10
5110

더보기

Solution

#include<stdio.h>
#include<string.h>

int main(void)
{
	int T;

	scanf("%d", &T);

	for(int t=0;t<T;t++)
	{
		int A, B, a=0, b=0, cmp=1, count=0, ans=0;
		char str_a[11]={'\0', }, str_b[11]={'\0', };

		scanf("%d %d", &A, &B);

		while(A>0)
		{
			if(A%2==1)
				a+=cmp;
			A/=2;
			cmp*=10;
		}
		cmp=1;
		while(B>0)
		{
			if(B%2==1)
				b+=cmp;
			B/=2;
			cmp*=10;
		}

		sprintf(str_a,"%d", a);
		sprintf(str_b,"%d", b);
		while(str_a[count]==str_b[count] && count<strlen(str_a) && count<strlen(str_b))
			count++;

		cmp=1;
		while(count>0)
		{
			ans+=cmp*(str_a[--count]-'0');
			cmp*=2;
		}

		printf("%d\n", 10*ans);
	}

	return 0;
}
728x90

문제

태현이는 매일 한양대로 통학하는 학생이다. 태현이가 집에서 나와서부터 건물까지 도착하는 데는 약 1시간 40분이 걸린다. 태현이는 올해부로 3년 째 통학하고 있다. 오늘 태현이는 학교를 가기 위해 1시간 40분 전에 출발했으나, 열차가 지연되어 아직 왕십리까지 오지도 못했다. 평소에도 통학이 지긋지긋했지만 오늘 태현이는 화가 머리 끝까지 났다. 이게 과연 받아들이고 말아야 할 일인가? 하루에 길바닥에 4시간을 버리다니! 매번 학교까지 오는데 에너지를 다 써버려서 수업을 들을 힘도 없다.

열차는 왜 매번 지연되고, 지하철엔 왜 이렇게 사람이 터지는가! 아침 통학길마다 태현이는 평생 사람과 이렇게 가까이 붙어본 적이 없다고 느낀다. 열차를 조금만 증설해 준다면 태현이의 통학길이 조금 편해질 텐데… 하지만 피곤에 쩐 태현이가 할 수 있는 것은 그렇게 많지 않았다.

너무 화가 난 태현이는 통학 동아리를 만들기로 했다. 매일 모여서 통학이 얼마나 힘든지에 대해 이야기하고 서로 위로를 해주는 동아리이다. 태현이에게 이 동아리는 큰 힘이 되었고, 힘든 통학길마다 서로를 격려해주며 하나의 힘이 되어나갔다.

태현이를 비롯한 통학 동아리 회원들을 가엾이 여긴 통학의 신은 자비를 베풀어 통학 동아리 사람 중, 단 한 명에게 학교를 왔다갔다 할 때 순간이동 할 수 있는 권한을 주어 통학의 고통에서 벗어나게 하려한다. 그는 이 행운을 자신이 만든 테스트를 통과한 회원들에게 하사하려 한다. 

사실 통학의 신이 만든 테스트는 아니다. 통학의 신은 요즘 학습지를 풀고 있는데, 수학엔 영 젬병이라 힘들어 한다. 간단한 이차방정식의 근을 구하고 싶은데, 제대로 풀리지 않아 괜히 사람들에게 문제를 내서 학습지를 안 풀고 학습지 선생님께 칭찬을 받으려는 것이다.

태현이는 통학 동아리 회장이지만 이 엄청난 행운을 통해 통학 동아리에서 나가고 싶어한다. 태현이를 도와주자!

입력

x^2  + 2Ax + B = 0 의 두 계수 A, B가 주어진다. A, B는 정수이며, 이 방정식의 근은 항상 정수이다. (-1000 ≤ A, B ≤ 1000)

출력

첫 번째 줄에 방정식의 근들을 모두 공백으로 분리해 오름차순으로 출력한다. 중근일 경우 하나만 출력한다.

예제 입력 1

2 3

예제 출력 1

-3 -1

예제 입력 2

1 -8

예제 출력 2

-4 2

예제 입력 3

1 1

예제 출력 3

-1

더보기

Solution

#include<stdio.h>
#include<math.h>

int main(void)
{
	int A, B;

	scanf("%d %d", &A, &B);

	if(A*A==B)
		printf("%d\n", -A);
	else
		printf("%d %d\n", -A-(int)sqrt(A*A-B), (int)sqrt(A*A-B)-A);

	return 0;
}
728x90

문제

택시 거리는 바둑판 모양의 도로망을 가진 도시에서 점 A에서 B까지의 최단 거리를 구할 경우 도로를 따라서만 가는 가장 짧은 거리를 뜻한다.

위의 사진에서는 빨간색 선이 택시거리이다. 즉, 점 A의 좌표가 (x1, y1)이고 점 B의 좌표를 (x2, y2)라고 했을 때, 두 장소 사이의 택시 거리 D는 다음과 같다.

D=|x2−x1|+|y2−y1|

인접한 0과 0, 0과 1, 1과 1 사이의 거리를 1이라고 할 때, 두 1 사이의 거리를 구하는 프로그램을 작성하시오.

입력

첫 줄엔 문자열의 높이 N과 가로 M이 주어진다. (2 ≤ N, M ≤ 1,000) 이다.

두 번째 줄부터 M개의 숫자 0또는 1이 예제 입력과 같이 N개의 줄에 걸쳐 입력된다.

1는 항상 두 개만 입력된다.

출력

주어진 숫자들에서 1과 1사이의 택시 거리를 구하시오.

예제 입력 1

3 4
1 0 0 0
0 0 0 0
0 0 0 1

예제 출력 1

5

더보기

Solution

#include<stdio.h>
#include<stdbool.h>

int main(void)
{
	int N, M, start[2], end[2], temp;
	bool started=false;

	scanf("%d %d", &N, &M);

	for(int n=0;n<N;n++)
		for(int m=0;m<M;m++)
		{
			scanf("%d", &temp);

			if(temp==1)
			{
				if(!started)
				{
					start[0]=n;
					start[1]=m;
					started=true;
				}
				else
				{
					end[0]=n;
					end[1]=m;
					break;
				}
			}
		}
	for(int i=0;i<2;i++)
	{
		end[i]-=start[i];
		end[i]=end[i]<0?-end[i]:end[i];
	}

	printf("%d\n", end[0]+end[1]);
	return 0;
}
728x90

문제

선린인터넷고등학교의 한 학생은 프로그래밍 대회에 참가하여 거액의 상금을 수상하는 영광을 누리게 되었다. 하지만, 이 학생이 상금 금액의 전부를 수령하게 되는 것은 아니다. 상금의 일부를 제세공과금으로 납부하고, 나머지 금액을 수령하게 된다.

일반적으로, 대회에서 상금을 받으면 전체 금액의 22%를 제세공과금으로 국가에 납부하고, 나머지 금액을 수령하게 된다. 하지만, 상금의 80%를 필요 경비로 인정하게 되면, 나머지 20% 중 22%만을 제세공과금으로 납부해도 된다.

대회 상금의 금액이 주어질 때, 다음 두 경우 각각에 대해 이 학생이 실제로 수령하는 금액을 구해보자.

  1. 전체 상금의 22%를 제세공과금으로 납부하는 경우
  2. 상금의 80%를 필요 경비로 인정받고, 나머지 금액 중 22%만을 제세공과금으로 납부하는 경우

입력

상금의 금액을 나타내는 하나의 정수 N이 주어진다.

출력

1번 경우에 대한 답과 2번 경우에 대한 답을 사이에 공백을 두고 출력한다.

제한

  • 1000≤N≤10000000
  • N 1000의 배수

예제 입력 1

10000000

예제 출력 1

7800000 9560000

예제 입력 2

1000

예제 출력 2

780 956

더보기

Solution

#include<stdio.h>

int main(void)
{
	int N;

	scanf("%d", &N);

	printf("%d %d\n", (N/100)*78, (N/1000)*956);
	return 0;
}
728x90

문제

En route to Rigel 7, Chief Engineer Geordi Laforge and Data were discussing favorite numbers. Geordi exclaimed he preferred Narcissistic Numbers: those numbers whose value is the same as the sum of the digits of that number, where each digit is raised to the power of the number of digits in the number.

Data agreed that Narcissistic Numbers were interesting, but not as good as his favorite: Perfect Numbers. Geordi had never heard of a Perfect Number, so Data elaborated, “A positive integer is said to be Perfect if it is equal to the sum of its positive divisors less than itself. For example, 6 is Perfect because 6 = 1 + 2 + 3.”

Geordi began thinking about an algorithm to determine if a number was Perfect, but did not have the raw computing ability of Data. He needs a program to determine if a given number is Perfect.

Help Geordi write that program.

입력

Input consists of a single entry per line. Each line contains a single positive integer n, where 2 < n < 100000 for each case. A line containing -1 denotes the end of input and should not be processed.

출력

For each case, determine whether or not the number is Perfect. If the number is Perfect, display the sum of its positive divisors less than itself. The ordering of the terms of the sum must be in ascending order. If a number is not Perfect, print “<NUM> is NOT perfect.” where <NUM> is the number in question. There must be a single space between any words, symbols, or numbers in all output, with the exception of the period at the end of the sentence when a number is not perfect.

예제 입력 1

6
12
28
-1

예제 출력 1

6 = 1 + 2 + 3
12 is NOT perfect.
28 = 1 + 2 + 4 + 7 + 14

더보기

Solution

#include<stdio.h>

int main(void)
{
	int n;

	scanf("%d", &n);

	while(n!=-1)
	{
		int sum=0;

		for(int i=1;i*2<=n;i++)
			if(n%i==0)
				sum+=i;

		if(sum==n)
		{
			printf("%d = 1", n);
			for(int i=2;i*2<=n;i++)
				if(n%i==0)
					printf(" + %d", i);
			printf("\n");
		}
		else
			printf("%d is NOT perfect.\n", n);

		scanf("%d", &n);
	}

	return 0;
}
728x90

문제

Farmer John was performing his nightly bedtime reading duties for Bessie. "Oh, this gives me a headache," moaned Bessie. "But it's just simple number theory," replied FJ. "Let's go over it again. The sigma function of a number is just the sum of the divisors of the number. So, for a number like 12, the divisors are 1, 2, 3, 4, 6, and 12. Summing them we get 28." "That's all there is to it?" asked Bessie. "Yep." replied FJ. "Perhaps someone will write a program to calculate the sigma function of an integer I (1 <= I <= 1,000,000)."

입력

A single integer I.

출력

Output a line containing the sum of all of I's divisors.

예제 입력 1

12

예제 출력 1

28

더보기

Solution

#include<stdio.h>

int main(void)
{
	int I, sum=0;

	scanf("%d", &I);

	for(int i=1;i<=I/2;i++)
		if(I%i==0)
			sum+=i;
	sum+=I;

	printf("%d\n", sum);
	return 0;
}
728x90

문제

Suppose we have a number like 12. Let’s define shifting a number to mean adding a zero at the end. For example, if we shift that number once, we get the number 120. If we shift the number again we get the number 1200. We can shift the number as many times as we want.

In this problem you will be calculating a shifty sum, which is the sum of a number and the numbers we get by shifting. Specifically, you will be given the starting number N and a non-negative integer k. You must add together N and all the numbers you get by shifting a total of k times.

For example, the shifty sum when N is 12 and k is 1 is: 12 + 120 = 132. As another example, the shifty sum when N is 12 and k is 3 is 12 + 120 + 1200 + 12000 = 13332.

입력

The first line of input contains the number N (1 ≤ N ≤ 10000). The second line of input contains k, the number of times to shift N (0 ≤ k ≤ 5).

출력

Output the integer which is the shifty sum of N by k.

예제 입력 1

12
3

예제 출력 1

13332

더보기

Solution

#include<stdio.h>

int main(void)
{
	int N, k, cal=1;

	scanf("%d", &N);
	scanf("%d", &k);

	for(int i=0;i<k;i++)
		cal=10*cal+1;

	printf("%d\n", N*cal);
	return 0;
}
728x90

문제

영선이는 길이가 a, b, c인 세 막대를 가지고 있고, 각 막대의 길이를 마음대로 줄일 수 있다.

영선이는 세 막대를 이용해서 아래 조건을 만족하는 삼각형을 만들려고 한다.

  • 각 막대의 길이는 양의 정수이다
  • 세 막대를 이용해서 넓이가 양수인 삼각형을 만들 수 있어야 한다.
  • 삼각형의 둘레를 최대로 해야 한다.

a, b, c가 주어졌을 때, 만들 수 있는 가장 큰 둘레를 구하는 프로그램을 작성하시오. 

입력

첫째 줄에 a, b, c (1 ≤ a, b, c ≤ 100)가 주어진다.

출력

첫째 줄에 만들 수 있는 가장 큰 삼각형의 둘레를 출력한다.

예제 입력 1

1 2 3

예제 출력 1

5

예제 입력 2

2 2 2

예제 출력 2

6

예제 입력 3

1 100 1

예제 출력 3

3

예제 입력 4

41 64 16

예제 출력 4

113

더보기

Solution

#include<stdio.h>

int main(void)
{
	int length[3];

	for(int l=0;l<3;l++)
		scanf("%d", &length[l]);

	for(int i=0;i<2;i++)
		for(int j=i+1;j<3;j++)
			if(length[i]>length[j])
			{
				int temp=length[i];
				length[i]=length[j];
				length[j]=temp;
			}

	printf("%d\n", length[0]+length[1]>length[2]?length[0]+length[1]+length[2]:2*(length[0]+length[1])-1);
	return 0;
}
728x90

+ Recent posts