본문 바로가기
IT(기존 자료 보관용)

뇌를 자극하는 C# 5.0 프로그래밍 10장 연습문제 1~5번 답

by 공학코드 2015. 4. 27.
728x90
반응형

10장에서 배운 배열과 컬렉션 클래스에 대한 연습문제이다.


1. 다음 배열 선언 문장 중 올바르지 않은 것을 고르세요.


1) int[] array = new String[3] { "안녕", "Hello", "Halo" };

int형 배열인데 String 형식으로 초기화하려고 하고 있다.


2. 두 행렬의 곱을 2차원 배열을 이용하여 계산하는 프로그램을 작성하세요.


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace practice10_2
  8. {
  9. class Matrix
  10. {
  11. public static int[,] multiplication(int[,] a, int[,] b){
  12.  
  13. int[,] result = new int[2, 2] { { 0, 0 }, { 0, 0 } };
  14. result[0, 0] = a[0, 0] * b[0, 0] + a[0, 1] * b[1, 0];
  15. result[0, 1] = a[0, 0] * b[0, 1] + a[0, 1] * b[1, 1];
  16. result[1, 0] = a[1, 0] * b[0, 0] + a[1, 1] * b[0, 1];
  17. result[1, 1] = a[1, 0] * b[0, 1] + a[1, 1] * b[1, 1];
  18.  
  19. return result;
  20. }
  21. }
  22. class Program
  23. {
  24. static void Main(string[] args)
  25. {
  26. // 2*2 배열만 곱셈이 가능한 프로그램이다.
  27. int[,] a = new int[2, 2] { { 3, 2 }, { 1, 4 } };
  28. int[,] b = new int[2, 2] { { 9, 2 }, { 1, 7 } };
  29. int[,] result = Matrix.multiplication(a, b);
  30.  
  31. Console.WriteLine("{0}, {1}", result[0, 0], result[0, 1]);
  32. Console.WriteLine("{0}, {1}", result[1, 1], result[1, 1]);
  33. }
  34. }
  35. }
  36.  


3쇄의 행렬의 곱 계산식에 오타가 있다. 2*2배열 외에 다른 값이 들어오면 에러가 나는 프로그램이다.


3. 다음 코드의 출력 결과는 무엇일까요?


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Collections;
  7. namespace practice10_3
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. Stack stack = new Stack();
  14. stack.Push(1);
  15. stack.Push(2);
  16. stack.Push(3);
  17. stack.Push(4);
  18. stack.Push(5);
  19.  
  20. while (stack.Count > 0)
  21. Console.WriteLine(stack.Pop());
  22. }
  23. }
  24. }
  25.  

나중에 입력된 자료가 먼저 출력되는 후입선출(LIFO)이 특징인 Stack 자료구조에 대한 문제이다.




4. 다음 코드의 출력 결과는 무엇일까요?


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Collections; 
  7. namespace practice10_4
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. Queue que = new Queue();
  14. que.Enqueue(1);
  15. que.Enqueue(2);
  16. que.Enqueue(3);
  17. que.Enqueue(4);
  18. que.Enqueue(5);
  19.  
  20. while (que.Count > 0)
  21. Console.WriteLine(que.Dequeue());
  22. }
  23. }
  24. }
  25.  

먼저 입력된 자료가 먼저 출력되는 선입선출(FIFO)이 특징인 Queue 자료구조에 대한 문제이다.



5. 다음과 같은 결과를 출력하도록 아래의 코드를 완성하세요.


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Collections;
  7.  
  8. namespace practice10_5
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. Hashtable ht = new Hashtable();
  15. ht["Name"] = "Microsoft";
  16. ht["URL"] = "www.microsoft.com";
  17.  
  18. Console.WriteLine("회사 : {0}", ht["Name"]);
  19. Console.WriteLine("URL : {0}", ht["URL"]);
  20. }
  21. }
  22. }
  23.  




키와 값을 가지는 것이 특징인 Hashtable에 대한 연습문제이다.


728x90
반응형

댓글