Quiz 06
===
# [A] SciNotation
## Description
Scientific notation can be used to express floating-point numbers accurately. The representation of scientific notation is given as follows: $𝑥= a.bbbbbbE\pm c$
When conducting experiments, it is quite often to calculate the value $𝑝^𝑘$, where 0 ≤ 𝑝 ≤ 1 denotes eventprobability, and 𝑘 is the number of times the event occurred. For not losing the precision, we can use scientific notation to express the value $𝑝^𝑘$ so that $𝑝^𝑘 = a.bbbbbbE\pm c$.
A special method for any integer $𝑏 = \frac{1}{p}$ is to divide one by $b$ for $k$ times. Whenever the quotient is less than $p$ one, multiply the quotient by 10 until it is greater than or equal to one. Every time the quotient is multiplied by 10, the power $c$, starting from zero, should be subtracted by one. Write a program to calculate the value $𝑝^𝑘$ according to above method.
**Input**
The input contains several cases and ends with EOF. Each case contains two integer values, which in turn represent $b$ and $k$.
**Output**
For each case, output the probability value $𝑝^𝑘$ in scientific notation.
**Example**
| input | output |
|:-----:|:-------------------:|
| 2 10 | 2^-10 = 9.765625E-4 |
| 3 7 | 3^-7 = 4.572474E-4 |
## Solution
由題目可知:
所求 $𝑝^𝑘$ = ($\frac{1}{b}$)^k^ = ($\frac{1}{b^k}$) = b^-k^,亦可視為1除以$b$ $k$次。
輸出$p^k$須以科學記號表示。
## Code
```c=
#include <stdio.h>
int main(){
int b, k;
int c;
double result;
while(scanf("%d %d", &b, &k) != EOF){
result = 1;
c = 0;
for(int i=0; i<k; i++){
result /= b;
}
while(result < 1){
result *= 10;
c--;
}
printf("%d^-%d = %fE%d\n", b, k, result, c);
}
}
```
# [B] SimEquations
## Description
In high school, we have learned a lot about simultaneous equations. In fact, many real-world applications can be described as simultaneous equations. To solve a set of simultaneous equations by hand, we could use many math techniques about algebra, yet these techniques are impractical in computers. A simple concept of finding a solution of a set of simultaneous equations in the sense of computing is to enumerate all the possible solutions if the variables are in integer domain.
Write a program to find a solution satisfying the following simultaneous equations:
\begin{cases}
u + 2v + 3w = P\\
uvw = Q\\
u^3 + v^2 + w = R\\
\end{cases}
Note that 𝑢, 𝑣, and 𝑤 are unique integer values, satisfying $0 < u < v < w$.
Requirement: Use for loop rather than while loop for simplicity.
**Input**
The input starts from an integer $n$, which indicates the number of cases, and $n$ lines follow. Each case in a line contains three integer values, which in turn represent $P$, $Q$ and $R$.
**Output**
For each case, output the three values $u$, $v$, and $w$.
**Sample**
| input | output |
| --------- | ------ |
| 2 | |
| 42 100 43 | Case 1: u = 2, v = 5, w = 10 |
| 30 28 24 | Case 2: u = 1, v = 4, w = 7 |
## Solution
利用迴圈,將各個可能的正整數帶入方程式中,若符合,則輸出$u$, $v$, $w$分別為何,並結束此迴圈,使使用者輸入下一組$P$, $Q$, $R$。
## Code
```c=
#include <stdio.h>
#include <math.h>
int main(){
int P, Q, R; //inputs
int u, v, w; //result
int n; //number of cases
int done;
scanf("%d", &n);
for(int i=1; i<=n; i++){
scanf("%d %d %d", &P, &Q, &R);
done = 0;
for(u=pow(R, 1.0/3.0); u>0; u--){
for(v=u+1; v<pow(R, 0.5); v++){
for(w=v+1; w<P/3; w++){
if(((u+2*v+3*w)==P) && ((u*v*w)==Q) && ((u*u*u + v*v + w)==R)){
printf("Case %d: u = %d, v = %d, w = %d\n", i, u, v, w);
done = 1;
break;
}
}
if(done){
break;
}
}
if(done){
break;
}
}
}
}
```
- line 15: 將 $u$ 的範圍限制在 0 與 $R^\frac{1}{3}$(方程式第三條)之間,可縮短程式執行時間。
- line 15: 令 $u$ 由 $R^\frac{1}{3}$ 開始遞減,可加速運行時間(根據測資之運行時間)
- line 16: 同理line 15。
- `done`: 用此以判斷是否已經找出解答,若已找到,則終止迴圈,使使用者輸入下一筆$P$, $Q$, $R$。
# [C] Mortgage
## description
Nahida wants to buy a house for Nilou after her wedding. She has viewed several properties under different contracts. A property can be categorized into six ranks ranged from 1 to 6, which in turn correspond to the maximum mortgage of 90%, 85%, 80%, 70%, 60%, and 50% of its value. The loan repayment for mortgage depends on the period of repayment, the length of grace period, and the rate. During grace period, only interests have to be paid. After grace period, it is quite common to have a fixed monthly repayment, including the monthly interests and the a portion of the principle of the mortgage.
Please write a program to help for calculating the mortgage.
Hint: The portion of the principle of the mortgage in the monthly repayment can be found by a brute-force approach.
**Input**
The input contains several cases and ends with EOF. Each case contains five integers, which represent
1. the value of the property
2. the rank of the property
3. the period of the mortgage in month
4. the grace period in month
5. the rate(year) in percentage.
**Output**
For each case, output the details of the mortgage according to the format given in sample output.
**Sample**
*Input*
10000000 1 240 0 2
20000000 3 360 36 1
*Output*
The mortgage for the rank 1 property with price 10000000 is 9000000.
The loan repayment at 2% loan rate is 45529 for term 1 to term 239, and 45526 for term 240.
The mortgage for the rank 3 property with price 20000000 is 16000000.
The loan repayment at 1% loan rate is 13333 for term 1 to term 36 (grace period), 56369 for term 37 to term 359, and 56358 for term 360.
## solution
**第一行輸出**:判斷房貸可貸多少
可使用switch或是陣列判斷
```c
//mortgage: 房貸 | value: 房子價值
float rank_percentage[6] = {0.9, 0.85, 0.8, 0.7, 0.6, 0.5};
mortgage = value * rank_percentage[rank-1];
```
**第二行輸出**: 寬限期(grace)還款,每月還款,最後一個月之還款
- `寬限期還款` = `房貸` $\times$ `月利率` = `房貸` $\times$ (`年利率` / 12)
- `每月還款`
- 使用brute force (窮舉法):
1. 先令`每月還款`為$\frac{房貸}{貸款期數-寬限期數}$(若利率為 0 時的金額)
2. 接著將`房貸`乘以`月利率`並減`每月還款`,重複`貸款期數-寬限期數`次
3. 若最終`房貸`>0,則代表此金額不足以還完貸款,故將其+1,並再次執行第2步驟
- 使用公式:(公式可能因小數點進位的關係,而導致無法得出judge system上的正確輸出)
`每月還款` = $\frac{(月利率+1)^p\times月利率}{(月利率+1)^p - 1} \times$ `房貸` (p=`貸款期數`-`寬限期數`)
- `最後還款`
- 結束計算`每月還款`後,若`房貸<0`,則`最後還款` = `房貸`+`每月還款`
## code
:::info
直到現在還是無法通過judge system......
不知道究竟是我的問題還是題目的問題
不過邏輯基本上是沒什麼錯,根據上方說明應該能夠寫出來。
如果還有需要的話就留言或私訊吧
:::
###### tags: `程設一quiz`