2008年12月10日 星期三

C Selection Sort - 選擇排序法


#include <stdio.h>
#include <stdlib.h>

#define SWAP(x,y) {int t; t = x; x = y; y = t;}

void show( int a[], int n ) {
int i;
for( i = 0; i < n; i++ )
printf("%d ", a[i] );
printf("\n");
}
void selection_sort( int a[], int n ){
int i, j, m;
for( i = 0; i < n - 1; i++ ){
m = i;
for( j = i + 1; j < n; j++ )
if( a[j] < a[m] )
m = j;
if( i != m )
SWAP( a[i], a[m] );
printf("Sort step %d : ", i+1);
show( a, n );
}
}
int main(void) {
int n = 5;
int a[] = { 18, 2, 20, 34, 12 };
printf("Before selection sort : ");
show( a, n );
selection_sort( a, n );
system("pause");
return 0;
}

C - Bubble Sort - 氣泡排序


#include <stdio.h>
#include <stdlib.h>

#define SWAP(x,y) {int t; t = x; x = y; y = t;}

void show( int a[], int n ) {
int i;
for( i = 0; i < n; i++ )
printf("%d ", a[i] );
printf("\n");
}
void bubble_sort( int a[], int n ){
int i, j, flag = 1;
for( i = 0; i < n - 1 && flag == 1; i++ ){
flag = 0;
for( j = 0; j < n - i - 1; j++ ) {
if( a[j] > a[j + 1] ){
SWAP( a[j], a[j + 1] );
flag = 1;
}
}
printf("第 %d 次排序:", i+1);
show( a, n );
}
}
int main(void) {
int n = 5;
int a[] = { 18, 2, 20, 34, 12 };
printf(" 氣泡排序前:");
show( a, n );
bubble_sort( a, n );
system("pause");
return 0;
}

2008年12月8日 星期一

C - Magic Square - 魔術方陣(尚待改進)


#include <stdio.h>
#include <stdlib.h>

#define N 17

int x[N][N];
void oddMagic(int);
void printMagic(int n);
int main( ) {
int n;
do {
printf("請輸入 3~17 的奇數:");
scanf("%d", &n);
} while( n < 3 || n > N || n % 2 == 0 );
oddMagic(n);
printMagic(n);
system("pause");
return 0;
}
void oddMagic(int n) {
int i, j, key, k, l;
for( i = 0; i < n; i++ )
for( j = 0; j < n; j++ )
x[ i ][ j ] = 0;
/* 先將陣列第一行的中間設為 1 且從這開始出發 */
x[ 0 ][ ( n - 1 ) / 2 ] = 1;
i = 0;
j = ( n - 1 ) / 2;
/* 將陣列想像成上下相連、左右相連,往左走再往上走 */
/* 將數字從 2 開始遞增,指定給此點 */
for( key = 2; key <= n * n; key++ ) {
if( ( i - 1 ) < 0 ) /* 往上走,超過到對面 */
k = n - 1;
else /* 往上走 */
k = i - 1;
if( ( j - 1 ) < 0 ) /* 往左走,超過到對面 */
l = n - 1;
else /* 往左走 */
l = j - 1;
/* 若此點已經有值了,就不往左再往右,而是往下,然後給值 */
if( x[ k ][ l ] )
i = ( i + 1 ) % n;
else {
i = k;
j = l;
}
x[ i ][ j ] = key;
}
}
void printMagic(int n) {
int r, c;
printf("大小為 %d 的魔術方陣:\n", n );
for( r = 0; r < n; r++ ) {
for( c = 0; c < n; c++ )
printf("%4d", x[r][c]);
printf("\n");
}
}

c# - guess number in range - 範圍內猜數字


using System;

namespace cs4 {
class Program {
public static void Main(string[] args) {
int n, count = 0;
Random myran = new Random();
int ran = myran.Next(0,101);
do {
Console.Write("請猜一個 0~100 的整數:");
n = int.Parse( Console.ReadLine() );
if( n > ran )
Console.WriteLine("數字再小一點");
else if( n < ran )
Console.WriteLine("數字再大一點");
count++;
} while( n != ran );
Console.WriteLine("您一共猜了 {0} 次", count);
Console.WriteLine("恭喜您猜對了!");
if( count < 10 )
Console.WriteLine("在 9 次以內!不錯喔!");
Console.Write("請按任意鍵繼續. . . ");
Console.ReadKey(true);
}
}
}

C# - previous input less then now - 前一次的輸入是否比本次輸入小


using System;

namespace cs3 {
class Program {
public static void Main(string[] args) {
int n = 0, before, count = 0;
do {
before = n;
Console.Write("請輸入整數:");
n = int.Parse( Console.ReadLine() );
count++;
}while( n >= before || count == 1 );
Console.WriteLine("前一次的輸入比本次輸入小,結束程式");
Console.Write("請按任意鍵繼續. . . ");
Console.ReadKey(true);
}
}
}

2008年12月6日 星期六

C - Minesweeper - 踩地雷尚待改進版


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>

#define N 5 /* 踩地雷大小 N x N */

int main(void) {
srand(time(NULL));
int a[N][N] = {0};
int b[N][N] = {0};
int i, j, x, y, count = 0, left;

bool judge = false;

for( i = 0; i < N; i++ ) {
for( j = 0; j < N; j++ ) {
a[i][j] = rand() % 2;
if( a[i][j] == 1 ){
count++;
a[i][j] = 9;
}
}
}
left = N * N - count;
for( i = 0; i < N; i++ ) {
for( j = 0; j < N; j++ ) {
if( a[i][j] == 9 ) {
for( x = -1; x <= 1; x++ ) {
for( y = -1; y <= 1; y++ ) {
if( i + x >= 0 && j + y >= 0 && i + x <= N && j + y <= N ) {
a[ i + x ][ j + y ]++;
}
}
}
}
}
}
do{
system("cls");
printf("踩地雷,還剩 %d 個空間:\n ", left );
for( i = 0; i < N; i++ ) {
printf("%d ", i );
}
printf("\n");
for( i = 0; i < N; i++ ) {
printf("%d ", i );
for( j = 0; j < N; j++ ) {
if( b[i][j] == 0 )
printf("?");
else
printf("%d ", b[i][j] );
}
printf("\n");
}
printf("請輸入 X 座標:");
scanf("%d", &x);
printf("請輸入 Y 座標:");
scanf("%d", &y);
if( a[x][y] >= 9 ){
printf("你踩到地雷了!\n ");
judge = true;
} else {
for( i = -1; i <= 1; i++ ) {
for( j = -1; j <= 1; j++ ) {
if( i + x >= 0 && j + y >= 0 && i + x <= N && j + y <= N ) {
if( a[ i + x ][ j + y ] < 9 && b[ i + x ][ j + y ] == 0 ){
b[ i + x ][ j + y ] = a[ i + x ][ j + y ];
left--;
}
}
}
}
if( left == 0 ) {
printf("恭喜你完全沒踩到地雷!\n ");
break;
}
}
} while( judge == false );
system("pause");
return 0;
}

C++ Prime using Bitset - 位元列出質數


#include <iostream>
#include <bitset>

using namespace std;

#define FALSE 0
#define bSize 1000

typedef bitset<bSize> Prime_Set;

Prime_Set primes;

void make_primes() {
primes.set();
for( int i = 2; i * i < bSize; i++ )
if( primes.test(i) )
for(int k = i * i; k < bSize; k += i)
primes.set( k, FALSE );
}
int main() {
make_primes();
for(int k = 2; k < bSize; k++)
if( primes.test(k) )
cout << k << endl;
system("pause");
return 0;
}

// 程式結束