龙空技术网

畅享聊:Python的练习题(2)

LearningYard学苑 75

前言:

如今兄弟们对“菜鸟教程python100题”大概比较珍视,各位老铁们都想要分析一些“菜鸟教程python100题”的相关文章。那么小编同时在网摘上搜集了一些有关“菜鸟教程python100题””的相关知识,希望小伙伴们能喜欢,你们一起来学习一下吧!

分享兴趣,传播快乐,增长见闻,留下美好!

亲爱的您,这里是LearningYard新学苑。今天小编为你带来

畅享聊:Python的练习题(2),

欢迎您的访问!

Share interests, spread happiness, increase knowledge, and leave beautiful!

Dear you, this is LearningYard Academy.

Today, the editor brings you

Enjoy chatting: Python Exercises (2),

welcome your visit!

题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?

Title: An integer is a perfect square after adding 100, and a perfect square after adding 168. What is the number?

程序分析:

因为题目说明x是整数,但没有说明n和m一定大于零,此处考虑了n和m小于零的情形。

Program analysis:

Because the title says that x is an integer, but it does not say that n and m must be greater than zero, the case where n and m are less than zero is considered here

x+100=n^2;x+100+168=m^2;(注意,没有要求m,n一定大于零)

m^2-n^2=168 即 (m+n)(m-n)=168;

设 m+n=i,m-n=j,则m=(i+j)/2;n=(i-j)/2 有 i*j=168, 因为 m,n 都是整数且 m!=n, 所以 j,i!=0, 因为i*j是正数,故 abs(i)>=1,abs(j)>=1。所以i和j的上限可以取 168。因此m和n的上限也是168,下限可以取-168(注意,m,n的取值范围可以放得很大,不影响结果,因为限制条件可以自己去约束,但所取的范围一定要比真实范围大。

x+100=n^2; x+100+168=m^2; (Note that m and n are not required to be greater than zero)

M ^ 2-n ^ 2=168, that is, (m+n) (m-n)=168;

Let m+n=i, m-n=j, then m=(i+j)/2; N=(i-j)/2 has i * j=168, because m and n are integers and m= n. So j, i= 0, because i * j is a positive number, abs (i)>=1, abs (j)>=1. So the upper limit of i and j can be 168. Therefore, the upper limit of m and n is 168, and the lower limit can be - 168. (Note that the value range of m and n can be greatly expanded, without affecting the results, because the constraints can be constrained by themselves, but the range must be larger than the real range.

代码如下:

The code is as follows:

结果如下:

The results are as follows:

当然方法有很多,这只是其中的一种,还可以看看另一种

Of course, there are many methods. This is just one of them. You can also see another

题目:输入某年某月某日,判断这一天是这一年的第几天?

程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于2时需考虑多加一天:

Question: Enter the date of the year, and determine the day of the year?

Program analysis: Take March 5 as an example, we should first add up the previous two months, and then add 5 days, that is, the day of the year. Under special circumstances, when the leap year and the input month is greater than 2, we need to consider adding an additional day:

结果如下:

The results are as follows:

今天的分享就到这里了。

如果您对今天的文章有独特的想法,

欢迎给我们留言,

让我们相约明天,

祝您今天过得开心快乐!

That's it for today's sharing.

If you have a unique idea about today’s article,

Welcome to leave us a message,

Let us meet tomorrow,

I wish you a happy day today!

本文由learningyard新学苑原创,如有侵权,请联系我们。

部分文字&图片来源:菜鸟教程

翻译:百度翻译

文字&排版|calm

审核|闫庆红

标签: #菜鸟教程python100题