計算概論(基於C語言,北大主幹基礎課)
從問題到程序————程序設計與C語言引論
數據結構(基於C語言,北大主幹基礎課)
C語言程序設計——課程材料、參考材料和討論
最經常使用標準庫函數
裘宗燕(Qiu Zongyan) 北京大學數學學院信息科學系教授
理論計算機html
1) 寫出運行結果算法
#include <stdio.h> int main() { unsigned int i = -2; int j = ~i; printf("%d\n", j); }
2) 寫出運行結果segmentfault
#include <stdio.h> void swap(int &a, int &b) { a ^= b ^= a ^= b; } int main() { int m = 5, n = 3; swap(m, n); printf("m = %d, n = %d\n", m, n); }
/* 定義順序表的大小。應根據須要修改 */ #define MAXNUM 20 /* 定義順序表的元素類型。應根據須要修改 */ typedef int DataType; struct SeqList { int n; /* 存放線性表中元素的個數 n < MAXNUM */ DataType element[MAXNUM]; /* 存放線性表中的元素 */ }; typedef struct SeqList *PSeqList; /* 建立新的順序表 */ PSeqList createNullList_seq(void); /* 釋放順序表 */ void freeList_seq(PSeqList palist); /* 判斷順序表是否爲空 */ int isNullList_seq(PSeqList palist); /* 在palist所指順序表中下標爲p的元素以前插入元素x */ int insert_seq(PSeqList palist, int p, DataType x); /* 在palist所指順序表中刪除下標爲p的元素 */ int delete_seq(PSeqList palist, int p); /* 求x在palist所指順序表中的下標 */ int locate_seq(PSeqList palist, DataType x); /* 求palist所指順序表中下標爲p的元素值 */ DataType retrieve_seq(PSeqList palist, int p);
/* 定義連接表元素類型。應根據須要定義 */ typedef int DataType; struct Node; /* 單鏈表結點類型 */ typedef struct Node *PNode; /* 結點指針類型 */ typedef struct Node *LinkList; /* 單鏈表類型 */ struct Node { /* 單鏈表結點結構 */ DataType info; PNode link; }; /* 建立一個帶頭結點的空鏈表 */ LinkList createNullList_link(void); /* 釋放一個帶頭節點的鏈表 */ void freeList_link(LinkList llist); /* 判斷llist帶有頭結點的單鏈表是不是空鏈表 */ int isNullList_link(LinkList llist); /* 在llist帶頭結點的單鏈表中下標爲i的(第i+1個)結點前插入元素x */ int insert_link(LinkList llist, int i, DataType x); /* 在llist帶有頭結點的單鏈表中刪除第一個值爲x的結點 */ int delete_link(LinkList llist, DataType x); /* 在llist帶有頭結點的單鏈表中找第一個值爲x的結點存儲位置 */ PNode locate_link(LinkList llist, DataType x); /* 在帶有頭結點的單鏈表llist中求下標爲i的(第i+1個)結點的存儲位置 */ /* 當表中無下標爲i的(第i+1個)元素時,返回值爲NULL */ PNode find_link(LinkList llist, int i);
#include <stdio.h> void chai(int a[], int i) { a[2] = i%10; i /= 10; a[1] = i%10; i /= 10; a[0] = i; } int zhong(int w[]) { int f[] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0}; for(int i=0; i<9; i++) { if(f[w[i]]) return 0; f[w[i]] = 1; } return 1; } int main() { for(int i=123; i<=329; i++) { int w[9]; chai(w, i); chai(w+3, 2*i); chai(w+6, 3*i); if(zhong(w)) printf("%d %d %d\n", i, 2*i, 3*i); } }
#include <stdio.h> #define N 6 static int s[N][N]; void output_solution() { if(s[0][10] == N*N || s[2][11] == N*N || s[3][12] == N*N) { for(int y=0; y<N; y++) { for(int x=0; x<N; x++) printf("%02d ", s[x][y]); printf("\n"); } printf("\n"); } } void dfs(int x, int y, int count) { struct { int x; int y; } hn[] = { { 1, 2}, { 2, 1}, { 2,-1}, { 1,-2}, {-1,-2}, {-2,-1}, {-2, 1}, {-1, 2} }; if(count > N*N) { output_solution(); return; } for(int i=0; i<8; i++) { int tx = x + hn[i].x; int ty = y + hn[i].y; if(0<=tx && tx<N && 0<=ty && ty<N && !s[tx][ty]) { s[tx][ty] = count; dfs(tx, ty, count+1); s[tx][ty] = 0; } } } int main() { s[1][0] = 1; dfs(1, 0, 2); }
#include <stdio.h> unsigned long Fibonacci(int n) { static unsigned long F[100] = {1, 1}; static int N = 2; while(N<n) { F[N] = F[N-1]+F[N-2]; N++; } return F[n-1]; } int main() { printf("Fibonacci(2)=%lu\n", Fibonacci(2)); printf("Fibonacci(3)=%lu\n", Fibonacci(3)); printf("Fibonacci(5)=%lu\n", Fibonacci(5)); printf("Fibonacci(19)=%lu\n", Fibonacci(19)); printf("Fibonacci(80)=%lu\n", Fibonacci(80)); printf("Fibonacci(90)=%lu\n", Fibonacci(90)); printf("Fibonacci(91)=%lu\n", Fibonacci(91)); printf("Fibonacci(92)=%lu\n", Fibonacci(92)); printf("Fibonacci(93)=%lu\n", Fibonacci(93)); printf("Fibonacci(95)=%lu\n", Fibonacci(95)); printf("Fibonacci(100)=%lu\n", Fibonacci(100)); }