# Sample Code HW4 #### [Back to Computer Programming (I)](https://hackmd.io/9tPBK8QATpCD5UQ0uvAZ-g?both) ###### tags: `NTNU` `CSIE` `必修` `Programming(I)` ## hw0401 ![](https://i.imgur.com/WCmX65j.jpg) ![](https://i.imgur.com/t5pOofu.jpg) ## recursive program ### Author : ShihHeng ```c= #include <stdio.h> #include <stdint.h> void Hanoi(int32_t N, int32_t A, int32_t B, int32_t C){ if(N == 1) printf("move disk 1 to rod %d\n", C); else{ Hanoi(N-1, A, C, B); printf("move disk %d to rod %d\n", N, C); Hanoi(N-1, B, A, C); } } int main(){ int32_t N = 0; printf("Please enter the disk number (2-20): "); scanf("%d", &N); Hanoi(N, 1, 3, 2); return 0; } ``` ## iterative program ### Author : ShihHeng ```c= #include <stdio.h> #include <stdint.h> int main(){ int32_t N = 0; printf("Please enter the disk number (2-20): "); scanf("%d", &N); int32_t index[4] = {0}; int32_t stack_1[21] = {0}, stack_2[21] = {0}, stack_3[21] = {0}; index[1] = N; stack_1[0] = 21; stack_2[0] = 21; stack_3[0] = 21; for(size_t i = 1; i <= N; i++) stack_1[i] = N - i + 1; if(N & 1){ for(int i = 0; i < 3; i++){ if(stack_1[index[1]] < stack_2[index[2]]){ printf("move disk %d to rod 2\n", stack_1[index[1]]); stack_2[++index[2]] = stack_1[index[1]]; stack_1[index[1]--] = 0; //printf("%d\n", stack_2[0]); if(index[2] == N) break; } else{ printf("move disk %d to rod 1\n", stack_2[index[2]]); stack_1[++index[1]] = stack_2[index[2]]; stack_2[index[2]--] = 0; } if(stack_1[index[1]] < stack_3[index[3]]){ printf("move disk %d to rod 3\n", stack_1[index[1]]); stack_3[++index[3]] = stack_1[index[1]]; stack_1[index[1]--] = 0; } else{ printf("move disk %d to rod 1\n", stack_3[index[3]]); stack_1[++index[1]] = stack_3[index[3]]; stack_3[index[3]--] = 0; } if(stack_2[index[2]] < stack_3[index[3]]){ printf("move disk %d to rod 3\n", stack_2[index[2]]); stack_3[++index[3]] = stack_2[index[2]]; stack_2[index[2]--] = 0; } else{ printf("move disk %d to rod 2\n", stack_3[index[3]]); stack_2[++index[2]] = stack_3[index[3]]; stack_3[index[3]--] = 0; if(index[2] == N) break; } } } else{ while(1){ if(stack_1[index[1]] < stack_3[index[3]]){ printf("move disk %d to rod 3\n", stack_1[index[1]]); stack_3[++index[3]] = stack_1[index[1]]; stack_1[index[1]--] = 0; } else{ printf("move disk %d to rod 1\n", stack_3[index[3]]); stack_1[++index[1]] = stack_3[index[3]]; stack_3[index[3]--] = 0; } if(stack_1[index[1]] < stack_2[index[2]]){ printf("move disk %d to rod 2\n", stack_1[index[1]]); stack_2[++index[2]] = stack_1[index[1]]; stack_1[index[1]--] = 0; if(index[2] == N) break; } else{ printf("move disk %d to rod 1\n", stack_2[index[2]]); stack_1[++index[1]] = stack_2[index[2]]; stack_2[index[2]--] = 0; } if(stack_2[index[2]] < stack_3[index[3]]){ printf("move disk %d to rod 3\n", stack_2[index[2]]); stack_3[++index[3]] = stack_2[index[2]]; stack_2[index[2]--] = 0; } else{ printf("move disk %d to rod 2\n", stack_3[index[3]]); stack_2[++index[2]] = stack_3[index[3]]; stack_3[index[3]--] = 0; if(index[2] == N) break; } } } return 0; } ``` ### Author : 蕭瀜 ```c= #include <stdio.h> #include <stdint.h> int32_t rod[3][21] = {{1000}, {1000}, {1000}}; int32_t top[3] = {0}; void put(int32_t a, int32_t b) { if(rod[a][top[a]] > rod[b][top[b]]) { rod[a][++top[a]] = rod[b][top[b]]; printf("move disk %d to rod %d\n", rod[b][top[b]--], a + 1); } else { rod[b][++top[b]] = rod[a][top[a]]; printf("move disk %d to rod %d\n", rod[a][top[a]--], b + 1); } } int main(){ int32_t number = 0, from = 0, to = 1; printf("Please enter the disk number (2-20): "); scanf("%d", &number); if(number < 2 || number > 20) printf("wrong input\n"); for(int32_t i = 1; i <= number; i++) rod[0][i] = number - i + 1; top[0] = number; while(1) { rod[to][++top[to]] = rod[from][top[from]--]; printf("move disk 1 to rod %d\n", to + 1); if(rod[to][1] == number && top[to] == number) break; put((1 + to) % 3, (2 + to) % 3); from = to; to = (to + 1) % 3; } } ``` ## hw0402 ![](https://i.imgur.com/MGKTOCX.jpg) ![](https://i.imgur.com/9ELzS0m.jpg) ### Author : Joseph Tu ```c= #include <stdio.h> #include <stdint.h> #include <math.h> int main(){ int32_t degree,i = 0; double x,ans; printf("Please enter the degree: "); scanf("%d",&degree); double f[degree+1]; printf("Please enter the coefficients: "); for(size_t i = 0;i <= degree;i++) scanf("%lf",&f[i]); printf("Please enter x: "); scanf("%lf",&x); while(degree >= 0){ ans = 0; for(size_t j = 0;j <= degree;j++){ ans += f[j] * pow(x,degree-j); f[j] *= (degree - j); } if(!i) printf("f(%.0lf) = %.0lf\n",x,ans); else printf("f^%d(%.0lf) = %.0lf\n",i,x,ans); degree--; i++; } return 0; } ``` ## hw0403 ![](https://i.imgur.com/oezS8P2.jpg) ![](https://i.imgur.com/zNWOoBK.jpg) ### Author : Joseph Tu ```c= #include <stdio.h> #include <stdint.h> int main(){ int32_t input,action[10] = {0},satus = 0,count = 0; printf("Please enter your action: "); scanf("%d",&input); while(input){ switch(input){ case -1: satus --; break; case -2: if(action[satus+1]) satus++; break; default: if(satus < 10){ action[satus] = input; satus++; count = satus; for(int32_t i = satus; i < 10;i++) action[i] = 0; } else{ for(int32_t i = 0;i < 9;i++) action[i] = action[i+1]; action[satus-1] = input; } break; } printf("Please enter your action: "); scanf("%d",&input); } printf("%d",action[0]); if(satus == 1) printf("(*)"); for(size_t i = 1; i < count;i++){ printf(" %d",action[i]); if(i == satus-1) printf("(*)"); } printf("\n"); return 0; } ``` ## hw0404 ![](https://i.imgur.com/Cd5LWie.jpg) ![](https://i.imgur.com/jdphD3s.jpg) ### Author : Joseph Tu ```c= #include <stdio.h> #include <stdint.h> int main(){ int32_t n; double x,y,sumx = 0,sumy = 0,sumxy = 0,sumx2 = 0,m,avergex,avergey,b; printf("Please enter the point number: "); scanf("%d",&n); for(size_t i = 0;i < n;i++){ printf("Please enter Point %ld: ",i+1); scanf("%lf %lf",&x,&y); sumx += x; sumy += y; sumxy += x * y; sumx2 += x * x; } avergex = sumx / n; avergey = sumy / n; m = (n * sumxy - sumx * sumy) / (n * sumx2 - sumx * sumx); b = avergey - m * avergex; printf("Regression Equation: y = %lf x + %lf\n",m,b); return 0; } ``` ## hw0405 ![](https://i.imgur.com/P8QuN91.jpg) ### Author : main.c: ```c= #include <stdio.h> #include <stdint.h> #include "h5.h" int main(){ int32_t a,n; printf("Please enter the modulus: "); scanf("%d",&n); printf("Please enter the number: "); scanf("%d",&a); if(!a || !n || n == 1 || gcd(a,n) != 1) printf("No modular multiplicative inverse.\n"); else printf("The modular multiplicative inverse of %d is %d.\n",a,inverse(a,n)); return 0; } ``` h5.h: ```c= #pragma once #include <stdio.h> #include <stdint.h> int32_t gcd(int32_t a,int32_t b); int32_t inverse(int32_t a,int32_t n); ``` h5.c: ```c= #include "h5.h" int32_t gcd(int32_t a,int32_t b){ while(b != 0){ a %= b; a ^= b; b ^= a; a ^= b; } return a; } int32_t inverse(int32_t a,int32_t n){ int32_t i; a %= n; while(a * i % n != 1) i++; return i; } ``` ## hw0406 ![](https://i.imgur.com/eZTmKNc.jpg) ```c= int p(int i, int N) { return (i < N && printf("%d\n", i) && !p(i + 1, N)) || printf("%d\n", i); } ``` ![](https://i.imgur.com/S8GwRuv.jpg)