時間:2021/12/9(第五次社課)
語言限制:C、C++、Python、Java、Golang、JavaScript
輸入、輸出格式將與每題的範例輸入、輸出相同,請 Python 使用者特別留意。
Java 使用者,請以 Main
為 class 名稱,否則無法編譯。
依序讀入一個整數、一個浮點數以及一個字元到三個不同的變數中,並將他們依照浮點數、字元、整數的順序分別印出來(以空白隔開)。
範例輸入
5 2.09 g
範例輸出
2.09 g 5
#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;
}
#include <iostream>
using namespace std;
int main() {
int a;
float b;
char c;
cin >> a >> b >> c;
cout << b << " " << c << " " << a << "\n";
}
a, b, c = map(str, input().split())
a = int(a)
b = float(b)
# c 的型別為 str
# c[0] 的型別為 chr
print(f"{b} {c[0]} {a}")
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);
}
}
依序讀入兩個整數,並依照順序輸出他們經過加、減、乘、除、模(%
)運算後的結果(以空白隔開)。
範例輸入
5 2
範例輸出
7 3 10 2.5 1
#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;
}
#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";
}
a, b = map(int, input().split())
# a / b 為一般除法
# a // b 為整數除法
print(f"{a+b} {a-b} {a*b} {a/b} {a%b}")
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);
}
}
#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;
}
#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";
}
a, b = map(int, input().split())
# a / b 為一般除法
# a // b 為整數除法
print(f"{a+b} {a-b} {a*b} {a/b} {a%b}")
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
6 9 54
範例輸出1
Equal
範例輸入2
10 7 90
範例輸出2
Less
#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;
}
#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";
}
}
a, b, c = map(int, input().split())
if a*b == c:
print("Equal")
elif a*b < c:
print("Less")
else:
print("Greater")
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
迴圈,依序印出所有介在兩者之間的整數(以空白隔開)。
範例輸入
2 5
範例輸出
2 3 4 5
#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;
}
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
for(int i = a; i <= b; i++) {
cout << i << " ";
}
}
a, b = map(int, input().split())
for i in range(a, b+1):
print(i, end = " ")
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
迴圈,將第一個整數一直乘以二,直到其大於第二個整數,並印出其值。
範例輸入
1 1024
範例輸出
2048
#include <stdio.h>
int main() {
int a, b;
scanf("%d%d", &a, &b);
while(a <= b) {
a *= 2;
}
printf("%d\n", a);
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
while(a <= b) {
a *= 2;
}
cout << a << "\n";
}
a, b = map(int, input().split())
while a <= b:
a *= 2
print(a)
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
一隻蠕蟲位於
範例輸入
第一行有一個正整數
接下來有
2
10 3 1
10 4 1
範例輸出
每筆測資輸出一行,每行一個正整數代表蠕蟲花多少分鐘爬出井口。
9
5
#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;
}
#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";
}
}
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)
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。現在給你兩個正整數
範例輸入
第一行有一個正整數
3
1 3
1 4
2 5
範例輸出
每筆測資輸出一行,每行一個正整數,代表
1
2
3
#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);
}
}
#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";
}
}
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)
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);
}
}
}