{
"question_id": 1053941, "question_title": "猴子吃桃", "question_content": "问题:猴子第一天摘了若干个桃子,吃了一半,不过瘾,又多吃了1个。第二天早上将剩余的桃子又吃掉一半,并且又多吃了1个。此后每天都是吃掉前一天剩下的一半零一个。到第n天再想吃时,发现只剩下1个桃子,问第一天它摘了多少桃子?为了加强交互性,由用户输入不同的天数n进行递推,即假设第n天的桃子数为1。同时还要增加对用户输入数据的合法性验证(如:不允许输入的天数是0和负数)\n", "difficulty": "中等", "answer_id": 1251130, "answer_content": "\n```\n#include <stdio.h>\n\nint main()\n{\n\tint ret,day,i=1,sum=1;\n\twhile (1)\n\t{\n\t\tprintf(\"Input days:\\n\");\n\t\tret=scanf(\"%d\",&day);\n\t\tif ((ret!=1)||(day<=0))\n\t\t{\n\t\t\tfflush(stdin);\n\t\t\tcontinue;\n\t\t}\n\t\tbreak;\n\t}\n\tdo\n\t{\n\t\tsum=(sum+1)*2;\n\t\ti++;\n\t}while(i<day);\n\tprintf(\"sum=%d\\n\",sum);\n\treturn 0;\n}\n```\n\n# 问题解决的话,请点下`采纳`", "tag_name": "c语言", "cpp": "#include <stdio.h>\nint main()\n{\n\tint ret,day,i=1,sum=1;\n\twhile (1)\n\t{\n\t\tprintf(\"Input days:\\n\");\n\t\tret=scanf(\"%d\",&day);\n\t\tif ((ret!=1)||(day<=0))\n\t\t{\n\t\t\tfflush(stdin);\n\t\t\tcontinue;\n\t\t}\n\t\tbreak;\n\t}\n\tdo\n\t{\n\t\tsum=(sum+1)*2;\n\t\ti++;\n\t}while(i<day);\n\tprintf(\"sum=%d\\n\",sum);\n\treturn 0;\n}", "topic_link": "https://bbs.csdn.net/topics/600469947", "status": 1, "keywords": "算法高阶,NP完全性,算法问题选编,多项式时间的验证", "license": "csdn.net", "notebook": { "cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/62.ipynb?type=file" }, "notebook_enable": 1, "author": "qq_32543603"
}