수업 복습(코드 실습) ==> 테스트관련 예제
==> 증감연산자
using System;
public class 증감연산자
{
public static void Main()
{
int a = 3;
int b = 5;
int c = 7;
int d = ++a - --b * c--;
Console.WriteLine("{0}", d);
}
}
==============================================================================================
==> 시프트연산자
using System;
public class 시프트연산자
{
public static void Main()
{
int a = 40;
int r = 0;
r = a >> 3; // 40 * 1/8(1/2의 3승)
Console.WriteLine(r); //?
r = a << 1; // 40 * 2(2의 1승)
Console.WriteLine(r); //?
}
}
/*
어떤 수를 2배 하는 코드가 필요하다?
a = a * 2;
a *= 2;
a <<= 1 ; 속도 가장빠르다.
*/
==============================================================================================
==> 비트연산자
using System;
public class 비트연산자
{
public static void Main()
{
int a = 3;
int b = 2;
Console.WriteLine(a & b); // 비트 AND (논리곱) ==> 둘 다 참일 때만 참, 곱하기
Console.WriteLine(a | b); // 비트 OR (논리합) ==> 하나라도 참이면 참, 더하기
Console.WriteLine(a ^ b); // 비트 XOR (배타적논리합) ==> 서로 다를 때(배타적)만 참
Console.WriteLine(~a); // 비트 NOT (부정) ==> 참이면 거짓, 거짓이면 참
}
}
/*
음수 이진수 : 2의 보수법 : 부호를 붙이고 1을 더함
*/
==============================================================================================
==> 선택정렬
using System;
public class 선택정렬
{
public static void Main()
{
int[] data = { 3, 5, 1, 2, 4 };
// Process
for (int i = 0; i < data.Length - 1; i++)
{
for (int j = i + 1; j < data.Length; j++)
{
if (data[i] > data[j])
{
Swap(ref data[i], ref data[j]);
}
}
}
for (int i = 0; i < data.Length; i++)
{
Console.WriteLine("{0}", data[i]);
}
}
private static void Swap(ref int i, ref int j)
{
int temp = i; i = j; j = temp;
}
}
==============================================================================================
==> 이진검색
==============================================================================================
/*
배수의 합
선택정렬 : 코드를 외워라~
이진검색 : 의미만~
*/