46.json 2.1 KB
Newer Older
每日一练社区's avatar
test  
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
{
   "question_id": 7405895,
   "question_title": "输入起始和结束的正整数,求其两个正整数之间的偶数和。",
   "question_content": "<p>输入起始和结束的正整数&#xff0c;求其两个正整数之间的偶数和。</p>",
   "difficulty": "简单",
   "answer_id": 53355299,
   "answer_content": "<pre>\n<code class=\"language-python\"># coding:utf-8\n\nx1 &#61; input(&#34;请输入起始数&#xff1a;&#34;)\nx2 &#61; input(&#34;请输入结束数&#xff1a;&#34;)\na &#61; int(x1)\nb &#61; int(x2)\n\n# 若a,b均为偶数时均算在内\nsum1 &#61; 0\nfor i in range(a, b&#43;1):\n    if i % 2 &#61;&#61; 0:\n        sum1 &#43;&#61; i\n        i &#43;&#61; 2\n    else:\n        i &#43;&#61; 1\n\n# 若a,b均为偶数时&#xff0c;不算a和b\nsum2 &#61; 0\nfor i in range(a&#43;1, b):\n    if i % 2 &#61;&#61; 0:\n        sum2 &#43;&#61; i\n        i &#43;&#61; 2\n    else:\n        i &#43;&#61; 1\n\nprint(str(a)&#43;&#34;到&#34;&#43;str(b)&#43;&#34;之间的偶数和(边界是偶数时算着两个边界)是&#xff1a;&#34;&#43;str(sum1))\nprint(str(a)&#43;&#34;到&#34;&#43;str(b)&#43;&#34;之间的偶数和(边界是偶数时不算两个边界)是&#xff1a;&#34;&#43;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
}