---
# System prepended metadata

title: Quiz 03
tags: [程設一quiz]

---

Quiz 03
===
# [A] Right Triangle
## Description

A simple corollary from Pythagorean theorem is that in a right triangle the square of hypotenuse equals to sum of square of the two legs. Write a program to determine whether a triangle is a right triangle or not by using this corollary.

**Input**
The input begins with an integer n, indicating the number of cases. Each case contains three integers, which are the lengths of the three sides of a triangle.

**Output**
For each case, output the description of such triangle in a separate line in the format:
“The triangle with edges a, b, and c is (not) a right triangle.”

**Sample**
| Input   | Output                                                       |
|:------- |:------------------------------------------------------------ |
| 3       |                                                              |
| 2 3 4   | The triangle with edges 2, 3, and 4 is not a right triangle. |
| 3 4 5   | The triangle with edges 3, 4, and 5 is a right triangle.     |
| 5 12 13 | The triangle with edges 5, 12, and 13 is a right triangle.   |

## Solution
- Pythagorean Theorem: 
Let a be the longest side: $a^2 = b^2 + c^2$
In this problem, we're not sure which size is the longest, therefore we need to check if $b^2 = a^2 + c^2$ and $c^2 = a^2 + b^2$.

- pseudo code:
```
user inputs how many cases there will be (n)
for case 1 to case n:
    user inputs a, b, c //three sides of the triangle
    if a, b, c satisfies the Pythagorean theorem:
        print "is a right triangle"
    else
        print "is not a right triangle"
```

## Code
```c=
#include <stdio.h>

int main(){
    int n; //cases
    int a, b, c; //side's length
    scanf("%d", &n);

    for(int i=0; i<n; i++){
        scanf("%d %d %d", &a, &b, &c);

        if((a*a == b*b + c*c) || (b*b == a*a + c*c) || (c*c == a*a + b*b)){
            printf("The triangle with edges %d, %d, and %d is a right triangle.\n", a, b, c);
        }
        else{
            printf("The triangle with edges %d, %d, and %d is not a right triangle.\n", a, b, c);
        }
    }
}
```

# [B] Combat
## Code
```c=
#include <stdio.h>

int main(){
    int atk_pow, atk_life, def_pow, def_life;
    int count = 1; //記錄為第幾局
    
    while(1){
        scanf("%d %d %d %d", &atk_pow, &atk_life, &def_pow, &def_life);

        //if all four inputs are zeros, end the loop
        if(!atk_pow && !atk_life && !def_pow && !def_life){
            break;
        }

        printf("Combat %d: ", count);
        if(atk_pow >= def_life){
            if(def_pow >= atk_life){ //both vanished
                printf("Sacrificed attack. Both vanished. ");
            }
            else{ //defender vanished, attacker survived
                printf("Well attack. Defender vanished, and attacker survived. ");
            }

            if(atk_pow > def_life){
                if(atk_pow-def_life == 1)
                    printf("Attacker caused 1 damage to opponent player.\n");
                else
                    printf("Attacker caused %d damages to opponent player.\n", atk_pow-def_life);
            }
            else{
                printf("No damage caused.\n");
            }
        }
        else{
            if(def_pow >= atk_life){ //attacker vanished, defender survived
                printf("Poor attack. Attacker vanished, but defender survived. No damage caused.\n");
            }
            else{ //both survived
                printf("Ineffective attack. Both survived. No damage caused.\n");
            }

            
        }

        count++;
    }
}
```

# [C] Find the Number
## Code
```c=
#include <stdio.h>

int main(){
    int n; //cases
    int t, a, c, target; //sequence type | starting number | difference/ratio | target number
    int x;

    scanf("%d", &n);
    for(int i=0; i<n; i++){
        scanf("%d %d %d %d", &t, &a, &c, &target);

        x = a;
        if(t==1){ //arithmetice sequence
            //find the greatest number
            while(x < target){
                x += c;
            }
            x -= c;

            printf("The greatest number in the arithmetic sequence starting from %d with difference %d less than %d is %d.\n", a, c, target, x);
        }
        else{ //geometric sequence
            //find the greatest number
            while(x < target){
                x *= c;
            }
            x /= c;

            printf("The greatest number in the geometric sequence starting from %d with ratio %d less than %d is %d.\n", a, c, target, x);
        }
    }
}
```

###### tags: `程設一quiz`