---
###### tags: `C++`
---
# C++期中訂正
## Sleep woman
:::success
**Question**
Daniela is a nurse in a large hospital, which causes her working shifts to constantly change. To make it worse, she has deep sleep, and a difficult time to wake up using alarm clocks. Recently she got a digital clock as a gift, with several different options of alarm sounds, and she has hope that it might help solve her problem. But, lately, she’s been very tired and want to enjoy every single moment of rest. So she carries her new clock to every place she goes, and whenever she has some spare time, she tries to sleep, setting her alarm clock to the time when she needs to wake up. But, with so much anxiety to sleep, she ends up with some difficulty to fall asleep and enjoy some rest. A problem that has been tormenting her is to know how many minutes of sleep she would have if she felt asleep immediately and woken up when the alarm clock ringed. But she is not very good with numbers, and asked you for help to write a program that, given the current time and the alarm time, find out the number of minutes she could sleep.
**Input**
Each test case contains four integers H1, M1, H2 and M2, with H1 : M1 representing the current hour and minute, and H2 : M2 representing the time (hour and minute) when the alarm clock is set to ring (0 ≤ H1 ≤ 23, 0 ≤ M1 ≤ 59, 0 ≤ H2 ≤ 23, 0 ≤ M2 ≤ 59).
**Output**
For each test case, containing a single integer, indicating the number of minutes Daniela has to sleep.
| Test | Input | Result |
| ---- | ---------- | ------ |
| 1 | 1 5 3 5 | 120 |
| 2 | 23 59 0 34 | 35 |
:::
### Solution
```C++=
#include <iostream>
using namespace std;
int main() {
int H1,H2,M1,M2;
cin>>H1>>M1>>H2>>M2;
M1+=H1*60;
M2+=H2*60;
if (M1>M2){
cout<<24*60-M1+M2; //24*60-(M1-M2)
}
else{
cout<<M2-M1;
}
}
```
## Find the maximum
:::success
**Question**
Write a program that read three integers, then outputs the largest number followed by the words "is largest." If the numbers are equal, print the message "These numbers are equal."
| Test | Input | Result |
| ---- | -------- | ------------------------ |
| 1 | 10 20 30 | 30 is largest. |
| 2 | 25 25 25 | These numbers are equal. |
:::
### Solution
```C++=
#include <iostream>
using namespace std;
int main(){
unsigned long long a,b,c,max;
cin>>a>>b>>c;
if(a==b&&a==c) {cout<<"These numbers are equal.";}
else{
max=a;
if(b>max) {max=b;}
if(c>max) {max=c;}
cout<<max<<" is largest.";
}
}
```
## Palindrome
:::success
**Question**
A palindrome is a number or a text phrase that read the same backward as forward.
For example, each of the folloing fiving-digit integers is a palindrome: 12321, 55555, 45554 and 11611.
Write a program that read a non-negative integer and determines whether it's a palindrome.
| Test | Input | Result |
| ---- | ----- | -------------- |
| 1 | 121 | Palindrome |
| 2 | 1234 | Not Palindrome |
:::
### Solution
```C++=
#include <iostream>
using namespace std;
int main() {
unsigned long long int a,reverse=0,a_new;
cin>>a;
a_new=a;
while(a/10!=0){
reverse=(a%10)+reverse*10;
a/=10;
}
reverse=(a%10)+reverse*10;
if (a_new == reverse){
cout<<"Palindrome";
}
else{
cout<<"Not Palindrome";
}
}
```
## Power fuction
:::success
**Question**
Write a recursive function power(base, exponent) that invoked, return baseexponent.
(Hint: Only implement a function without implemnt the main() function.
```C++
return_type power (type base, type exponent) {
return ooo ;
}
```
| Test | Input | Result |
| ---- | -------------------- | ------ |
| 1 | cout << power(2,3) ; | 8 |
| 2 | cout << power(3,2) ; | 9 |
:::
### Solution
```C++=
long long int power(long long int x,long long int y){
if (y==0){
return 1;
}
else {
return x*power(x,y-1);
}
}
```
## Reverse the number
:::success
**Question**
Write a program that read a non-negative integer, separate the integer into its digits and outputs them in reverse order by two spaces each.
| Test | Input | Result |
| ---- | ----- | ---------- |
| 1 | 4315 | 5 1 3 4 |
| 2 | 12 | 2 1 |
:::
### Solution
```C++=
#include <iostream>
using namespace std;
int main() {
unsigned long long int a;
cin>>a;
while (a/10!=0){
cout<<a%10<<" ";
a/=10;
}
cout<<a%10;
}
```
> **後記:** 題目測資很大要用 `unsigned long long int`
## Pythagorean triple
:::success
**Question**
The set of three integer values for the lengths of the sides of a triangle is called a Pythagorean triple.
Read maximum possible side length (N) of the triangle and output how many pythagorean triples are found which all sides are not larger than N?
| Test | Input | Result |
| ---- | ----- | ------ |
| 1 | 5 | 1 |
| 2 | 13 | 3 |
:::
### Solution
```C++=
#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
int sum=0;
for (int i=1;i<=n;i++){
for (int g=1;g<i;g++){
for (int j=1;j<g;j++){
if (i*i==g*g+j*j){
sum+=1;
}
}
}
}
cout<<sum;
}
```
> **後記:** Pythagorean是畢達哥拉斯==,所以題目在問right triangle.
## Soda drinker
:::success
**Question**
Tim is an absolutely obsessive soda drinker, he simply cannot get enough. Most annoyingly though, he almost never has any money, so his only obvious legal way to obtain more soda is to take the money he gets when he recycles empty soda bottles to buy new ones. In addition to the empty bottles resulting from his own consumption he sometimes find empty bottles in the street. One day he was extra thirsty, so he actually drank sodas until he couldn’t afford a new one.
**Input**
The description of each test case is given below:
Three non-negative integers e, f, c, where e < 1000 equals the number of empty soda bottles in Tim’s possession at the start of the day, f < 1000 the number of empty soda bottles found during the day, and 1 < c < 2000 the number of empty bottles required to buy a new soda.
**Output**
For each test case print how many sodas did Tim drink on his extra thirsty day? Look at the sample output for details.
| Test | Input | Result |
| ---- | ----- | ------ |
| 1 | 9 0 3 | 4 |
| 2 | 5 5 2 | 9 |
:::
### Solution
```C++=
#include <iostream>
using namespace std;
int main() {
int e,f,c;
cin>>e>>f>>c;
int can=e+f;
int sum=0;
while ( can/c != 0 ){
sum+=can/c;
can=can/c+can%c;
}
cout<<sum;
return 0;
}
```