A.Repeated Sequence
Problem:
There is a sequence arranged in the following order:
1,2,3,4,0, 1,2,3,4,0, 1,2,3,4,0, 1,2,3,4,0, ⋯
What is the n-th number in this sequence?
Input Format:A single integer: n
Output Format:A single integer: the value of the n-th number in the sequence
SampleInput1:1
SampleOutput1:1
SampleInput2:10
SampleOutput2:0
Note (Data Range):
1≤n≤1000000
Time Limit: 1000ms
Memory Limit: 512MiB
Solution→
Sample Analysis:
Sample Input 1: 1 → Output 1
The first number in the sequence is the first element of the cycle “1”.
Sample Input 2: 10 → Output 0
The sequence repeats with a cycle of 1,2,3,4,0, the 10th number is the last element of the second cycle “0” (each cycle has 5 numbers, 10=2×5).
Understanding the Problem:
The sequence repeats indefinitely with a fixed cycle of 1,2,3,4,0. Based on the input position n (1≤n≤1e6), output the corresponding number at that position.
Problem Solving Approach:
Identify the cycle: The cycle of the sequence is [1,2,3,4,0], with a length of 5, meaning it repeats every 5 numbers.
Locate the position within the cycle: For the n-th number, calculate its offset within the cycle (i.e., the remainder of n divided by the cycle length).
Map the result: Based on the offset, map it to the specific number within the cycle.
Algorithm Steps:
Calculate the offset: Since the sequence counts from 1, use (n-1) % 5 to get an offset of 0~4 (0 corresponds to the first element of the cycle, 4 corresponds to the fifth element of the cycle).
Conditional check: Use an if-else if structure to map the offset to the number within the cycle:
Offset 0 → 1
Offset 1 → 2
Offset 2 → 3
Offset 3 → 4
Offset 4 → 0
Basic Knowledge:
Modulus operation: (n-1) % 5 is used to calculate the position within the cycle, mapping any n to the range of 0~4, reflecting the periodic pattern.
Conditional checks (if-else if): A multi-branch structure is used to match the corresponding number based on different offsets, achieving a simple mapping logic.
Code Implementation (C++):
// C++ code implementation here
B.Lucky 7
Problem:
During a mall promotion, n lucky customers each received a lottery ticket numbered from 1 to n. The merchant stipulates that customers with tickets ending in 7 or whose numbers are divisible by 7 can enjoy a free meal.
For a given n, please help calculate how many people can enjoy the free meal.
Input Format:
Input a positive integer n, indicating there are n customers in total.
Output FormatOutput how many people can enjoy the free meal
SampleInput1:17
SampleOutput1:3
SampleInput2:21
SampleOutput2:4
Note (Data Range):
1≤n≤90
Time Limit: 1000ms
Memory Limit: 128MiB
Solution→
Sample Analysis:
Sample Input 1: 17
Among the numbers from 1 to 17, the numbers that meet the conditions are: 7 (divisible by 7), 14 (divisible by 7), 17 (ending in 7), totaling 3, thus output 3.
Sample Input 2: 21
Among the numbers from 1 to 21, the numbers that meet the conditions are: 7, 14, 17, 21 (the first three as above, 21 is divisible by 7), totaling 4, thus output 4.
Understanding the Problem:
Given a positive integer n, count the number of tickets from 1 to n that satisfy either of the following conditions:
The ticket number ends in 7 (i.e., the unit digit is 7);
The ticket number is divisible by 7 (i.e., the remainder when divided by 7 is 0).
Problem Solving Approach:
Iterate through all integers from 1 to n, checking each number to see if it meets the free meal conditions;
For each number, check if it ends in 7 (using the result of modulus 10 to see if it is 7) or if it is divisible by 7 (using the result of modulus 7 to see if it is 0);
Count the total number of numbers that meet the conditions, which is the answer.
Algorithm Steps:
Input the positive integer n;
Initialize a counter count to 0, used to record the number of satisfying ticket numbers;
Loop through i from 1 to n:
If i % 10 == 7 (ending in 7) or i % 7 == 0 (divisible by 7), then increment count by 1;
After the loop ends, output the value of count.
Basic Knowledge:
Modulus operation (%): In C++, a % b represents the remainder of a divided by b.
For example: 17 % 10 = 7 (used to check if the unit digit is 7);
14 % 7 = 0 (used to check if it is divisible by 7).
Loop structure: Use a for loop to iterate through all integers from 1 to n, implementing the logic of checking one by one.
Conditional checks: Use if statements combined with logical operators || (or) to check if the ticket number meets any of the free meal conditions.
Code Implementation (C++):
// C++ code implementation here
C.What Day of the Week
Problem:
Using natural numbers to represent the days of the week, Monday is represented by 1, Tuesday by 2, and so on, with the unique exception that Sunday is represented by 0.
Assuming today is day a of the week, what day will it be after b days?
Input Format:
The first line: two natural numbers representing a and b
Output Format:
A single natural number: representing what day it will be after b days
SampleInput1:3 1
SampleOutput1:4
SampleInput2:3 11
SampleOutput2:0
Note (Data Range):
0≤a≤6
0≤b≤10^9
Time Limit: 1000ms
Memory Limit: 512MiB
Solution→
Sample Analysis:
Sample Input 1: 3 1
Today is Wednesday (3), one day later is Thursday, corresponding to number 4, output 4.
Sample Input 2: 3 11
Today is Wednesday (3), after 11 days: 3 + 11 = 14, 14 divided by 7 gives a remainder of 0, corresponding to Sunday, output 0.
Understanding the Problem:
Using natural numbers to represent the week: 1 = Monday, 2 = Tuesday, …, 6 = Saturday, 0 = Sunday.
Given that today is day a of the week, find the day of the week after b days.
Problem Solving Approach:
The week has a periodic nature, repeating every 7 days. Therefore, the day of the week after b days only depends on the total days (today’s day + b days) modulo 7.
Core logic: Total days = a + b, using total days modulo 7, the result is the target day of the week (when the remainder is 0, it corresponds to Sunday, in line with the problem statement).
Algorithm Steps:
Read the two natural numbers a (today’s day) and b (days).
Calculate the total: total = a + b.
Calculate the total modulo 7: result = total % 7.
Output result, which is the day of the week after b days.
Basic Knowledge:
Modulus operation: x % 7 results in a range of 0~6, exactly corresponding to the week representation of 0~6, utilizing periodicity to simplify calculations.
Integer addition: a + b represents the total “offset” after b days from today, combined with modulus can directly map to the target day of the week.
Code Implementation (C++):
// C++ code implementation here
D.Field Charges
Problem:
A certain field operates from 12 PM to 10 PM daily, with the following charging rules:
From 12 PM to 6 PM (excluding 6 PM), the charge is 500 yuan per hour
From 6 PM to 10 PM (including 6 PM), the charge is 700 yuan per hour
Someone books the field, starting at x o’clock, for a duration of y hours. How much is the total fee?
Input Format:
Two integers, representing the booking start time X and duration Y hours.
Output Format:Output an integer, representing the total cost of booking the field
SampleInput1:17 3
SampleOutput1:1900
Note (Data Range):
It is guaranteed that both the start and end times are within the operating hours of the field
Time Limit: 1000ms
Memory Limit: 512MiB
Solution→
Sample Analysis:
Sample Input 1: 17 3
The start time is 17 o’clock, lasting 3 hours, the end time is 17+3=20 o’clock.
17 o’clock to 18 o’clock: 1 hour, within the 12-6 PM period, the cost is 1×500=500 yuan.
18 o’clock to 20 o’clock: 2 hours, within the 6-10 PM period, the cost is 2×700=1400 yuan.
Total cost: 500+1400=1900 yuan, consistent with the output.
Understanding the Problem:
Field operating hours: 12 PM to 10 PM, divided into two charging periods:
12 PM ≤ time < 6 PM: 500 yuan per hour.
6 PM ≤ time ≤ 10 PM: 700 yuan per hour.
Given the booking start time x (hour) and duration y (hour), calculate the total cost (ensuring both start and end times are within operating hours).
Problem Solving Approach:
Determine the time range: Start time x, end time x + y.
Divide the charging periods:
If end time ≤ 6 PM: All time is within 12-6 PM, total cost = y × 500.
If start time ≥ 6 PM: All time is within 6-10 PM, total cost = y × 700.
If the time crosses 6 PM: Split into two segments for calculation, duration before 6 PM ×500 + duration after 6 PM ×700.
Algorithm Steps:
Read the input start time x and duration y, calculate end time end = x + y.
Determine the charging period:
If end ≤ 6: Total cost = y * 500.
If x ≥ 6: Total cost = y * 700.
Otherwise (crossing 6 PM):
Duration before 6 PM: t1 = 6 – x.
Duration after 6 PM: t2 = end – 6.
Total cost = t1 * 500 + t2 * 700.
Output the total cost.
Basic Knowledge:
Branch judgment: Calculate the cost based on whether the time crosses the charging node (6 PM).
Integer operations: Calculate the duration of the two periods accurately through the difference between start and end times.
Code Implementation (C++):
// C++ code implementation here
E.Print Cubes
Problem:
Given a positive integer n, print the first n cubes (the first cube is 1, the second cube is 8, the third cube is 27).
Input Format:
The first line: a single positive integer n
Output Format:n lines: each line contains one cube
SampleInput1:
3
SampleOutput1:
1
8
27
SampleInput2:
5
SampleOutput2:
1
8
27
64
125
Note (Data Range):
1≤n≤100
Time Limit: 100ms
Memory Limit: 512MiB
Solution→
Sample Analysis:
Sample Input 1: 3
The first 3 cubes are: 1³=1, 2³=8, 3³=27, thus output 3 lines of corresponding results.
Sample Input 2: 5
The first 5 cubes are: 1³=1, 2³=8, 3³=27, 4³=64, 5³=125, output 5 lines of corresponding results.
Understanding the Problem:
Given a positive integer n, output the first n cubes, where the k-th cube is k³ (k starts from 1, sequentially as 1,2,…,n).
Problem Solving Approach:
Clarify the definition of cubes: The k-th cube is k raised to the power of three (i.e., k × k × k).
Loop to calculate and output: Iterate through each integer k from 1 to n, calculate k³ and print line by line.
Algorithm Steps:
Read the input positive integer n.
Loop variable k from 1 to n (inclusive):
Calculate the current k’s cube: k * k * k.
Print the calculated result, one per line.
Basic Knowledge:
Loop structure: Use a for loop to iterate through integers from 1 to n, implementing repeated calculations and outputs.
Integer operations: Calculate cubes (k*k*k) through multiplication, suitable for the problem’s range of n≤100 (the maximum cube is 100³=1000000, no overflow issues).
Code Implementation (C++):
// C++ code implementation here