{"id":172,"date":"2024-04-26T16:11:31","date_gmt":"2024-04-26T08:11:31","guid":{"rendered":"https:\/\/seanxd.com\/?p=172"},"modified":"2024-04-26T16:11:33","modified_gmt":"2024-04-26T08:11:33","slug":"zerojudge-e357","status":"publish","type":"post","link":"https:\/\/seanxd.com\/en\/zerojudge-e357\/","title":{"rendered":"ZeroJudge E357: Recursion Practice"},"content":{"rendered":"<p class=\"\">Define a function F(x).<\/p>\n\n\n\n<p class=\"\">If x=1, then F(x)=1.<\/p>\n\n\n\n<p class=\"\">If x is even, then F(x)=F(x\/2).<\/p>\n\n\n\n<p class=\"\">For all other cases, F(x)=F(x\u22121)+F(x+1).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Sample Inputs\/Outputs<\/h2>\n\n\n\n<figure class=\"wp-block-table nfd-wb-animate nfd-wb-fade-in-bottom-short\"><table class=\"has-fixed-layout\"><thead><tr><th>Sample Input(s)<\/th><th>Sample Output(s)<\/th><\/tr><\/thead><tbody><tr><td>The input consists of only one line containing a positive integer x (1 \u2264 x \u2264 2,000,000).<\/td><td>The output consists of only one line containing a positive integer F(x).<\/td><\/tr><tr><td>6<\/td><td>2<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Thought Process<\/h2>\n\n\n\n<p class=\"translation-block\">You can write a recursive function for this problem. The termination condition is when N=1, return N, then write the conditions for even and odd numbers. Finally, call the recursive function with N and print the result using cout.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Sample Code\uff0d<a href=\"https:\/\/zerojudge.tw\/ShowProblem?problemid=e357\" target=\"_blank\" rel=\"noreferrer noopener\">ZeroJudge E357: Recursion Practice<\/a><\/h3>\n\n\n\n<div class=\"hcb_wrap nfd-wb-animate nfd-wb-reveal-right nfd-delay-50\"><pre class=\"prism line-numbers lang-cpp\" data-lang=\"C++\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nint calc (int N)\n{\n    if (N == 1) return 1;\n    if (N % 2 == 0) return calc(N\/2);\n    else return calc(N-1)+calc(N+1);\n}\n\nint main() {\n    cin.sync_with_stdio(0);\n    cin.tie(0);\n    int N;\n    cin &gt;&gt; N;\n    cout &lt;&lt; calc(N) &lt;&lt; endl;\n}\n\n\/\/ZeroJudge E357\n\/\/Dr. SeanXD<\/code><\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>\u5b9a\u7fa9\u4e00\u500b\u51fd\u6578 F(x) \u82e5 x = 1\uff0c\u5247 F(x) = 1 \u82e5 x \u70ba\u5076\u6578\uff0c\u5247 F(x) = F(x\/2)  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","footnotes":""},"categories":[6],"tags":[8,13,28],"class_list":["post-172","post","type-post","status-publish","format-standard","hentry","category-6","tag-8","tag-13","tag-28"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/172","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/comments?post=172"}],"version-history":[{"count":1,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/172\/revisions"}],"predecessor-version":[{"id":173,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/172\/revisions\/173"}],"wp:attachment":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/media?parent=172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/categories?post=172"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/tags?post=172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}