Python 年到世纪功能
问题:给定一年,返回它所在的世纪。第一个世纪从第一年到100年,第二个世纪从101年到200年,等等 我的代码:Python 年到世纪功能,python,python-3.x,algorithm,Python,Python 3.x,Algorithm,问题:给定一年,返回它所在的世纪。第一个世纪从第一年到100年,第二个世纪从101年到200年,等等 我的代码: def centuryFromYear(year): century = year/100 decimal = int(str(century[-2:-1])) integer = int(str(century)[:2]) if decimal > 0: return integer + 1 else:
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))
在某些情况下,这似乎不起作用。比如2001年或2000年
有谁能提供一段更简单的代码吗?您可以在python 3中使用整数除法运算符
/
:
def centuryFromYear(year):
return (year) // 100 + 1 # 1 because 2017 is 21st century, and 1989 = 20th century
print(centuryFromYear(2017)) # --> 21
请注意:这不包括公元前世纪,它使用的截止日期为1999年12月31日
,有时严格定义为2000年12月31日
如果您想在xy00年12月31日设置更严格的截止日期,您可能需要这样做:
def centuryFromYear(year):
return (year - 1) // 100 + 1 # 1 because 2017 is 21st century, and 1989 = 20th century
print(centuryFromYear(2017)) # --> 21
使用整数除法,可在2000年和2017年正常工作:
1 + (year - 1) // 100
把这个数字除以100
century = a // 100
检查年份是否属于同一个世纪
if(a%100 != 0):
century = century + 1
print(century)
这是正确的答案:
def centuryFromYear(year):
if year % 100 == 0:
return year // 100
else:
return year // 100 + 1
另一种适用于0-9999的替代方案更符合您的尝试
year = 2018
cent = int(str(year).zfill(4)[:2])+1
print(cent)
返回:
21
您可以使用“数学”模块中提供的上限函数来获得所需的解决方案
def centuryFromYear(year):
return math.ceil(year/100)
它相当古老,但这是正确的世纪输出。这是一个负层划分
1700//100=17
1701 // 100 = 17
-(-1701//100)=18
它将楼层划分为-1701//100,即-18
适用于所有年份,仅为1行首先从上下文中的
年份减去1开始
def centuryFromYear(year):
return (year - 1) // 100 + 1
用于实施以下示例的工程:
print(centuryFromYear(2000)) # --> 20
print(centuryFromYear(2001)) # --> 21
print(centuryFromYear(2017)) # --> 21
- Python简单一行程序解决方案和JavaScript一行程序解决方案
- 使用javascript中内置的数学函数来回答一行问题
- Math.ceil函数总是将一个数字向上舍入到下一个最大的数字
整数或整数
这对我很有用:
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.")
实际上,我有一个最优雅的代码,我将与您分享C版本
#包括
#包括
int main(){
浮动x;
int-y;
fscanf(标准输入、%f、&x);
x=x/100;
y=ceil(x);
fprintf(标准值,“世纪%d”,y);
返回0;
}
我用PHP解决了这个问题
function centuryFromYear($year) {
if ($year % 100 == 0){
return $year/100;
}
else {
return ceil($year/100);
}
}
注:-
函数将数字向上舍入为最接近的整数
要将数字向下舍入为最接近的整数,请查看floor()函数
要对浮点数进行四舍五入,请查看round()函数
你需要(1年)或者你把100的倍数放在错误的世纪。这是一个好观点,但显然不是OP的首要任务,那就是找到一种进行整数除法的方法。。。我为此留下了一个警告,还有一个关于这个领域的“实践”(最好的和不太好的)讨论的链接。这是相当蹩脚的吹毛求疵,尤其是你似乎没有完整阅读我的答案!“恰当地”是一个用词不当的词,表达一种观点;“严格”当然更合适。人们普遍接受的做法是在1999年12月31日设置几个世纪的截止日期,这确实是不准确的。在我们的历法中没有0年,所以1世纪是1-100,2世纪是101-200,依此类推。是的,我知道有些人两次庆祝了第三个千年:)请解释一下为什么它会起作用,OP做错了什么,这是如何纠正的,以及为什么这个(岁)问题的其他答案不够好。Ps这看起来像是对另一个答案的重新表述。为什么你发布了一个针对python的js解决方案?这对Javascript程序员和算法查看很有帮助。发布了python解决方案,请进行投票,因为我已经发布了python解决方案,并且所有测试用例都通过了
year= int(input())
century = (year - 1) // 100 + 1
print(century)
// Python one-liner solution
def centuryFromYear(year):
return (year + 99) // 100
// Javascript one-liner solution
function centuryFromYear(year) {
return Math.ceil(year/100)
}
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.")
function centuryFromYear($year) {
if ($year % 100 == 0){
return $year/100;
}
else {
return ceil($year/100);
}
}