Question 1
If a number p is a prime number and is also a divisor of the integer a, then p is called a prime factor.
How many prime factors are there in 2024?
Step 1: Decomposition 2024
First, 2024 is an even number, which means it can be divisible by 2. Let's start with 2 to perform division decomposition:
2024÷2=1012
1012÷2=506
506÷2=253
253 is not an even number, so it cannot be divisible by 2 anymore. Let's check whether 253 can be divisible by 3. Because the sum of 253 numbers 2+5+3=10, 10 cannot be divisible by 3, so 253 cannot be divisible by 3.
Next, check whether 253 can be divisible by 5. The end of 253 is not 0 or 5, so it cannot be divisible by 5.
Then check whether 253 can be divisible by 11. Calculate 253 divided by 11:
253÷11=23
Therefore, 253 can be divided by 11 and the quotient is 23. 23 is a prime number and cannot be decomposed.
Step 2: Prime factor decomposition
Through the above steps, we can obtain the prime factor decomposition of 2024:
2024=23×11×232024=23×11×23
Step 3: Number of prime factors
The prime factors include 2, 11, and 23. Therefore, there are 3 prime factors in 2024, namely 2, 11 and 23.
Answer:
The prime factors in 2024 are3。
Question 2
For an integer n , we define a root transformation that will change n into the integer part after the root sign. That is, it becomes the largest number among the sum of squares that does not exceed n.
For example, 20 will become 4 after the root transformation, if it is again root transformation, it will become 2, if it is again root transformation, it will become 1 after the root transformation.
May I ask, how many times will it become 1 after 2024 after root transformation?
The initial value is 2024.
The first root transformation: 2024≈45.0112024≈45.011, take the integer part to 45.
The second root transformation: 45≈6.70845≈6.708, take the integer part to 6.
The third root transformation: 6≈2.4496≈2.4499, take the integer part to 2.
The fourth root transformation: 2≈1.4142≈1.414, take the integer part to 1.
Therefore, 2024 passed4The root change will become 1.
Code calculation:
import math def abc(n): c = 0 while n > 1: n = (n) c += 1 return c n = 2024 a = abc(n) print(a)
Question 3
Xiaolan has many small cubes of 1x1x1, which he can use multiple cubes to form larger cubes.
For example, Xiaolan can use 8 small cubes to form a large cube, with 2 on each side.
For example, Xiaolan can use 27 small cubes to form a large cube, with 3 on each side.
Now, Xiaolan has 2024 small cubes. He wants to buy some more small cubes to be used to piece a super large cube. He requires all small cubes to be used, and the length of each side of the grouped large cube is equal. May I ask, how many small cubes do Xiaolan need to buy at least?
The volume of a large cube:The volume of a large cube with a side length n is n**3 (i.e. n×n×n).
Target:We need to find an n such that n**3≥2024, and n**3−2024 is the smallest, that is, the minimum number of cubes to be purchased.
Calculate the cube root of 2024 to estimate a close nn. We can use a calculator to calculate:
2024≈12.6348
Therefore, the side length n should be at least 13, because 123=1728 is less than 2024, and 133=2197 is greater than 2024.
133=2197 is the volume of a large cube.
Xiaolan already has 2024 small cubes, so the quantity he needs to buy is:
2197−2024=173
Code calculation:
n = 2024 i = 13 while i**3 < n: i += 1 x = i**3 a = x - n print("The minimum number of cubes that Xiaolan needs to buy:", a)
Question 4
If the date of a date ends with 1 (1st, 11th, 21st, 31st, and 31st) and is Monday, then this date is called a good date.
How many good dates are there from January 1, 1901 to December 31, 2024?
Tip: January 1, 1901 is Tuesday.
Determine the date range:From January 1, 1901 to December 31, 2024.
Understand the "1st", "11th", "21st", and "31st" of each month:These are dates ending with 1. We need to check if these dates of each month are Mondays.
Determine the specific situation of each month of each year:Among the different months of each year, the dates of some months may appear on the 31st, while some months (such as February, April, June, etc.) do not have the 31st.
Determine the day of the week for each date:Using a known start date (January 1, 1901 is Tuesday), we can determine whether it is Monday by calculating the day of the week of each date.
Code calculation:
import datetime start_date = (1901, 1, 1) good_dates_count = 0 # Traverse dates from January 1, 1901 to December 31, 2024for year in range(1901, 2025): for month in range(1, 13): # Check the 1st, 11th, 21st and 31st of the month (if there are 31st days) for day in [1, 11, 21]: try: # Create this date current_date = (year, month, day) # If the date is Monday (the corresponding number on Monday is 0) if current_date.weekday() == 0: good_dates_count += 1 except ValueError: # If the date is invalid (such as February 30), skip pass # Check if the 31st day is valid in the middle of the month if month in [1, 3, 5, 7, 8, 10, 12]: # Months with 31 days try: current_date = (year, month, 31) if current_date.weekday() == 0: good_dates_count += 1 except ValueError: pass print("The total number of good dates is:", good_dates_count)
Question 5
Two numbers are bitwise XOR, which means that after converting these two numbers into binary, the lowest and lowest bits are the lowest bits as the result, the second and lowest bits are the result, and so on.
For example, the bitwise exclusive OR of 3 and 5 is 6.
Xiaolan has the following 30 integers:
9226, 4690, 4873, 1285, 4624, 1596, 6982, 590, 8806, 121, 8399, 8526, 5426, 64, 9655, 7705, 3929, 3588, 7397, 8020, 1311, 5676, 3469, 2325, 1226, 8203, 9524, 3648, 5278, 8647.
Xiaolan wants to find an integer V so that after V is exclusively or with these 30 numbers, the sum of squares of the 30 numbers obtained is the smallest. What is the minimum sum of squares?
1. Convert each number into a 16-bit binary number (because the binary representation of the maximum number 9655 requires 14 bits, which can be supplemented with 16 bits for unified processing).
2. For each bit, count the number of 30 numbers that is 1.
3. If the number of the bit is more than half (i.e., most numbers are 1 at that bit), then V is selected as 1 at this bit, otherwise it is 0.
4. Calculate the sum of squares of each ai⊕V based on the obtained V.
Code calculation:
nums = [ 9226, 4690, 4873, 1285, 4624, 1596, 6982, 590, 8806, 121, 8399, 8526, 5426, 64, 9655, 7705, 3929, 3588, 7397, 8020, 1311, 5676, 3469, 2325, 1226, 8203, 9524, 3648, 5278, 8647 ] bit_length = 16 count_ones = [0] * bit_length for num in nums: for i in range(bit_length): if num & (1 << (bit_length - 1 - i)): count_ones[i] += 1 V = 0 for i in range(bit_length): if count_ones[i] > len(nums) // 2: V |= (1 << (bit_length - 1 - i)) squared_sum = 0 for num in nums: squared_sum += (num ^ V) ** 2 print(f"The smallest sum of squares is: {squared_sum}")
Question 6
Xiaolan parks in a parking lot.
The charging rules for parking lots are: 2 yuan per 15 minutes, no charge for those less than 15 minutes.
Xiaolan has a total of n minutes of parking. What is the total fee?
[Input Format] Enter a line containing an integer n, indicating the duration of Xiaolan parking.
[Output Format] The output line contains an integer indicating parking fee.
【Sample input】 150
【Sample Output】 20
[Sample input] 2024
【Sample Output】 268
We can use integer division and balance operations to help us determine the number of segments for the parking time.
Can be used(n + 14) // 15
To calculate the number of rounding upwards. ifn
If it is not an integer multiple of 15, just count for one more period.
For example:n = 16
,(16 + 14) // 15
The result is2
, indicating a fee of 2 periods of 15 minutes.
Each 15-minute period is charged 2 yuan, so the final charge is(n + 14) // 15 * 2
。
Code calculation
n=int(input()) if(n>=15): b=n//15 c=b*2 print(c)
Question 7
Xiaolan has an integer n , and each non-zero digit of this integer can be reduced by 1 for each operation.
How many operations will be done, this number will become 0.
For example, the integer 2024 becomes 1013 after one operation, then becomes 2 after another operation (that is, 0002), then becomes 0 after two operations, and a total of 4 transformations become 0 after a total of 4 transformations.
[Input Format] Enter a line containing an integer n.
[Output Format] Output a line, containing an integer, representing the answer.
[Sample input] 2024
【Sample Output】 4
Observe every one: Each non-zero number of an integer is reduced by 1 in each operation until the number becomes 0.
Number of operations: The number of operations can be judged by counting the maximum value of each bit of the integer. Because each digit will be reduced by 1 in each operation, the number of operations that need to be performed in the end is the number of maximum digits for each of the entire number.
Code calculation:
def min_operations_to_zero(n): # Convert to a string and traverse each character digits = str(n) # Find the maximum value of all non-zero numbers max_digit = max(int(d) for d in digits if d != '0') return max_digit n = int(input().strip()) print(min_operations_to_zero(n))
Question 8
Xiaolan has a subtraction formula, which is like a-b, where a and b are non-negative integers (the result is not guaranteed to be non-negative).
Please program this equation and output the calculation result.
[Input Format] The input line contains a subtraction expression, and the formula contains only numeric characters and a minus sign.
[Output Format] The output line contains an integer to represent the operation result.
[Sample input] 2024-1949
【Sample Output】 75
【Sample input】 20-24
【Sample Output】-4
There is a subtraction symbol in the input format-
, it will two numbersa
andb
separate. We can use this symbol to split the input string to obtaina
andb
。
The parseda
andb
After converting it to an integer, directly perform subtraction operations to obtain the result.
Code calculation:
e = input().strip() a, b = ('-') a = int(a) b = int(b) print(a - b)
Question 9
The small blue has an integer sequence of length n a[1], a[2], ..., a[n] .
For an integer k for a given point, Xiaolan wants to find k numbers a[p], a[p+2], a[p+4], ..., a[p+2k-2] with adjacent intervals of 1, so that their sum is maximum. where 1 <= p <= n-2k+2.
Given the sequence sum k , give the largest sum.
[Input Format] The first line of input contains an integer n.
The second line contains n integers, separated by a space between adjacent numbers, representing a[1], a[2], ..., a[n] in turn.
The third line contains an integer k .
[Output Format] Output a line, containing an integer, representing the answer.
[Sample input] 10 2 1 4 7 4 8 3 6 4 7 2 [Sample output] 15 [Sample description] Take p=4, a[4]+a[6]=7+8=15 maximum.
Code calculation:
def max_sum(n, a, k): max_sum_value = float('-inf') for p in range(n - 2 * k + 2): current_sum = 0 for i in range(k): current_sum += a[p + 2 * i] # a[p], a[p+2], ..., a[p+2k-2] max_sum_value = max(max_sum_value, current_sum) return max_sum_value n = int(input()) a = list(map(int, input().split())) k = int(input()) result = max_sum(n, a, k) print(result)
Question 10
Xiaolan has a sequence of integers of length n a[1], a[2], ..., a[n] .
He hopes to find the longest subsequence from it and form a hook shape (√).
That is, find 1 <= p[1] < p[2] < ... < p[k] <= n, satisfying a[p[1]] > a[p[2]] > a[p[3]] > ... > a[p[x]] < a[p[x+1]] < ... < a[p[k]] . where k is the length of the subsequence and x is the smallest position in the hook. The goal is to make k maximum.
Please find the maximum length of the hook.
[Input Format] The first line of input contains an integer n.
The second line contains n integers, separated by a space between adjacent numbers, representing a[1], a[2], ..., a[n] in turn.
[Output Format] Output a line, containing an integer, representing the answer.
[Sample input] 10 2 1 4 7 4 8 3 6 4 7 [Sample output] 5 [Sample description] When p = (4,5,7,9,10), a[4] , a[5] , a[7] , a[9] , a[10] can form a hook with length 5: 7,4,3,6,7.
Code calculation:
def longest_hoop(n, a): dec = [1] * n inc = [1] * n for i in range(1, n): for j in range(i): if a[i] < a[j]: dec[i] = max(dec[i], dec[j] + 1) for i in range(n - 2, -1, -1): for j in range(n - 1, i, -1): if a[i] < a[j]: inc[i] = max(inc[i], inc[j] + 1) # Calculate the maximum length of the hook max_length = 0 for i in range(n): if dec[i] > 1 and inc[i] > 1: max_length = max(max_length, dec[i] + inc[i] - 1) return max_length n = int(input()) a = list(map(int, input().split())) print(longest_hoop(n, a))
Summarize
This is the end of this article about python simulation exercises. For more related python simulation exercises, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!