|
Programming/Image Processing 2009. 10. 23. 10:26
(구현한 방법)
완벽하지는 않지만 교수님 결과값에 근접할 수 있는 방법이었음, 더 나은 방법이 있을 것임
Programming/.NET Programming 2009. 9. 15. 23:15
Collections 용어 정리 |
Collection : 데이터 보관 가능 수정, 삭제 삽입의 기능을 가지고 있음
ex. *(원형)Linked List, *Stack, *Queue, *List, Array List, Hash, Hash Table 등
1. Linked List : 노드와 노드 간이 포인터로 연결되어 있어서 노드 간 검색이 가능
2. Array List : List와 거의 흡사, List와 구분이 되는 것은 자료형을 따지지 않는 점.
노드간의 다른 자료형을 넣을 수 있음. 편하지만 용량을 많이 차지.
3. List : 사용법은 1차원 배열과 같음,
Stack, Queue, Linked List의 모든 단점을 보완할 수 있음,
노드간 검색 가능하고 Stack, Queue의 모든 기능을 소화함.
넣을 때 insert, 뺄 때 remove
원하는 인덱스에 넣고 뺄 수 있음(Stack, Queue 대신 이걸 더 많이 씀)
4. Stack : 넣을 때 push, 뺄 때 pop, LIFO
5. Queue : 넣을 때 enqueue, 뺄 때 dequeue, FIFO
6. Hash : 넓은 범위의 것을 좁게 접근, 인덱스를 가지고 있음.
각각 node에 key값과 value값을 가짐.
value에 접근할 때 key로 접근이 가능함
- 배열과 Collection의 가장 큰 차이점 : 정적 - 동적의 차이 |
Stack 예제 |
using System;
using System.Collections; // Stack을 사용하기 위해 필요한 namespace
using System.Collections.Generic; // Stack을 사용하기 위해 필요한 namespace
namespace Stack
{
class Program
{
static void Main(string[] args)
{
Stack<int> stackTest = new Stack<int>(); // Stack 생성
for (int i = 0; i < 10; i++)
stackTest.Push(i); // Data 입력
while (stackTest.Count != 0)
Console.WriteLine(stackTest.Pop()); // Data 출력
}
}
}
|
Queue 예제 |
using System;
using System.Collections; // Queue을 사용하기 위해 필요한 namespace
using System.Collections.Generic; // Queue을 사용하기 위해 필요한 namespace
namespace Queue
{
class Program
{
static void Main(string[] args)
{
Queue<int> queueTest = new Queue<int>(); // Queue 생성
for (int i = 0; i < 10; i++)
queueTest.Enqueue(i); // Data 입력
while (queueTest.Count != 0)
Console.WriteLine(queueTest.Dequeue()); // Data 출력
}
}
}
|
Hach Table 예제 |
using System;
using System.Collections; // Hach Table을 사용하기 위해 필요한 namespace
namespace HachTable
{
class Program
{
static void Main(string[] args)
{
Hashtable table = new Hashtable();
table.Add("Happy", "^^*");
table.Add("Sad", "T_T");
table.Add(0, 5);
Console.WriteLine(table["Happy"]);
A a = new A();
Object b = new A();
// 명시되어 있지는 않지만 모든 클래스는 object클래스로 부터 상속 받음.
// 가장 기본적인 클래스이기 때문.
// class A는 사용자가 method를 입력하지 않았지만 몇가지 method를 기본적으로 가지고 있음.
// table.Add(Object Key, Object Value)에는 아무 자료형(class, string, int ...)이나 들어가도 됨
// Object가 가장 기본이 되는 클래스이기 때문임
}
}
class A { }
}
|
Programming/SRM Practice 2009. 9. 15. 23:11
SRM 150 DIV 2 (250)
문제 요약 : 지갑을 고치는 데 몇 일을 소비할까?
using System;
public class WidgetRepairs
{
public int days(int[] arrivals, int numPerDay)
{
int days = 0;
int remains = 0;
for (int i = 0; i < arrivals.Length - 1; i++)
{
arrivals[i] += remains;
remains = 0;
if (arrivals[i] == 0) continue;
if (arrivals[i] - numPerDay > 0)
{
int temp = arrivals[i] - numPerDay;
if (temp > numPerDay) remains = temp;
else arrivals[i + 1] += temp;
}
days++;
}
days += (arrivals[arrivals.Length - 1] + remains) / numPerDay;
if ((arrivals[arrivals.Length - 1] + remains) % numPerDay != 0) days++;
return days;
}
}
Programming/SRM Practice 2009. 9. 15. 23:04
SRM 149 DIV 2 (250)
문제 요약 : dollars, cents 표기법에 맞게 출력하기
using System;
public class FormatAmt
{
public string amount(int dollars, int cents)
{
string res = "", temp = dollars.ToString();
for (int i = temp.Length - 1; i >= 0; i--)
{
res = temp[i] + res;
if ((temp.Length - i) % 3 == 0 && i != 0) res = "," + res;
}
if (cents.ToString().Length > 1) res += "." + cents.ToString();
else res += ".0" + cents.ToString();
return "$" + res;
}
}
Programming/SRM Practice 2009. 7. 4. 06:53
SRM 307 DIV 2 (250)
문제 요약 : 짝이 맞지 않아서 교환해야하는 신발은 총 몇 켤레인가?
#include <iostream>
#include <vector>
using namespace std;
class BootsExchange
{
public:
int leastAmount(vector <int> left, vector <int> right);
};
int BootsExchange::leastAmount(vector <int> left, vector <int> right)
{
int cnt = 0;
for(int i = 0; i < left.size() ; i++)
{
for(int j = 0; j < right.size() ; j++)
{
if(left[i] == right[j])
{
right[j] = 0;
break;
}
else
{
if(j == right.size() - 1) cnt++;
}
}
}
return cnt;
}
Programming/.NET Programming 2009. 6. 2. 17:06
Collections 용어 정리 |
Collection : 데이터 보관 가능 수정, 삭제 삽입의 기능을 가지고 있음
ex. *(원형)Linked List, *Stack, *Queue, *List, Array List, Hash, Hash Table 등
1. Linked List : 노드와 노드 간이 포인터로 연결되어 있어서 노드 간 검색이 가능
2. Array List : List와 거의 흡사, List와 구분이 되는 것은 자료형을 따지지 않는 점.
노드간의 다른 자료형을 넣을 수 있음. 편하지만 용량을 많이 차지.
3. List : 사용법은 1차원 배열과 같음,
Stack, Queue, Linked List의 모든 단점을 보완할 수 있음,
노드간 검색 가능하고 Stack, Queue의 모든 기능을 소화함.
넣을 때 insert, 뺄 때 remove
원하는 인덱스에 넣고 뺄 수 있음(Stack, Queue 대신 이걸 더 많이 씀)
4. Stack : 넣을 때 push, 뺄 때 pop, LIFO
5. Queue : 넣을 때 enqueue, 뺄 때 dequeue, FIFO
6. Hash : 넓은 범위의 것을 좁게 접근, 인덱스를 가지고 있음.
각각 node에 key값과 value값을 가짐.
value에 접근할 때 key로 접근이 가능함
- 배열과 Collection의 가장 큰 차이점 : 정적 - 동적의 차이
|
List를 Stack과 Queue처럼 사용하는 방법 예제 |
using System;
using System.Collections; // List를 구현하기 위해 추가적으로 필요한 namespace
using System.Collections.Generic; // List를 구현하기 위해 추가적으로 필요한 namespace
namespace List
{
class Program
{
static void Main(string[] args)
{
List<int> Stack = new List<int>(); // Stack용 List생성
List<int> Queue = new List<int>(); // Queue용 List생성
for (int i = 0; i < 10; i++)
{
Stack.Add(i);
Queue.Add(i);
}
Console.WriteLine(" Stack Queue");
while (Stack.Count != 0)
{
Console.WriteLine("\t" + Stack[Stack.Count - 1] + "\t" + Queue[0]);
// Stack처럼 사용, Stack.Count - 1는 배열의 마지막 인덱스
Stack.RemoveAt(Stack.Count - 1);
Queue.RemoveAt(0); // Queue처럼 사용
}
}
}
}
|
List Sorting 예제 |
using System;
using System.Collections; // List를 구현하기 위해 추가적으로 필요한 namespace
using System.Collections.Generic; // List를 구현하기 위해 추가적으로 필요한 namespace
namespace List
{
class Program
{
static void Main(string[] args)
{
List <int> list = new List<int>(); // List 생성
list.Add(-1); // Data 추가
list.Add(100);
list.Add(5);
list.Add(3);
list.Add(40);
Console.WriteLine("Not Sorted...");
for (int i = 0; i < list.Count; i++)
Console.WriteLine(list[i]); // Sorting되기 전 Data 출력
list.Sort(); // Sorting
Console.WriteLine("\nSorted...");
for(int i = 0 ; i < list.Count ; i++)
Console.WriteLine(list[i]); // Sorting된 Data 출력
// int, char,string 등 다양한 자료형을 Sorting 할 수 있음
}
}
}
|
List Sorting이 불가능한 경우 예제 <IComparer의 필요성) |
using System;
using System.Collections; // List를 구현하기 위해 추가적으로 필요한 namespace
using System.Collections.Generic; // List를 구현하기 위해 추가적으로 필요한 namespace
class Program
{
static void Main(string[] args)
{
List<A> list = new List<A>();
list.Add(new A(1, 1));
list.Add(new A(-5, 3));
list.Add(new A(1, 9));
list.Add(new A(5, 7));
list.Add(new A(7, 10));
list.Sort(); // 비교가 불가능하기 때문에 Runtime Error 발생!
}
}
class A
{
public int a;
public int b;
public A(int a, int b)
{
this.a = a;
this.b = b;
}
}
|
IComparer 사용하여 문제를 해결한 예제 |
using System;
using System.Collections; // List를 구현하기 위해 추가적으로 필요한 namespace
using System.Collections.Generic; // List를 구현하기 위해 추가적으로 필요한 namespace
namespace Comparer
{
class Program
{
public static void Main(string[] args)
{
List<A> list = new List<A>();
list.Add(new A(1, 1));
list.Add(new A(-5, 3));
list.Add(new A(1, 9));
list.Add(new A(5, 7));
list.Add(new A(7, 10));
list.Sort(new CompareA());
for (int i = 0; i < list.Count; i++)
Console.WriteLine(list[i].b);
}
}
class A
{
public int a;
public int b;
public A(int a, int b)
{
this.a = a;
this.b = b;
}
}
// 방법1 : 파라미터 형을 명시(List에서 사용)
class CompareA : IComparer<A>
{
int IComparer<A>.Compare(A x, A y)
{
return x.b.CompareTo(y.b);
}
}
// 방법2 : 파라미터 형을 명시하지 않음(Array List에서 사용)
class CompareA2 : IComparer
{
int IComparer.Compare(object x, object y)
{
A class1 = (A)x;
A class2 = (A)y;
return class1.b.CompareTo(class2.b);
}
}
}
|
Programming/.NET Programming 2009. 6. 2. 15:43
Collections 용어 정리 |
Collection : 데이터 보관 가능 수정, 삭제 삽입의 기능을 가지고 있음
ex. *(원형)Linked List, *Stack, *Queue, *List, Array List, Hash, Hash Table 등
1. Linked List : 노드와 노드 간이 포인터로 연결되어 있어서 노드 간 검색이 가능
2. Array List : List와 거의 흡사, List와 구분이 되는 것은 자료형을 따지지 않는 점.
노드간의 다른 자료형을 넣을 수 있음. 편하지만 용량을 많이 차지.
3. List : 사용법은 1차원 배열과 같음,
Stack, Queue, Linked List의 모든 단점을 보완할 수 있음,
노드간 검색 가능하고 Stack, Queue의 모든 기능을 소화함.
넣을 때 insert, 뺄 때 remove
원하는 인덱스에 넣고 뺄 수 있음(Stack, Queue 대신 이걸 더 많이 씀)
4. Stack : 넣을 때 push, 뺄 때 pop, LIFO
5. Queue : 넣을 때 enqueue, 뺄 때 dequeue, FIFO
6. Hash : 넓은 범위의 것을 좁게 접근, 인덱스를 가지고 있음.
각각 node에 key값과 value값을 가짐.
value에 접근할 때 key로 접근이 가능함
- 배열과 Collection의 가장 큰 차이점 : 정적 - 동적의 차이
|
Linked List 사용 예제 |
using System;
// LinkedList를 구현하기 위해 추가적으로 필요한 namespace
using System.Collections;
using System.Collections.Generic;
namespace LinkedList
{
class Program
{
static void Main(string[] args)
{
int cnt;
int input = 0;
LinkedList<int> list = new LinkedList<int>(); // Linked List 생성
LinkedListNode<int> plist; // Linked List Node 생성
///////////////////////////////////////////////////////////////////////////////////
//
// Linked List Node : Linked에 대한 참조(연결정보를 담고 있음)
//
///////////////////////////////////////////////////////////////////////////////////
Console.Write("몇 개의 데이터를 저장하실건가요? ");
cnt = int.Parse(Console.ReadLine()); // string값을 int형으로 읽어옴
Console.WriteLine("\n---------------입력---------------");
for (int i = 0; i < cnt; i++)
{
Console.Write(i+1 + "번째 데이터를 입력해주세요 : ");
input = int.Parse(Console.ReadLine()); // string값을 int형으로 읽어옴
list.AddLast(input); // Linked List에 값을 추가
}
Console.WriteLine("----------------------------------");
Console.WriteLine("\n---------------출력---------------");
plist = list.First; // ListNode가 List의 첫번째 Node 연결정보를 가져옴.
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine(i+1 + "번째 노드에 저장된 데이터 값 : "
+ plist.Value); // Data를 읽어옴
plist = plist.Next; // 다음 Node의 연결정보를 가져옴.
}
Console.WriteLine("----------------------------------");
}
}
}

