This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int weight,result = 0;//result 紀錄是否有解,為 0 則無解,為 1 則有解 | |
/* | |
* combination function: | |
* s array記錄所有砝碼的重量 | |
* m 表示砝碼的個數 | |
* n 想要取的砝碼個數 | |
*/ | |
void combination(int *s, int m, int n, int pos, int got) { | |
int i, tmp, sum; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main() { | |
// 輸入學生數量 n | |
int n; | |
scanf("%d", &n); | |
// 輸入 n 個學生成績,並計算成績總和 | |
int grade[n], i, sum = 0; | |
for(i = 0; i < n; i++) { | |
scanf("%d", &grade[i]); | |
sum += grade[i]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
//return if input is leap year | |
int leap(int year) { | |
return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0); | |
} | |
//return leap day between base day and target day. | |
int leapDay(int year, int month, int date) { | |
int count = 0, i; | |
//大於等於基準日 2000 / 01 / 01 | |
if(year >= 2000) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int func(int price) { | |
int count = 0; | |
while(price > 50) { | |
price = price / 2; | |
count = count + 1; | |
} | |
return count; | |
} | |
int main() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
//use strtok(string.h) to split token | |
//reference: http://www.cplusplus.com/reference/cstring/strtok | |
int main() { | |
char str[] = "this is token test"; | |
char str2[] = "it will show how to split string"; | |
char * token; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
void getRandNum(int ans[]) { | |
// 洗牌取亂數 | |
// 準備好 pool 陣列,把0~9依序放入 | |
int pool[10], i; | |
for(i = 0; i <= 9; i++) | |
pool[i] = i; | |
// 初始化亂數種子 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
//計算階乘 | |
int factorial(int x) { | |
int i, result = 1;; | |
for (i = 2; i <= x; i++) { | |
result *= i; | |
} | |
return result; | |
} | |
//判定是否為質數 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
//recursion | |
int func(int n) { | |
if(n == 0) { | |
return 0; | |
} | |
if(n % 2 != 0) { | |
return n + func(n - 1); | |
} else { | |
return - n + func(n - 1); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
void print(int val, int base) { | |
// 把原始值複製一份(tempVal),除一次看結果會有幾位(digit) | |
int digit = 0, tempVal = val; | |
while( tempVal > 0 ) { | |
tempVal /= base; | |
digit++; | |
} | |
// 讓一位可以存一個位址,並依序取餘數後存起來 | |
int i, result[digit]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
//讀入兩值,將val以base進位印出 | |
//若base > 10,以A, B, C....,代表10, 11, 12.... | |
void print(int val, int base){ | |
int tmp[100], digit = 0; | |
while(val != 0) { | |
tmp[digit++] = val % base; | |
val = val / base; | |
} | |
for(; digit > 0; digit--) { |