# Sample Code HW1 #### [Back to Computer Programming (I)](https://hackmd.io/9tPBK8QATpCD5UQ0uvAZ-g?both) ###### tags: `NTNU` `CSIE` `必修` `Programming(I)` 想看誰的Code可以按右邊的目錄快速移動喔 ## hw0102 ![](https://i.imgur.com/QUpt5b5.png) ### Author:芮芮 ```c= /** *Author:芮芮 *Method: **/ #include <stdio.h> #include <stdint.h> int main(){ printf("Please enter a 5-digits integer: "); int32_t a, ans = 0;//a用來存輸入的數,ans用來存最後輸出的整數 scanf("%d", &a);//因為題目已經保證是五位整數了,所以可以不用做輸入維護 while(a){//當a!=0的時候重複以下 ans += a % 10;//將a的個位數加進ans a /= 10;//捨去a的個位數 } printf("Result: %d\n", ans); return 0; } ``` ## hw0103 ![](https://i.imgur.com/tjohA1c.png) ### Author:芮芮 ```c= /** *Author:芮芮 **/ #include <stdio.h> #include <stdint.h> int main(){ double x1, x2, y1, y2, x, y, z; printf("Please enter the 1st point: "); scanf("%lf %lf", &x1, &y1);//輸入第一點 printf("Please enter the 2nd point: "); scanf("%lf %lf", &x2, &y2);//輸入第二點 printf("Please enter x of the 3rd point: "); scanf("%lf", &x);//輸入第三點的x座標 y = y1 + (x - x1) * (y2 - y1) / (x2 - x1); //記得在做浮點數運算的時候先做乘法 //雖然乘除都有可能會有誤差,但乘法出包機率比較小 //原因請洽計概 int32_t precision = 0;//記錄小數有幾位 z = y; while(z != (int)z && precision < 15){ z *= 10; precision ++; } //上面這一段是判斷小數有幾位數的部分(但我寫爆了) //原理是不斷乘10並判斷 z的整數部分 和 z 相不相等 //但因為浮點數誤差的關係,只要小數不能完美以二進位表示就會出錯 printf("y of the 3rd point: %.*lf\n", precision, y); //%.*lf 可以透過在後面加一個參數來決定輸出幾位 } ```