|
Array List 사용 예제 |
using System;
using System.Collections; // ArrayList를 구현하기 위해 추가적으로 필요한 namespace
namespace ArrayList
{
class Program
{
static void Main(string[] args)
{
ArrayList list = new ArrayList(); // Array List 생성, 자료형 따지지 않음
int z = 0 ;
float j = 1.5f;
double k = 2.3;
list.Add(z); // Data 추가
list.Add(j); // Data 추가
list.Add(k); // Data 추가
for(int i = 0 ; i< list.Count; i++)
Console.WriteLine(list[i]); // Data 출력
}
}
}
|
Programming/.NET Programming 2009. 5. 31. 08:06
Enum(열거형 상수) 예제 |
using System;
namespace Enum
{
class Program
{
enum Flag { A, B, C };
static void Main(string[] args)
{
Console.WriteLine((int)Flag.A);
}
}
}
// enum(열거형 상수) : 문자열을 숫자처럼 사용하기 위해서 사용함 |
일차원 배열 관련 예제 프로그램 |
using System;
namespace Array
{
class program
{
public static void Main(string[] args)
{
int[] a;
Console.Write("배열의 크기를 입력해주세요 : ");
a = new int[Convert.ToInt32(Console.ReadLine())];
int i;
for (i = 0; i < a.Length; i++)
a[i] = i;
addarray(a);
for (i = 0; i < a.Length; i++)
Console.WriteLine("a[{0}] = {1}", i, a[i]); // {0}. {1} 변수 출력 순서 정함
}
// a가 배열의 첫번째 주소값을 가져서 포인터 형으로 사용, 주소복사가 일어남
static private void addarray(int[] array)
{
for (int i = 0; i < array.Length; i++)
{
array[i]++;
}
}
}
}
|
다차원 배열 선언 방법 2가지 예제 |
using System;
namespace Array
{
class Program
{
static void Main(string[] args)
{
// 방법1 : for문 이용, 2차원 배열 선언
int[][] t = new int[10][];
for(int i = 0 ; i < 10 ; i++)
{
t[i] = new int[i];
}
// 방법2 : 콤마(,)이용, 4차원 배열 선언
int[,,,] t2 = new int[10,10,10,10];
}
}
} |
Programming/.NET Programming 2009. 5. 31. 07:07
키워드 this |
this : Class에서 정의한 변수나 함수에 접근할 때 사용. 즉, 클래스 멤버 변수나 멤버 함수에 접근할 때 사용. 혹은 class의 멤버 변수나 멤버 함수라는 것을 명시적으로 표현하기 위해서 사용. |
키워드 this 예제 |
using System;
namespace This
{
class A
{
public static void Main(string[] args)
{
B b = new B("HELLO");
Console.WriteLine(b.strMessage);
Console.WriteLine(b.num);
}
}
class B
{
public string strMessage;
public int num;
public B(string strMessage)
{
// 함수의 지역변수가 아닌 Class 멤버 변수에 접근하기 위해서 사용
this.strMessage = strMessage;
this.num = 10; // 명시적으로 하기 위해서 사용
}
}
}
|
상속 관련 문제 |
Q. B가 부모클래스이고, A가 자식클래스일 때 어떤 것이 맞는 표현일까요?
1. B b = new A();
2. A a = new B(); |
A. 1. B b = new A();
위 벤다이어그램에서 보면 B b = new A();일 경우 b는 A크기의 공간을 할당 받아 가리키게 되며, Class B가 가진 멤버 변수에 전부 접근이 가능하다. 하지만 A a = new B();일 경우 a는 B크기의 공간을 할당 받아 가리키게 되며, Class A가 가진 멤버 변수에 전부 접근이 가능하지는 않다. 그렇기 때문에 에러가 난다.
|
Programming/.NET Programming 2009. 5. 31. 06:57
오버로딩과 오버라이딩 용어 정리 |
오버로딩 : 함수의 이름과 목적은 같지만, 구현 방법 등이 다를 경우, 파라미터의 타입이나 갯수의 차이로 함수를 구별하는 방식. 반환 타입으로는 구별되지 않는다.
ex. Console.WriteLine(); : 19개의 overloading 함수가 존재한다.
오버라이딩 : 함수가 같은 이름, 같은 타입, 같은 수의 매개변수를 가져도 다른(추가적) 기능을 구현하길 원할 때 사용하는 방식. 접근 제한 키워드와 함수 반환 타입 사이에 new를 적어주면 된다.
cf. override : 추상클래스로부터 상속을 받았을 때, 선언만 되어있는 함수를 구할 때 사용하는 키워드 |
오버로딩 예제(생성자 오버로딩) |
using System;
namespace Overloading
{
public class TestA
{
public int a;
public TestA()
{
Console.WriteLine("기본 생성자 입니다.");
}
/// <summary>
/// 생성자 오버로딩
/// </summary>
public TestA(int a)
{
a = 1;
Console.WriteLine("오버로딩 생성자 입니다. a값은 " +a+"입니다.");
}
public static void Main(string[] args)
{
TestA A = new TestA();
TestA B = new TestA(1);
}
}
}
|
오버라이딩 예제 |
using System;
namespace Overriding
{
class Shape
{
public void method()
{
Console.WriteLine("Shape의 method입니다.");
}
}
class Rectangle : Shape
{
public new void method()
{
Console.WriteLine("Rectangle의 method입니다.");
}
}
class Triangle : Shape
{
public new void method()
{
Console.WriteLine("Triangle의 method입니다.");
}
}
class Circle : Shape
{
public new void method()
{
Console.WriteLine("Circle의 method입니다.");
base.method(); // base 키워드를 이용해 부모클래스의 method를 호출
}
}
class Output
{
public static void Main(string[] args)
{
Shape s = new Shape();
s.method();
Rectangle r = new Rectangle();
r.method();
Triangle t = new Triangle();
t.method();
Circle c = new Circle();
c.method();
}
}
}
|
|