1. 다음과같이 출력되는 프로그램을 작성하시오.
==== 다음 ===
출력 문자수 입력 : 13
12345678910123
*
***
*****
*******
*********
***********
*************
계속하려면 아무 키나 누르십시오 . . .
== Source
#include <stdafx.h>
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
int main ()
{
cout << "출력 문자수 입력 : ";
int num;
cin >> num;
int halfnum = num / 2;
for (int i = 0; i < num; i++)
cout << ((i % 10) + 1);
cout << endl;
for (int i = 0; i < (halfnum + 1); i++)
{
for (int j = 0; j < num; j++)
{
if ((j >= (halfnum - i)) && (j <= (halfnum + i)))
{
cout << "*";
}
else
{
cout << " ";
}
}
cout << endl;
}
cout << endl;
//system("pause");
return 0;
}
2. 다음과같이 출력되는 프로그램을 작성하시오.
==== 다음 ===
출력 문자수 입력 : 11
123456789101
***********
*********
*******
*****
***
*
계속하려면 아무 키나 누르십시오 . . .
== Source
#include <stdafx.h>
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
int main ()
{
cout << "출력 문자수 입력 : ";
int num;
cin >> num;
int halfnum = num / 2;
for (int i = 0; i < num; i++)
cout << ((i % 10) + 1);
cout << endl;
for (int i = 0; i < (halfnum + 1); i++)
{
for (int j = 0; j < num; j++)
{
if ((j >= (halfnum - (halfnum - i))) && (j <= (halfnum + (halfnum - i))))
{
cout << "*";
}
else
{
cout << " ";
}
}
cout << endl;
}
//system("pause");
return 0;
}
3. 2부터 N까지의 소수와 소수의 합을 구하는 프로그램을 작성하시오.
== Source
#include <stdafx.h>
#include <stdio.h>
int main()
{
int i,j,k,sum=0;
printf("입력: ");
scanf("%d", &k);
printf("\n");
for(i=2;i<=k;i++) // 2에서 100까지
{
for(j=2;j<i;j++) if(i%j==0) break; // 2부터 자신까지 나누어서 나누어 떨어지면 확인 끝
if(i==j) // 자신의 수로만 나누어 떨어지면 소수
{
printf("%d ",i); // 소수를 출력하고
sum=sum+i; // 합계에 누적
}
}
printf("\n합계 : %d\n",sum);
return 0;
}
== Result
입력: 200
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107
109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199
합계 : 4227
계속하려면 아무 키나 누르십시오 . . .
4.정수형 배열(jungsu[20])에 srand()함수와 rand()함수를 사용하여 200보다 작은 값을 저장하고, 저장된 배열 값을 오름차순 정렬하는 프로그램을 작성하시오.
== Source
#include <stdafx.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SWAP(x, y, t) ( (t)=(x), (x)=(y), (y)=(t) )
int n=20; /* 실제 레코드의 수 */
int jungsu[20];
void quick_sort(int, int); /* 퀵정렬 */
void main(void)
{
int i;
printf("Before array :\n"); /* 정렬전 레코드 출력 */
srand(time(NULL));
for(i=0; i<n; i++) { /* 난수 생성 및 출력 */
jungsu[i] = rand() % 200; /*난수 발생 범위 0~199*/
printf("%d ", jungsu[i]);
}
printf("\n");
quick_sort(0,n-1); /* 퀵정렬 호출 */
printf("\nSorted array :\n"); /* 정렬된 레코드 출력 */
for(i=0; i<n; i++)
printf("%d ", jungsu[i]);
printf("\n");
}
void quick_sort(int left, int right)
{
int pivot, i, j, temp;
if(left<right)
{ /* 리스트에 2개 이상의 레코드가 있을 경우 */
i = left;
j = right+1;
pivot = jungsu[left]; /* 피벗 설정 */
do {
do /* 왼쪽 리스트에서 피벗보다 큰 레코드 선택 */
i++;
while(jungsu[i]<pivot);
do /* 오른쪽 리스트에서 피벗보다 작은 레코드 선택 */
j--;
while(jungsu[j]>pivot);
if(i<j) SWAP(jungsu[i], jungsu[j], temp); /* 레코드 i, j 교환 */
}
while(i<j); /* 인덱스 i,j가 엇갈리지 않는 한 반복 */
SWAP(jungsu[left], jungsu[j], temp); /* 레코드 j와 피벗 교환 */
quick_sort(left, j-1); /* 왼쪽 부분리스트를 퀵정렬 */
quick_sort(j+1, right); /* 오른쪽 부분리스트를 퀵정렬 */
}
}
== Result
Before array :
110 120 177 168 4 190 74 155 185 33 39 154 96 141 191 99 25 114 4 19
Sorted array :
4 4 19 25 33 39 74 96 99 110 114 120 141 154 155 168 177 185 190 191
계속하려면 아무 키나 누르십시오 . . .