问题:给定一年,回归它所处的世纪。第一个世纪从第1年开始,包括第100年,第二年 - 从101年到包括200年,等等。 / p>
我的代码:
def centuryFromYear(year):
century = year/100
decimal = int(str(century[-2:-1]))
integer = int(str(century)[:2])
if decimal > 0:
return integer + 1
else:
return integer
print(centuryFromYear(2017))
在某些情况下,这似乎不起作用。比如year = 2001或year = 2000。
是否有人能够提供更简单的代码?
答案 0 :(得分:5)
你可以在python 3中使用整数除法运算符//
:
def centuryFromYear(year):
return (year) // 100 + 1 # 1 because 2017 is 21st century, and 1989 = 20th century
print(centuryFromYear(2017)) # --> 21
请注意:这不考虑公元前一个世纪,它使用Dec 31st xy99
处的截止日期,有时严格定义为Dec 31st xy00
more info here
如果你想在Dec 31st xy00
上设置一个更严格的截止值,你可能会想这样做:
def centuryFromYear(year):
return (year - 1) // 100 + 1 # 1 because 2017 is 21st century, and 1989 = 20th century
print(centuryFromYear(2017)) # --> 21
答案 1 :(得分:4)
Python简单单线解决方案和JavaScript单线解决方案
使用javascript中的内置数学函数来获得单行答案
Math.ceil函数始终将数字四舍五入到下一个大数 整数或整数。
// Python one-liner solution
def centuryFromYear(year):
return (year + 99) // 100
// Javascript one-liner solution
function centuryFromYear(year) {
return Math.ceil(year/100)
}
答案 2 :(得分:1)
使用整数除法,适用于2000年和2017年:
1 + (year - 1) // 100
答案 3 :(得分:1)
您可以使用“数学”模块中可用的上限功能来获得所需的解决方案。
def centuryFromYear(year):
return math.ceil(year/100)
答案 4 :(得分:0)
clean
将数字除以100
repoB
检查年份是否属于同一世纪
a = int(input('Find the Century = '))
答案 5 :(得分:0)
另一种适用于0-9999的替代方案,更符合您的尝试。
year = 2018
cent = int(str(year).zfill(4)[:2])+1
print(cent)
返回:
21
答案 6 :(得分:0)
def centuryFromYear(year):
return -(-year // 100)
它很老了,但这是正确的世纪输出。它是负的底数分区
1700 // 100 = 17 1701 // 100 = 17 -(-1701 // 100)= 18
它在-1701 // 100上将楼层划分为-18
所有年份都有效,并且只有1行
答案 7 :(得分:0)
首先从上下文中的year
减去1开始
def centuryFromYear(year):
return (year - 1) // 100 + 1
实现以下示例的工作:
print(centuryFromYear(2000)) # --> 20
print(centuryFromYear(2001)) # --> 21
print(centuryFromYear(2017)) # --> 21
答案 8 :(得分:0)
这对我有用:
def whatCenturyIsX(x):
#turn our input into a string for modification
x = str(x)
#separate the characters of x into a list for further use
xlist = list(x)
#set a boolean to contatin negativity or positivity of the number
#if the "minus" sign is in x, set the boolean to true and remove the "minus" for easier handling of the variable
#(the minus doesn't tell us anything anymore because we already set the boolean)
negative = False
if "-" in xlist:
negative = True
xlist.remove("-")
for i in xlist:
x += i
#to define what century year x is in, we are going to take the approach of adding 1 to the first n characters, when N is the number of digits - 2. This is proved. So:
#also, we need the string to be at least 4 characters, so we add 0's if there are less
if len(xlist) >= 4:
pass
else:
if len(xlist) == 3:
xlist.insert(0, 0)
x = ""
for i in xlist:
x += str(i)
elif len(xlist) == 2:
xlist.insert(0, 0)
xlist.insert(1, 0)
x = ""
for i in xlist:
x += str(i)
elif len(xlist) == 1:
xlist.insert(0, 0)
xlist.insert(1, 0)
xlist.insert(2, 0)
x = ""
for i in xlist:
x += str(i)
n = len(xlist) - 2
#j is the number formed by the first n characters.
j = ""
for k in range(0, n):
#add the first n characters to j
j += str(xlist[k])
#finally form the century by adding 1 to j and calling it c.
c = int(j) + 1
#for the final return statement, we add a "-" and "B.C." if negative is true, and "A.C." if negative is false.
if negative:
xlist.insert(0, "-")
x = ""
for i in xlist:
x += str(i)
return(str(x) + " is en the century " + str(c) + " B.C.")
else:
return(str(x) + " is en the century " + str(c) + " A.C.")
答案 9 :(得分:0)
我用 PHP 解决了这个问题。
function centuryFromYear($year) {
if ($year % 100 == 0){
return $year/100;
}
else {
return ceil($year/100);
}
}
注意:-
答案 10 :(得分:-1)
year= int(input())
century = (year - 1) // 100 + 1
print(century)
答案 11 :(得分:-1)
我实际上是最优雅的代码之一,我将与您共享C版本。
#include<math.h>
#include<stdio.h>
int main() {
float x;
int y;
fscanf(stdin, "%f", &x);
x = x / 100;
y = ceil(x);
fprintf(stdout, "Century %d ", y);
return 0;
}
答案 12 :(得分:-3)
这是正确答案:
def centuryFromYear(year):
if year % 100 == 0:
return year // 100
else:
return year // 100 + 1