{ "type": "code_options", "author": "csdn.net", "source": "solution.md", "exercise_id": "8517fe2b3ab94ebfaefe815949da2d20", "keywords": "数列,递归,数学运算", "title": "用递归求第n项的值", "desc": [ { "content": "\n1,2,4,2,3,6,12,6,3,....求第n项值", "language": "markdown" } ], "answer": [ { "content": "", "language": "cpp" } ], "prepared": [ [ { "content": "", "language": "cpp" } ], [ { "content": "", "language": "cpp" } ], [ { "content": "", "language": "cpp" } ] ], "template": { "content": "#include \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}", "language": "cpp" }, "node_id": "dailycode-42e8e695908b45c9ac0372043073d584", "license": "csdn.net", "created_at": 1637894160, "topic_link": "https://bbs.csdn.net/topics/600470270" }