# 中和資研小測驗
時間:2021/12/9(第五次社課)
語言限制:C、C++、Python、Java、Golang、JavaScript
輸入、輸出格式將與每題的範例輸入、輸出相同,請 Python 使用者特別留意。
Java 使用者,請以 ```Main``` 為 class 名稱,否則無法編譯。
## (一)變數、輸入輸出
依序讀入一個整數、一個浮點數以及一個字元到三個不同的變數中,並將他們依照浮點數、字元、整數的順序分別印出來(以空白隔開)。
**範例輸入**
```c
5 2.09 g
```
**範例輸出**
```c
2.09 g 5
```
:::spoiler **參考解答100%**
- C
```c=
#include <stdio.h>
int main() {
int a;
float b;
char c;
scanf("%d%f %c", &a, &b, &c);
printf("%f %c %d\n", b, c, a);
return 0;
}
```
- C++
```cpp=
#include <iostream>
using namespace std;
int main() {
int a;
float b;
char c;
cin >> a >> b >> c;
cout << b << " " << c << " " << a << "\n";
}
```
- Python
```python=
a, b, c = map(str, input().split())
a = int(a)
b = float(b)
# c 的型別為 str
# c[0] 的型別為 chr
print(f"{b} {c[0]} {a}")
```
- Java
```java=
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int a;
float b;
char c;
Scanner sc = new Scanner(System.in);
a = sc.nextInt();
b = sc.nextFloat();
c = sc.next().charAt(0);
System.out.printf("%f %c %d\n", b, c, a);
}
}
```
:::
## (二)五則運算
依序讀入兩個整數,並依照順序輸出他們經過加、減、乘、除、模(```%```)運算後的結果(以空白隔開)。
**範例輸入**
```c
5 2
```
**範例輸出**
```c
7 3 10 2.5 1
```
:::spoiler **參考解答60%**
- C
```c=
#include <stdio.h>
int main() {
int a, b;
scanf("%d%d", &a, &b);
printf("%d %d %d %f %d\n", a+b, a-b, a*b, 1.0*a/b, a%b);
return 0;
}
```
- C++
```cpp=
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << a+b << " ";
cout << a-b << " ";
cout << a*b << " ";
cout << 1.0*a/b << " ";
cout << a%b << "\n";
}
```
- Python
```python=
a, b = map(int, input().split())
# a / b 為一般除法
# a // b 為整數除法
print(f"{a+b} {a-b} {a*b} {a/b} {a%b}")
```
- Java
```java=
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int a, b;
Scanner sc = new Scanner(System.in);
a = sc.nextInt();
b = sc.nextInt();
System.out.printf("%d %d %d %f %d\n", a+b, a-b, a*b, 1.0*a/b, a%b);
}
}
```
:::
:::spoiler **參考解答40%**
- C
```c=
#include <stdio.h>
int main() {
long long int a, b;
scanf("%ld%ld", &a, &b);
printf("%ld %ld %ld %f %ld\n", a+b, a-b, a*b, 1.0*a/b, a%b);
return 0;
}
```
- C++
```cpp=
#include <iostream>
using namespace std;
int main() {
long long int a, b;
cin >> a >> b;
cout << a+b << " ";
cout << a-b << " ";
cout << a*b << " ";
cout << 1.0*a/b << " ";
cout << a%b << "\n";
}
```
- Python
```python=
a, b = map(int, input().split())
# a / b 為一般除法
# a // b 為整數除法
print(f"{a+b} {a-b} {a*b} {a/b} {a%b}")
```
- Java
```java=
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
long a, b;
Scanner sc = new Scanner(System.in);
a = sc.nextLong();
b = sc.nextLong();
System.out.printf("%d %d %d %f %d\n", a+b, a-b, a*b, 1.0*a/b, a%b);
}
}
```
:::
## (三)條件判斷
依序讀入三個整數,判斷前兩個整數相乘後與第三個整數的關係,
如果等於第三個數,輸出"Equal"
如果小於第三個數,輸出"Less"
如果大於第三個數,輸出"Greater"
**範例輸入1**
```c
6 9 54
```
**範例輸出1**
```c
Equal
```
**範例輸入2**
```c
10 7 90
```
**範例輸出2**
```c
Less
```
:::spoiler **參考解答100%**
- C
```c=
#include <stdio.h>
int main() {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
if(a*b == c) {
printf("Equal\n");
} else if(a*b < c) {
printf("Less\n");
} else {
printf("Greater\n");
}
return 0;
}
```
- C++
```cpp=
#include <iostream>
using namespace std;
int main() {
int a, b, c;
cin >> a >> b >> c;
if(a*b == c) {
cout << "Equal\n";
} else if(a*b < c) {
cout << "Less\n";
} else {
cout << "Greater\n";
}
}
```
- Python
```python=
a, b, c = map(int, input().split())
if a*b == c:
print("Equal")
elif a*b < c:
print("Less")
else:
print("Greater")
```
- Java
```java=
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int a, b, c;
Scanner sc = new Scanner(System.in);
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();
if(a*b == c) {
System.out.println("Equal");
} else if(a*b < c) {
System.out.println("Less\n");
} else {
System.out.println("Greater\n");
}
}
}
```
:::
## (四)迴圈[一]
依序讀入兩個整數,並利用 ```for``` 迴圈,依序印出所有介在兩者之間的整數(以空白隔開)。
**範例輸入**
```c
2 5
```
**範例輸出**
```c
2 3 4 5
```
:::spoiler **參考解答**
- C
```c=
#include <stdio.h>
int main() {
int a, b;
scanf("%d%d", &a, &b);
for(int i = a; i <= b; i++) {
printf("%d ", i);
}
return 0;
}
```
- C++
```cpp=
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
for(int i = a; i <= b; i++) {
cout << i << " ";
}
}
```
- Python
```python=
a, b = map(int, input().split())
for i in range(a, b+1):
print(i, end = " ")
```
- Java
```java=
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int a, b;
Scanner sc = new Scanner(System.in);
a = sc.nextInt();
b = sc.nextInt();
for(int i = a; i <= b; i++) {
System.out.printf("%d ", i);
}
}
}
```
:::
## (五)迴圈[二]
依序讀入兩個整數,並利用 ```while``` 迴圈,將第一個整數一直乘以二,直到其大於第二個整數,並印出其值。
**範例輸入**
```c
1 1024
```
**範例輸出**
```c
2048
```
:::spoiler **參考解答**
- C
```c=
#include <stdio.h>
int main() {
int a, b;
scanf("%d%d", &a, &b);
while(a <= b) {
a *= 2;
}
printf("%d\n", a);
return 0;
}
```
- C++
```cpp=
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
while(a <= b) {
a *= 2;
}
cout << a << "\n";
}
```
- Python
```python=
a, b = map(int, input().split())
while a <= b:
a *= 2
print(a)
```
- Java
```java=
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int a, b;
Scanner sc = new Scanner(System.in);
a = sc.nextInt();
b = sc.nextInt();
while(a <= b) {
a *= 2;
}
System.out.println(a);
}
}
```
:::
## (六)綜合練習
> 題目出處:HDU - Climbing Worm
一隻蠕蟲位於 $n$ 公分深的井的底部。牠每分鐘會向上爬 $u$ 公分,但是必須休息一分鐘才能再次向上爬。在休息的時候,牠會往下滑落 $d$ 公分,之後牠將重複向上爬和休息的過程。試問蠕蟲爬出井口花費了多長的時間?我們將不足一分鐘的部分算做一分鐘。如果蠕蟲向上爬後剛好到了井口,我們也算做蠕蟲已經爬出井口。
**範例輸入**
第一行有一個正整數 $t$,代表接下來有幾筆測資。
接下來有 $t$ 行,每行有三個正整數,分別為 $n$、$u$、$d$。
```c
2
10 3 1
10 4 1
```
**範例輸出**
每筆測資輸出一行,每行一個正整數代表蠕蟲花多少分鐘爬出井口。
```c
9
5
```
:::spoiler **參考解答100%**
- C
```c=
#include <stdio.h>
int main() {
int t;
int n, u, d;
scanf("%d", &t);
for(int i = 0; i < t; i++) {
scanf("%d%d%d", &n, &u, &d);
int time = 0;
int dist = 0;
while(1) {
dist += u;
time++;
if(dist >= n) {
break;
}
dist -= d;
time++;
}
printf("%d\n", time);
}
return 0;
}
```
- C++
```cpp=
#include <iostream>
using namespace std;
int main() {
int t;
int n, u, d;
cin >> t;
for(int i = 0; i < t; i++) {
cin >> n >> u >> d;
int time = 0;
int dist = 0;
while(true) {
dist += u;
time++;
if(dist >= n) {
break;
}
dist -= d;
time++;
}
cout << time << "\n";
}
}
```
- Python
```python=
t = int(input())
for i in range(t):
n, u, d = map(int, input().split())
time = 0
dist = 0
while True:
dist += u
time += 1
if dist >= n:
break
dist -= d
time += 1
print(time)
```
- Java
```java=
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int t;
int n, u, d;
Scanner sc = new Scanner(System.in);
t = sc.nextInt();
for(int i = 0; i < t; i++) {
n = sc.nextInt();
u = sc.nextInt();
d = sc.nextInt();
int time = 0;
int dist = 0;
while(true) {
dist += u;
time++;
if(dist >= n) {
break;
}
dist -= d;
time++;
}
System.out.println(time);
}
}
}
```
:::
## (七)加分題
> 題目出處:LeetCode - Hamming Distance
我們說兩個整數間的「**漢明距離**」為兩個整數各自轉成二進位後,有多少個相對應位置的位元是不同的。舉例來說,1 轉成二進位後為 0001;3 轉成二進位後為 0011,兩者間有 1 個位元是不同的(右起第 2 個),所以 1 和 3 的漢明距離為 1。現在給你兩個正整數 $x$ 及 $y$,試計算兩數間的漢明距離。
**範例輸入**
第一行有一個正整數 $t$,代表有幾筆測資,接下來有 $t$ 行,每行有兩個正整數 $x$ 和 $y$。
```c
3
1 3
1 4
2 5
```
**範例輸出**
每筆測資輸出一行,每行一個正整數,代表 $x$ 與 $y$ 的漢明距離。
```c
1
2
3
```
:::spoiler **參考解答100%**
- C
```c=
#include <stdio.h>
int main() {
int t;
scanf("%d", &t);
for(int i = 0; i < t; i++) {
int x, y;
scanf("%d%d", &x, &y);
int z = x ^ y;
int w = 0;
while(z) {
if(z & 1) {
w++;
}
z >>= 1;
}
printf("%d\n", w);
}
}
```
- C++
```cpp=
#include <iostream>
using namespace std;
int main() {
int t;
cin >> t;
for(int i = 0; i < t; i++) {
int x, y;
cin >> x >> y;
int z = x ^ y;
int w = 0;
while(z) {
if(z & 1) {
w++;
}
z >>= 1;
}
cout << w << "\n";
}
}
```
- Python
```python=
t = int(input())
for i in range(t):
x, y = map(int, input().split())
z = x ^ y
w = 0
while z:
if z & 1:
w += 1
z >>= 1
print(w)
```
- Java
```java=
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t;
t = sc.nextInt();
for(int i = 0; i < t; i++) {
int x, y;
x = sc.nextInt();
y = sc.nextInt();
int z = x ^ y;
int w = 0;
while(z > 0) {
if((z & 1) == 1) {
w++;
}
z >>= 1;
}
System.out.println(w);
}
}
}
```
:::