1. Time Converter
Problem Description
As we all know, 1 hour = 3600 seconds. Please write a program to create a time converter that takes seconds as input and outputs the corresponding hours, discarding any fractions of an hour. For example, input 3600, which is exactly 1 hour, should output 1; input 7206, which exceeds 2 hours but is less than 3 hours, should output 2.
Input:
A single line containing an integer representing seconds s.
Output:
A single line containing an integer representing the corresponding hours h.
Data Range:
s and h are both positive integers within the int range.
Input Sample 1:
7206
Output Sample 1:
2
Input Sample 2:
3600
Output Sample 2:
1
// Reference Code
#include<iostream>
using namespace std;
int main() {
int s , h;
cin >> s;
h = s / 3600;
cout << h << endl;
return 0;
}
Division operation (/): When both the dividend and divisor are integers, it performs integer division, meaning the result is the quotient (discarding the decimal part).
2. Contaminated Reagent
Problem Description
There are four types of chemical reagents in the laboratory, numbered 1, 2, 3, and 4. Only one reagent is contaminated, which can cause a dangerous reaction when mixed, while the other three are safe. Researcher A mixed reagents 1 and 2, while researcher B mixed reagents 1 and 3. Given the experimental results of A and B, where 0 represents a dangerous reaction and 1 represents safety, deduce the type number of the contaminated reagent.
Input:
Two integers (0 or 1) representing the experimental results of A and B, separated by a space.
Output:
An integer (1-4) representing the type number of the contaminated reagent.
Data Range:
Input consists only of 0 and 1.
Input Sample 1:
1 1
Output Sample 1:
4
Input Sample 2:
0 1
Output Sample 2:
2
// Reference Code
#include<iostream>
using namespace std;
int main() {
int a , b;
cin >> a >> b;
for(int x = 1; x <= 4; x++) {
int res_a = (x == 1 || x == 2) ? 0 : 1;
int res_b = (x == 1 || x == 3) ? 0 : 1;
if(res_a == a && res_b == b) {
cout << x << endl;
break;
}
}
return 0;
}
According to the original problem, when a=0 and b=0, output 1.
Test this code:
When input a=0, b=0, loop i=1 to 4:
i=1:
res_a: (1==1 || 1==2) → true → a==0 → 0==0 → true
res_b: (1==1 || 1==3) → true → b==0 → 0==0 → true → Satisfied, output 1.
i=2:
res_a: (2==1 || 2==2) → true → a==0 → 0==0 → trueres_b: (2==1 || 2==3) → false → b==1 → 0==1 → false → Not satisfied.
i=3:
res_a: (3==1 || 3==2) → false → a==1 → 0==1 → false → Not satisfied.
i=4:
res_a: false → a==1 → 0==1 → false → Not satisfied.
Thus, only i=1 satisfies, output is correct.
Input: A=0 (danger), B=0 (danger) → Output: 1
Input: A=0 (danger), B=1 (safe) → Output: 2
Input: A=1 (safe), B=0 (danger) → Output: 3
Input: A=1 (safe), B=1 (safe) → Output: 4
// Reference Code
#include<iostream>
using namespace std;
bool isLeap(int y) {
return((y % 400 == 0) || (y % 4 == 0 && y % 100 != 0));
}
int main() {
int year;
cin >> year;
while(year > 0) {
if(isLeap(year)) {
cout << year << endl;
return 0;
}
year--;
}
return 0;
}
Test Cases:
Input Output Description
2020 2020 Current leap year
2021 2020 Previous leap year
1900 1896 Century year does not satisfy 400 condition
2000 2000 Century year satisfies 400 condition
3. Sensor Data Statistics
Problem Description
Recently, Xiaoming installed n sensors in an autonomous vehicle project. These sensors collect road environment data in real-time. He found that when a certain digit in the sensor data is a multiple of 3, it may correspond to a critical road condition signal, and that digit should be counted as a valid signal. Please write a program to analyze the data from n sensors and calculate the sum of all valid signal digits.
Input:
The first line contains an integer n (1≤n≤1000), representing the number of sensors. The second line contains n integers, representing the raw data collected by the sensors (values are positive integers within the int range).
Output:
An integer representing the sum of valid digits in the n sensor data.
Data Range:
1 ≤ n ≤ 1000
Input Sample:
36 42 15
Output Sample:
9
// Reference Code
#include<iostream>
using namespace std;
int main() {
int n , num;
cin >> n;
int sum=0;
for(int i=0; i < n; i++) {
cin >> num;
while(num > 0) {
int x = num % 10;
if(x % 3 == 0) sum += x;
num /= 10;
}
}
cout << sum << endl;
return 0;
}
4. Smart Lighting System
Problem Description
The smart lighting system of the Golden Hall in Vienna adjusts the lighting based on the frequency of musical notes. When the difference in the occurrence of the highest and lowest frequency notes in the piano score is even, the lighting will display a rainbow spectrum; otherwise, the concert hall will maintain basic lighting. Assume maxn represents the occurrence of the highest frequency note, and minn represents the occurrence of the lowest frequency note.
Input:
A single line containing a string of musical notes, consisting only of uppercase letters A-G, with a length of less than 100.
Output:
Two lines of output.
First line: If the rainbow spectrum is triggered, output “Luminous Harmony”; otherwise, output “Silent Keys”;
Second line: Output the difference maxn – minn.
Data Range:
1 ≤ n ≤ 1000
Input Sample 1:
BABABC
Output Sample 1:
Luminous Harmony
2
Input Sample 2:
ABCDGAAA
Output Sample 2:
Silent Keys
3
Input Sample 3:
AAAAAAAA
Output Sample 3:
Luminous Harmony
0
// Reference Code
#include<iostream>
#include<string>
using namespace std;
int main() {
string s;
cin >> s;
int count[7]={0};
for(char c:s) {
count[c - 'A']++;
}
int maxn = 0;
int minn = 100;
for(int i=0; i < 7; i++) {
if(count[i] > 0) {
maxn = max(maxn, count[i]);
minn = min(minn, count[i]);
}
}
if((maxn - minn) % 2 == 0)
cout << "Luminous Harmony" << endl;
else
cout << "Silent Keys" << endl;
cout << maxn - minn << endl;
return 0;
}
