diff --git a/pset8/1-values.c b/pset8/1-values.c new file mode 100644 index 0000000..cb0c1bc --- /dev/null +++ b/pset8/1-values.c @@ -0,0 +1,21 @@ +// #include +// +// int fuc(int n); +// +// int main() { +// int n; +// +// scanf("%d", &n); +// printf("%d\n", fuc(n)); +// +// return 0; +// } + +/* 请在这里填写答案 */ +int fuc(int n) { + if (n == 0) { + return 0; + } else { + return fuc(n - 1) + n * n * n; + } +} diff --git a/pset8/2-getSum.c b/pset8/2-getSum.c new file mode 100644 index 0000000..8d69852 --- /dev/null +++ b/pset8/2-getSum.c @@ -0,0 +1,21 @@ +// #include +// +// int getSum(int n, int a); +// +// int main() { +// int n, a; +// +// scanf("%d%d", &n, &a); +// printf("%d\n", getSum(n, a)); +// +// return 0; +// } + +/* 请在这里填写答案 */ +int getSum(int n, int a) { + if (n == 0) { + return a; + } else { + return getSum(n - 1, a) + a + 3 * n; + } +} diff --git a/pset8/3-findMax.c b/pset8/3-findMax.c new file mode 100644 index 0000000..55c8cf7 --- /dev/null +++ b/pset8/3-findMax.c @@ -0,0 +1,28 @@ +// #include +// +// int findMax(int n); +// +// int main() { +// int n; +// +// scanf("%d", &n); +// printf("%d\n", findMax(n)); +// +// return 0; +// } + +/* 请在这里填写答案 */ +int findMax(int n) { + int input; + scanf("%d", &input); + if (n == 1) { + return input; + } else { + int tmp = findMax(n - 1); + if (input >= tmp) { + return input; + } else { + return tmp; + } + } +} diff --git a/pset8/4-convert.c b/pset8/4-convert.c new file mode 100644 index 0000000..e44daea --- /dev/null +++ b/pset8/4-convert.c @@ -0,0 +1,23 @@ +// #include +// +// int convert(int n); +// +// int main() { +// int n; +// +// scanf("%d", &n); +// +// printf("%d\n", convert(n)); +// return 0; +// } + +/* 请在这里填写答案 */ +int convert(int n) { + if (n == 1) { // Base case + return 1; + } else if (n == 0) { // Base case + return 0; + } else { + return n % 10 + convert(n/10) * 2; + } +} diff --git a/pset8/5-countBinary.c b/pset8/5-countBinary.c new file mode 100644 index 0000000..dd65bc7 --- /dev/null +++ b/pset8/5-countBinary.c @@ -0,0 +1,23 @@ +// #include +// +// int countBinary(int); +// +// int main() { +// int n; +// +// scanf("%d", &n); +// printf("%d\n", countBinary(n)); +// +// return 0; +// } + +/* 请在这里填写答案 */ +int countBinary(int n) { + if (n == 0) { + return 1; + } else if (n == 1) { + return 1; + } else { + return 1 + countBinary(n / 2); + } +} diff --git a/pset8/6-eveluation.c b/pset8/6-eveluation.c new file mode 100644 index 0000000..f83c362 --- /dev/null +++ b/pset8/6-eveluation.c @@ -0,0 +1,24 @@ +// #include +// +// int evaluation(int n, int a); +// int main() { +// int n, a; +// +// scanf("%d%d", &n, &a); +// printf("%d\n", evaluation(n, a)); +// +// return 0; +// } + +/* 请在这里填写答案 */ +int evaluation(int n, int a) { + if (n == -1) { + return 1; + } else { + int an = 1; + for (int i = 0; i < n; i++) { + an *= a; + } + return a * an + evaluation(n - 1, a); + } +} diff --git a/pset8/7-gcd.c b/pset8/7-gcd.c new file mode 100644 index 0000000..7b2c1c0 --- /dev/null +++ b/pset8/7-gcd.c @@ -0,0 +1,22 @@ +#include + +int GCD(int a , int b ); + +int main() +{ + int a , b ; + + scanf("%d%d", &a , &b ); + printf( "%d\n" , GCD( a, b ) ) ; + + return 0 ; +} + +/* 请在这里填写答案 */ +int GCD(int a , int b ) { + if (a % b != 0) { + return GCD(b, a % b); + } else { + return b; + } +} diff --git a/pset8/8-reverse.c b/pset8/8-reverse.c new file mode 100644 index 0000000..453a8e3 --- /dev/null +++ b/pset8/8-reverse.c @@ -0,0 +1,22 @@ +// #include +// +// void reverse(int n); +// +// int main() { +// int n; +// +// scanf("%d", &n); +// reverse(n); +// printf("\n"); +// return 0; +// } + +/* 请在这里填写答案 */ +void reverse(int n) { + if (n < 10) { + printf("%d", n); + } else { + printf("%d", n % 10); + reverse(n / 10); + } +} diff --git a/pset8/9-printFactor.c b/pset8/9-printFactor.c new file mode 100644 index 0000000..2cf94c5 --- /dev/null +++ b/pset8/9-printFactor.c @@ -0,0 +1,29 @@ +// #include +// +// void printFactor(int, int); +// +// int main() { +// int a, b, i; +// +// scanf("%d%d", &a, &b); +// for (i = a; i <= b; i++) +// printFactor(i, 1); +// +// return 0; +// } + +/* 请在这里填写答案 */ +void printFactor(int num, int fac) { + if (fac == 1) { // Start of the line + printf("%d=", num); + printFactor(num, 2); + } else if (num <= fac) { // End of the line + printf("%d\n", num); + } else if (num % fac == 0) { // The middle ones + printf("%d*", fac); + printFactor(num / fac, 2); + } else { + printFactor(num, fac + 1); + } + return; +}