{
  "question_id": 7442662,
  "question_title": "用递归求第n项的值",
  "question_content": "1，2，4，2，3，6，12，6，3，....求第n项值",
  "difficulty": "简单",
  "answer_id": 53419757,
  "answer_content": "<pre>\n<code class=\"language-cpp\">#include &lt;stdio.h&gt;\n\nint fun(int n,int *x,int *y)\n{\n\tint sum &#61; 0,i;\n\tint size &#61; 0;\n\tint dd &#61; 1;\n\tfor (i &#61; 1; i &lt;&#61; (*x);i&#43;&#43;)\n\t{\n\t\tsum &#43;&#61; (2*i-1);\n\t}\n\tif (sum &#61;&#61; n)\n\t{\n\t\t*y &#61; 2*(*x) -1;\n\t\treturn (*x);\n\t}else if (sum &gt; n)\n\t{\n\t\t(*y) &#61; n - (sum - (2 * (*x) -1));\n\t\tsize &#61; 2* (*x) -1;\n\t\tdd &#61; (*x);  //第x行的第一个数\n\t\tfor (i &#61; 2; i &lt;&#61; (*y);i&#43;&#43;)\n\t\t{\n\t\t\tif(i &lt;&#61; (*x))\n\t\t\t\tdd *&#61; 2;\n\t\t\telse\n\t\t\t\tdd /&#61; 2;\n\t\t}\n\t\treturn dd;\n\t}else\n\t{\n\t\t(*x)&#43;&#43;;\n\t\treturn fun(n,x,y);\n\t}\n}\n\nint main()\n{\n\tint n;        //n项(从第1项开始&#xff09;\n\tint row &#61; 1,col &#61; 0;\n\tint val;\n\n\trow &#61; 1;\n\tcol &#61; 0;\n\tprintf(&#34;请输入n&#xff1a;&#34;);\n\tscanf(&#34;%d&#34;,&amp;n);\n\tval &#61; fun(n,&amp;row,&amp;col);\n\tprintf(&#34;第%d项是&#xff1a;%d\\n&#34;,n,val);\n\treturn 0;\n}\n</code></pre>\n\n<p> </p>\n",
  "tag_name": "c语言",
  "cpp": "#include <stdio.h>\nint fun(int n,int *x,int *y)\n{\n\tint sum = 0,i;\n\tint size = 0;\n\tint dd = 1;\n\tfor (i = 1; i <= (*x);i++)\n\t{\n\t\tsum += (2*i-1);\n\t}\n\tif (sum == n)\n\t{\n\t\t*y = 2*(*x) -1;\n\t\treturn (*x);\n\t}else if (sum > n)\n\t{\n\t\t(*y) = n - (sum - (2 * (*x) -1));\n\t\tsize = 2* (*x) -1;\n\t\tdd = (*x);  \n\t\tfor (i = 2; i <= (*y);i++)\n\t\t{\n\t\t\tif(i <= (*x))\n\t\t\t\tdd *= 2;\n\t\t\telse\n\t\t\t\tdd /= 2;\n\t\t}\n\t\treturn dd;\n\t}else\n\t{\n\t\t(*x)++;\n\t\treturn fun(n,x,y);\n\t}\n}\nint main()\n{\n\tint n;\t\t\n\tint row = 1,col = 0;\n\tint val;\n\trow = 1;\n\tcol = 0;\n\tprintf(\"请输入n：\");\n\tscanf(\"%d\",&n);\n\tval = fun(n,&row,&col);\n\tprintf(\"第%d项是：%d\\n\",n,val);\n\treturn 0;\n}",
  "topic_link": "https://bbs.csdn.net/topics/600470270",
  "status": 1,
  "keywords": "数列,递归,数学运算",
  "license": "csdn.net",
  "notebook": {
    "cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/89.ipynb?type=file"
  },
  "notebook_enable": 1,
  "author": "qq_45814103"
}