{ "question_id": 7405895, "question_title": "输入起始和结束的正整数,求其两个正整数之间的偶数和。", "question_content": "<p>输入起始和结束的正整数,求其两个正整数之间的偶数和。</p>", "difficulty": "简单", "answer_id": 53355299, "answer_content": "<pre>\n<code class=\"language-python\"># coding:utf-8\n\nx1 = input("请输入起始数:")\nx2 = input("请输入结束数:")\na = int(x1)\nb = int(x2)\n\n# 若a,b均为偶数时均算在内\nsum1 = 0\nfor i in range(a, b+1):\n if i % 2 == 0:\n sum1 += i\n i += 2\n else:\n i += 1\n\n# 若a,b均为偶数时,不算a和b\nsum2 = 0\nfor i in range(a+1, b):\n if i % 2 == 0:\n sum2 += i\n i += 2\n else:\n i += 1\n\nprint(str(a)+"到"+str(b)+"之间的偶数和(边界是偶数时算着两个边界)是:"+str(sum1))\nprint(str(a)+"到"+str(b)+"之间的偶数和(边界是偶数时不算两个边界)是:"+str(sum2))</code></pre>\n\n<p> </p>", "tag_name": "python", "python": "x1 = input(\"请输入起始数:\")\nx2 = input(\"请输入结束数:\")\na = int(x1)\nb = int(x2)\nsum1 = 0\nfor i in range(a, b+1):\n\tif i % 2 == 0:\n\t\tsum1 += i\n\t\ti += 2\n\telse:\n\t\ti += 1\nsum2 = 0\nfor i in range(a+1, b):\n\tif i % 2 == 0:\n\t\tsum2 += i\n\t\ti += 2\n\telse:\n\t\ti += 1\nprint(str(a)+\"到\"+str(b)+\"之间的偶数和(边界是偶数时算这两个边界)是:\"+str(sum1))\nprint(str(a)+\"到\"+str(b)+\"之间的偶数和(边界是偶数时不算两个边界)是:\"+str(sum2))", "topic_link": "https://bbs.csdn.net/topics/600470507", "status": 1, "keywords": "数学,奇偶数", "license": "csdn.net", "notebook": { "python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/46.ipynb?type=file" }, "notebook_enable": 1 }