{"id":110,"date":"2024-04-26T12:16:48","date_gmt":"2024-04-26T04:16:48","guid":{"rendered":"https:\/\/seanxd.com\/?p=110"},"modified":"2024-04-26T12:16:50","modified_gmt":"2024-04-26T04:16:50","slug":"zerojudge-a414","status":"publish","type":"post","link":"https:\/\/seanxd.com\/en\/zerojudge-a414\/","title":{"rendered":"ZeroJudge A414: Bit Calculation"},"content":{"rendered":"<p class=\"\">How many times does a number need to carry when incremented in a computer?<\/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><strong>EOF<\/strong> Input: Each line contains a decimal positive integer N (1 &lt;= N &lt;= 2147483647). The last line of input contains a 0, indicating the end of input. Do not process the input for 0.<\/td><td>For each positive integer N, output the number of carries required when calculating N+1 in binary.<\/td><\/tr><tr><td>1<br>4<br>7<br>17<br>0<\/td><td>1<br>0<br>3<br>1<\/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\">Use a while loop to convert N into binary and perform the calculation directly. Because using a while loop to convert to binary starts from the rightmost bit, it perfectly fits the requirement of the problem. So, check if the current digit obtained is 1, if yes, increment the answer by 1; if it is 0, break the while loop.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Example:<\/h5>\n\n\n\n<ul class=\"wp-block-list\">\n<li class=\"\">The binary representation of \"4\" is \"100.\" When adding 1, it becomes \"101,\" which does not require a carry.<\/li>\n\n\n\n<li class=\"\">The binary representation of \"7\" is \"111.\" When adding 1, it becomes \"1000,\" requiring 3 carries because there are three consecutive 1s from right to left.<\/li>\n<\/ul>\n\n\n\n<p class=\"\">Finally, output the variable that stores the answer. Please note that since time is limited for this problem, it's recommended to avoid using #include  and instead use #include  along with scanf and printf for input and output.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Sample Code\uff0d<a href=\"https:\/\/zerojudge.tw\/ShowProblem?problemid=a414\" target=\"_blank\" rel=\"noreferrer noopener\">ZeroJudge A414: Bit Calculation<\/a><\/h3>\n\n\n\n<div class=\"hcb_wrap nfd-wb-animate nfd-wb-reveal-right\"><pre class=\"prism line-numbers lang-cpp\" data-lang=\"C++\"><code>#include &lt;stdio.h&gt;\nusing namespace std;\n\nint main() {\n    int N;\n    while (scanf(&quot;%d&quot;, &N) && N != 0)\n    {\n        int count = 0;\n        while(N % 2 == 1)\n        {\n            ++count;\n            N \/= 2;\n        }\n        printf(&quot;%d\\n&quot;, count);\n    }\n}\n\n\/\/A414\n\/\/Dr. SeanXD<\/code><\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>\u4e00\u500b\u6578\u5728\u96fb\u8166\u88e1\u905e\u589e\u6642\u9700\u8981\u9032\u4f4d\u5e7e\u6b21\uff1f \u7bc4\u4f8b\u6e2c\u8cc7 \u7bc4\u4f8b\u8f38\u5165 \u7bc4\u4f8b\u8f38\u51fa EOF \u8f38\u5165\uff0c\u6bcf\u4e00\u884c\u6709\u4e00\u500b\u5341\u9032\u5236\u6b63\u6574\u6578 N  [&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":[16,9,24],"class_list":["post-110","post","type-post","status-publish","format-standard","hentry","category-6","tag-tle","tag-9","tag-24"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/110","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=110"}],"version-history":[{"count":2,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/110\/revisions"}],"predecessor-version":[{"id":112,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/110\/revisions\/112"}],"wp:attachment":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/media?parent=110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/categories?post=110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/tags?post=110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}