[-문구-Compeople-문구-]

++ETC/Study2013. 6. 18. 22:42

VisualStudio2010 Project File :Chungam_3-1_C .vol1.eggChungam_3-1_C .vol2.egg

 

1. 아래와 같이 숫자를 입력하여 숫자 피라미드를 출력하는 프로그램을 완성하시고.(cin, cout, 반복문 사용)

출력하고자하는 라인 수를 입력하시오 : 7
       1
      212
     32123
    4321234
   543212345
  65432123456
 7654321234567
계속하려면 아무 키나 누르십시오 . . .

 

##Source

// Print_Number_asc.cpp : Defines the entry point for the console application.
//

#include <stdafx.h>
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
 
int main ()
{
 cout << "출력하고자하는 라인 수를 입력하시오 : ";
 int num,i,j;
 cin >> num;
 for(i = 0; i < num; i++) {
 // 앞에 빈공간 맞춤
  for(j = i; j < num; j++) printf(" ");    
  // 54321 형태출력
  for(j = 0; j <= i; j++) printf("%d",1 + i-j);   
  // 12345 형태 출력     
  for(j = 1; j <= i; j++) printf("%d",1 + j);
  // 줄바꿈
  cout << endl;
 }
//system("pause");
return 0;
}

 

 

2. 재귀호출을 이용하여 피보나치 수를 출력하는 프로그램을 완성하시오.

출력하고자하는 피보나치 인덱스 수를 입력하시오 : 7
피보나치 인덱스 : 0, 1, 2, 3, 4, 5, 6, 7
피보나치 수  열 : 0, 1, 1, 2, 3, 5, 8, 13

 

##Source

// fibonacci.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;

void fibonacci(int n);
 
void main()
{
 int num;
 printf("출력하고자하는 피보나치 인덱스 수를 입력하시오 : ");
 cin >> num;

 cout << "피보나치 인덱스 : ";

 for(int i=0; i<=num; i++)
 {
  cout << i;
  if( i < num ) cout << ", ";
 }
 cout << endl;

 cout << "피보나치 수  열 : ";

 fibonacci(num);

 cout << endl;
}

void fibonacci(int n)
{
 int n1 = 0, n2 = 1, n3;
 if(n == 0)
 {
  cout << n1 << ", ";
 }
 else if(n == 1)
    {
  cout <<  n2 << ", ";
 }
 else
 { 
  cout << n1 << ", " << n2 << ", ";
  for(int i=1; i < n; i++)
  {
   n3 = n1 + n2;
   // if( n3 > n)
   //       break;
   cout << n3;
   n1 = n2;
   n2 = n3;
   if(i < n-1)cout << ", ";
  }
 }
}

 

3. 배열에 포인터를 사용하여 10개 숫자를 읽고, 평균을 계산하고 평균보다 더 큰 수는 화면 출력하는 프로그램을 완성하시오.

1번째 입력값 : 1
2번째 입력값 : 2
3번째 입력값 : 3
4번째 입력값 : 4
5번째 입력값 : 5
6번째 입력값 : 6
7번째 입력값 : 7
8번째 입력값 : 8
9번째 입력값 : 9
10번째 입력값 : 10

평   균   값 : 5
평균보다 큰값: 6, 7, 8, 9, 10

 

##Source

// arr_point_aver.cpp : Defines the entry point for the console application.
//

#include <stdafx.h>
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

int main(void)
{
 int *arr;
 arr = new int[10];
 int i, max=0, total = 0;
 for(i = 0; i < 10; i++)
 {
  cout << (i+1) << "번째 입력값 : ";
  cin >> *(arr+i);
  if(max < *(arr+i)) max = *(arr+i);
  total += *(arr+i);
 }
 cout << endl;
 int average = total / 10;
 cout << "평   균   값 : " << average << endl;
 cout << "평균보다 큰값: ";
 for(i = 0; i < 10; i++)
 {
  if(*(arr+i) > average)
  cout << *(arr+i);
  if(*(arr+i) > average && *(arr+i) < max) cout << ", ";
 }
 cout << endl;
 delete []arr;
 return 0;
}

 

4. 아래와 같이 숫자를 입력하여 입력된 숫자보다 더 작은 합성수(1과 자기 자신이 이외에도 약수를 가진 정수)를 출력하는 프로그램을 완성하시오.(cin, cout, 반복문 사용)

출력하고자하는 최대 합성수를 입력하시오 : 15
합성수 : 4, 6, 8, 9, 10, 12, 14, 15

 

##Source

// Hapsungsu.cpp : Defines the entry point for the console application.
//

#include <stdafx.h>
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

void Habsungsu(int n)

 for(int i = 3; i <= n; i++) // 1과 2는 합성수가 아니므로 3부터 시작
 {
  int num = 2;
  while(i != num)
  {
   if( i % num == 0)
   {
    cout << i << ", ";
    break;
   }
   else num++;
  }
 }
}

int main(void)
{
 cout << "출력하고자하는 최대 합성수를 입력하시오 : ";
 int n;
 cin >> n;
 cout << "합성수 : ";
 Habsungsu(n);
 cout << endl;
 return 0;
}

 

Posted by Compeople