This is the first day of my participation in Gwen Challenge
Example 1
The title
There are four digits: 1, 2, 3, and 4. How many different, non-repeating three-digit numbers can be formed? What are they?
Analysis of the
Through the three-layer cycle, the number of the first, tenth and hundreds are respectively circulated once, when any two of them are the same, then skip, when the number of each are not at the same time, output
code
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018-10-3 11:10
# @Author : Manu
# @Site :
# @File : group.py
# @Software: PyCharm
count = 0
for i in range(1.5) :for j in range(1.5) :for k in range(1.5) :ifi ! = jandi ! = kandk ! = j:print(i, j, k)
count += 1
print("Total number of components: %d" % count)
Copy the code
The results of
Example 2
The title
Bonuses are paid on the basis of profits. When profit (I) is less than or equal to 100,000 yuan, 10% of bonus can be raised; If the profit is higher than 100,000 yuan, the part below 100,000 yuan will be 10% commission, and the part above 100,000 yuan will be 7.5% commission. Between 200,000 and 400,000 yuan, the part higher than 200,000 yuan, can be 5% commission; Between 400,000 and 600,000, the part higher than 400,000 yuan, can be 3% commission; When arrive between 600 thousand and 1 million, prep above 600 thousand yuan part, can commission 1.5%, prep above 1 million yuan when, the part of more than 1 million yuan presses 1% commission, input that month profit I from the keyboard, beg should extend bonus total amount?
Analysis of the
Enter the endless cycle, and then input enterprise profit, and then calculate the corresponding bonus in each stage of profit and save it in a variable, and finally output the variable value of saving bonus.
Code:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018-10-3 12:10
# @Author : Manu
# @Site :
# @File : bonus.py
# @Software: PyCharm
while True:
profit = int(input("Input the profit"))
print("The profit is % D million." % profit)
if profit <= 10:
bonus = profit * 0.1
elif profit > 10 and profit < 20:
bonus = 10 * 0.1 + (profit - 10) * 0.075;
elif profit >= 20 and profit < 40:
bonus = 10 * 0.1 + (20 - 10) * 0.075 + 0.05 * (profit - 20)
elif profit >= 40 and profit < 60:
bonus = 10 * 0.1 + (20 - 10) * 0.075 + 0.05 * (40 - 20) + 0.03 * (profit - 40)
elif profit >= 60 and profit < 100:
10 * 0.1 + (20 - 10) * 0.075 + 0.05 * (40 - 20) + 0.03 * (60 - 40) + (profit - 60) * 0.015
else:
bonus = 10 * 0.1 + (20 - 10) * 0.075 + 0.05 * (40 - 20) + 0.03 * (60 - 40) + (100 - 60) * 0.015 + (profit - 100) * 0.01
print("The prize is % F million." %bonus)
Copy the code
- Results:
Example 3
The title
An integer, 100 is a perfect square, 168 is a perfect square, what is that number?
Analysis of the
If a positive integer A is the square of an integer B, the positive integer a is called a perfect square; If the number is x: 1, then x + 100 = n ** 2, x + 100 + 168 = m ** 2; 2, calculate the equation: m ** 2-n ** 2 = (m + n)(m-n) = 168; 3, set: m + n = I, m-n = j, I * j =168, at least one of I and j is even; M = (I + j) / 2, n = (i-j) / 2, I and j are either even or odd; 5. It can be inferred from 3 and 4 that both I and j are even integers greater than or equal to 2; 6, if I * j = 1 and j>=2, then 1 < I < 168/2 + 1;
code
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018-10-3 12:10
# @Author : Manu
# @Site :
# @File : compSquNum.py
# @Software: PyCharm
res = 168 // 2
for i in range(1, res + 1) :if 168 % i == 0:
j = 168 / i;
if i > j and (i + j) % 2= =0 and (i - j) % 2= =0 :
m = (i + j) / 2
n = (i - j) / 2
x = m * m - 268
print('The number could be:', x)
Copy the code
The results of
Example 4
The title
Enter a year a month a day and determine the day of the year.
Analysis of the
The number of days in a month in an ordinary year and a leap year is stored in two lists. Then enter the day and year to determine whether it is a leap year or an ordinary year. Then calculate the number of days in the year.
code
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018-10-3 14:10
# @Author : Manu
# @Site :
# @File : day.py
# @Software: PyCharm
list1 = [31.28.31.30.31.30.31.31.30.31.30.31]
list2 = [31.29.31.30.31.30.31.31.30.31.30.31]
while True:
year = int(input('Enter year'))
month = int(input('Input month'))
day = int(input('Enter date'))
sum = 0
if year % 400= =0 or (year % 4= =0 and year % 100! =0) :for i in range(month - 1) :sum += list2[i]
sum += day
else:
for i in range(month - 1) :sum += list1[i]
sum += day
print('This is day % D' %sum)
Copy the code
The results of
Example 5
The title
Enter three integers x,y, and z. Please print the three numbers in ascending order.
Analysis of the
Enter an infinite loop, and then enter it three times. After each input, append it to the list, sort the list, and finally output the sorted list.
code
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018-10-3 15:10
# @Author : Manu
# @Site :
# @File : sortNum.py
# @Software: PyCharm
while True:
print('Input x, y, z:')
arr = []
for i in range(3):
tmp = int(input())
arr.append(tmp)
arr.sort()
print('Three numbers in descending order:', arr)
Copy the code