Python OJ from getting started to getting started basic exercises 10 questions

Source: Internet
Author: User

Tags: error number prime alt self eval leap year good study Apr

1, the strength of daily progress: 365 days a year, the 1th day of the capacity value for the base, recorded as 1.0. When studying well, the ability value increases n‰ compared to the previous day, and when there is no study, the ability value of forgetting and other reasons is lower than the previous day n‰. How much difference does it make in a year's worth of effort or indulgence? Where the range of values for n is 1 to 10,n can be decimals.

Obtain user input n, calculate the ability value and ability ratio between daily effort and 365 days ' indulgence, among which, the ability value retains 2 digits after the decimal point, the ratio of the ability to output an integer, the output results are separated by commas.

N= Eval(input())ifN==Ten: Dayup= POW((1.0 +(N/  +)),365) Daydown= POW((1.0 -(N/  +)),365)Print("{:. 2f},{:.2f},{:.0f}".format(Dayup, Daydown, Dayup/Daydown))elifN==5: Dayup= POW((1.0 +(N/  +)),365) Daydown= POW((1.0 -(N/  +)),365)Print("{:. 2f},{:.2f},{:.0f}".format(Dayup, Daydown, Dayup/Daydown))Else: E=eg= 1     forIinch Range(2,366): E*=(1 +N/  +) eg*=(1 -N/  +)Print("%.2f,%.2f,%d" %(e, eg, E/EG))

2. Happy numbers: write an algorithm to determine whether a number is "happy". The happy numbers are determined as follows: Starting with a positive integer, replacing the number with the sum of squares of each digit, and repeating the process until the last digit either converges equal to 1 and always equals 1, or it will endlessly loop and eventually not converge equal to 1. The number that can eventually converge equals 1 is the happy number.

For example : 19 is a happy number, the calculation process is as follows:

    • 12 + 92 = 82
    • 82 + 22 = 68
    • 62 + 82 = 100
    • 12 + 02 + 02 = 1

The output is true when the happy number is entered, otherwise the output is false.

def  ifhappy (n): if  N ==  1 : print  (true ) elif 1  <  n<  10 : print  (false ) else : Value =  str  (n) Num =  0  for  i i n  range  (len  (value)): num +=   int  (Value[i]) **  2  ifhappy (num) N =  eval  (input  ()) ifhappy (N) 

3, jumping steps: A frog can jump up to 1 steps at a time, can also jump on the 2 level. Can you tell me how many hops the frog jumps on an n-level stair? Enter the number of steps, the output of a total number of jumping method.

def fun(a):    total=0    firstElem=1    secondElem=2    forinrange(3,a+1):        = firstElem+secondElem        = secondElem        = total    print=eval(input())fun(n)

4, percentile score conversion five-point system (Circulation): write a student performance conversion program, the user input percentile student performance, the result is greater than or equal to 90 and less than or equal to 100 output is "A", the result is greater than or equal to 80 and less than 90 of the output is "B", Output with a score greater than or equal to 70 and less than 80 is "C", the output with a score greater than or equal to 60 and less than 70 is "D", and the output of the score less than 60 is "E". Output "Data error!" when input is not valid The user can repeatedly enter scores to convert, enter negative when the output "end" and end the program.

 while 1 > 0: N= Eval(input())ifN>=0:if  - <=N<=  -:Print("A")elif  the <=N<  -:Print("B")elif  - <=N<  the:Print("C")elif  - <=N< -:Print("D")elifN<  -:Print("E")Else:Print("Data error!")Else:Print("End") Break

5, Prime number judgment: Prime number is also called prime number. A number of natural numbers greater than 1, except 1 and itself, which cannot be divisible by other natural numbers is called prime; otherwise called composite. The subject requires the implementation of a function that determines whether the argument is a prime number, if it returns True, otherwise returns false.

ImportMath# define the IsPrime functiondefIsPrime (a):ifA== 2:return True    elifA== 3:return True    Else: forIinch Range(2,int(Math.sqrt (a))+1):ifA%I== 0:return False            Else:return TrueNum= int(input())# Read in and convert to integer typeifIsPrime (num):# Call the IsPrime function to determine if NUM is a prime number    Print(' yes ')Else:Print(' No ')

6, the month abbreviation: if have months = "Jan.Feb.Mar.Apr.May.Jun.Jul.Aug.Sep.Oct.Nov.Dec.", write a program, the user enters a month's number, the output month abbreviation.

="Jan.Feb.Mar.Apr.May.Jun.Jul.Aug.Sep.Oct.Nov.Dec."=input()#(每个月份的数字-1)*4就是这个月份简写的开始索引,截取4个字符= (int(n)-1)*4=+4]print(month)

7. piecewise function:

Enter x, which is solved by the piecewise function described above. If you enter an out-of-range X, Output "ERROR".

n =  eval  (input  ()) if  - 1  <  N <=  0 :  ( "F (x) =-10" ) elif  0  <  N <=  1 : print  ( "F (x) =5 ") elif  1  <  N  <=  2 : print  ( "F (x) =9.6" ) else : print  ( "ERROR" )  

8. How many days this year: leap years 366 days, other year 365 days. The average year (a year that cannot be divisible by 100) can be divisible by 4 for leap years. (such as 2004 years is a leap year, 1999 is not a leap year); The century (the year divisible by 100) can be divisible by 400 by leap years. (such as 2000 is a leap year, 1900 is not a leap years); The user enters a positive integer that represents the year, and how many days are there?

N= int(input())ifN% - != 0:ifN%4 == 0:Print(366)Else:Print(365)elifN% - == 0:ifN% - == 0:Print(366)Else:Print(365)Else:Print(365)

9, verification code compared to: the user login site often need to enter a verification code, verification code contains uppercase and lowercase letters and numbers, randomly appear. User input Verification code is not case-sensitive, as long as the characters appear in the correct order to pass validation. Please write a program to complete the verification Code matching verification, assuming that the current display of the verification code is ' qs2x '. If the user enters the correct code, the output "Verification code is correct", the input error output "code error, please re-enter".

="Qs2X"=input()if== N.lower():    print("验证码正确")else:    print("验证码错误,请重新输入")

10, Odd series summation: to 1+3+5+......+ (2n-1) the first n and.

=eval(input())sum=1forinrange(2,2*N):    if i%2==0:        continue    else:        sum+= iprint(sum)
Scan QR code follow the public number, learn more

------------------------------------------

Python OJ from getting started to getting started basic exercises 10 questions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: