{"id":34134,"date":"2024-11-01T09:24:35","date_gmt":"2024-11-01T09:24:35","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34134"},"modified":"2024-11-01T10:58:28","modified_gmt":"2024-11-01T10:58:28","slug":"c-coding-test-course-counting-stairways","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34134\/","title":{"rendered":"C++ Coding Test Course, Counting Stairways"},"content":{"rendered":"<p><body><\/p>\n<article>\n<header>\n<p>Author: [Your Name]<\/p>\n<p>Date: [Date]<\/p>\n<\/header>\n<section>\n<h2>1. Problem Description<\/h2>\n<p>The number of stairs refers to the combinations of ways to climb stairs by choosing either to go up or down. The given problem is as follows.<\/p>\n<blockquote>\n<p>Given an integer N, write a program to calculate the number of ways to climb N stairs.<\/p>\n<p>However, you can climb either one or two stairs at a time, and the last stair number must end with a digit from 1 to 9.<\/p>\n<\/blockquote>\n<p>For example, the ways to climb 4 stairs are as follows:<\/p>\n<ul>\n<li>1111 (1+1+1+1)<\/li>\n<li>112 (1+1+2)<\/li>\n<li>121 (1+2+1)<\/li>\n<li>211 (2+1+1)<\/li>\n<li>22 (2+2)<\/li>\n<\/ul>\n<p>Considering the above cases, you need to write code to correctly calculate the number of stairs.<\/p>\n<\/section>\n<section>\n<h2>2. Problem Analysis<\/h2>\n<p>Let us analyze some important points to solve the problem.<\/p>\n<ul>\n<li><strong>State Definition<\/strong>: The Nth stair is composed differently depending on the state of the previous stairs. For example, look at the counts for N=1 and N=2.<\/li>\n<li><strong>Recurrence Relation<\/strong>: The way to climb N stairs is the sum of the number of ways to climb 1 stair from N-1 stairs and the number of ways to climb 2 stairs from N-2 stairs. The last stair number must be one of the digits from 1 to 9.<\/li>\n<li><strong>Result<\/strong>: The number of ways to climb N stairs is the sum of all the counts of each case.<\/li>\n<\/ul>\n<\/section>\n<section>\n<h2>3. Deriving the Recurrence Relation<\/h2>\n<p>The general recurrence relation can be expressed as follows:<\/p>\n<pre><code>\ndp[i][j] = dp[i-1][j-1] + dp[i-2][j]\n            <\/code><\/pre>\n<p>Here, <code>dp[i][j]<\/code> represents the number of ways ending with j at the i-th stair. The initial conditions can be set as dp[1][1] = 1, dp[1][2] = 1 &#8230; dp[1][9] = 1. Based on this, we can write the following code.<\/p>\n<\/section>\n<section>\n<h2>4. Code Design<\/h2>\n<p>Now let&#8217;s write code in C++. This code takes the given N as an input and prints the corresponding number of ways to climb stairs.<\/p>\n<pre><code>\n#include &lt;iostream&gt;\n\nusing namespace std;\n\nconst int MOD = 1000000000; \/\/ MOD setting for large number processing\nint dp[100][10]; \/\/ dp table\n\nvoid init() {\n    for (int i = 1; i &lt;= 9; i++) {\n        dp[1][i] = 1; \/\/ Number of ways to climb one stair\n    }\n  \n    for (int i = 2; i &lt;= 100; i++) {\n        for (int j = 0; j &lt;= 9; j++) {\n            \/\/ Case ending with 0 (j == 0)\n            if (j == 0) {\n                dp[i][0] = dp[i - 1][1] % MOD; \n            }\n            \/\/ Case ending with 9 (j == 9)\n            else if (j == 9) {\n                dp[i][9] = dp[i - 1][8] % MOD; \n            }\n            \/\/ Other cases\n            else {\n                dp[i][j] = (dp[i - 1][j - 1] + dp[i - 1][j + 1]) % MOD;\n            }\n        }\n    }\n}\n\nint main() {\n    int N;\n    cin &gt;&gt; N; \/\/ Input number of stairs\n    init(); \/\/ Initialize dp table\n\n    long long total = 0;\n    for (int i = 0; i &lt;= 9; i++) {\n        total = (total + dp[N][i]) % MOD; \/\/ Calculate total number of cases\n    }\n\n    cout &lt;&lt; total &lt;&lt; endl; \/\/ Output result\n    return 0;\n}\n            <\/code><\/pre>\n<\/section>\n<section>\n<h2>5. Code Explanation<\/h2>\n<p>The above code is written in a dynamic programming style to calculate the number of stairs.<\/p>\n<ul>\n<li><strong>Input Handling:<\/strong> It takes N as input from the user to calculate the number of stairs.<\/li>\n<li><strong>Table Initialization:<\/strong> The first row of the dp table is initialized to set each ending digit to 1.<\/li>\n<li><strong>Applying the Recurrence Relation:<\/strong> It calculates the possible counts for each number of stairs and saves the values divided by MOD.<\/li>\n<li><strong>Result Calculation:<\/strong> It sums all counts from 1 to 9 and outputs the final result.<\/li>\n<\/ul>\n<\/section>\n<section>\n<h2>6. Complexity Analysis<\/h2>\n<p>The time complexity of this algorithm is O(N), which takes linear time for the given N. The space complexity also has a size of O(N). This ensures efficiency in both memory and time.<\/p>\n<\/section>\n<section>\n<h2>7. Conclusion<\/h2>\n<p>The stair number problem is suitable for learning the basic concepts of dynamic programming. Performing this problem greatly aids in understanding recursive thinking and the concept of memoization. By solving this problem through the C++ Programming Language, you can further strengthen your algorithm problem-solving skills.<\/p>\n<p>Hope this course helps you in preparing for the C++ coding test! Learn it, and try solving more problems!<\/p>\n<\/section>\n<footer>\n<p>Reference Link:<br \/>\n<a href=\"https:\/\/www.acmicpc.net\/problem\/1562\">Stair Number &#8211; Baekjoon<\/a><\/p>\n<\/footer>\n<\/article>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Author: [Your Name] Date: [Date] 1. Problem Description The number of stairs refers to the combinations of ways to climb stairs by choosing either to go up or down. The given problem is as follows. Given an integer N, write a program to calculate the number of ways to climb N stairs. However, you can &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34134\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C++ Coding Test Course, Counting Stairways&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[111],"tags":[],"class_list":["post-34134","post","type-post","status-publish","format-standard","hentry","category-c-coding-test-tutorials-2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C++ Coding Test Course, Counting Stairways - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/34134\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ Coding Test Course, Counting Stairways - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Author: [Your Name] Date: [Date] 1. Problem Description The number of stairs refers to the combinations of ways to climb stairs by choosing either to go up or down. The given problem is as follows. Given an integer N, write a program to calculate the number of ways to climb N stairs. However, you can &hellip; \ub354 \ubcf4\uae30 &quot;C++ Coding Test Course, Counting Stairways&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34134\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:24:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:58:28+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/34134\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34134\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C++ Coding Test Course, Counting Stairways\",\"datePublished\":\"2024-11-01T09:24:35+00:00\",\"dateModified\":\"2024-11-01T10:58:28+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34134\/\"},\"wordCount\":475,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C++ Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34134\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34134\/\",\"name\":\"C++ Coding Test Course, Counting Stairways - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:24:35+00:00\",\"dateModified\":\"2024-11-01T10:58:28+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34134\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34134\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34134\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ Coding Test Course, Counting Stairways\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C++ Coding Test Course, Counting Stairways - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/34134\/","og_locale":"ko_KR","og_type":"article","og_title":"C++ Coding Test Course, Counting Stairways - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Author: [Your Name] Date: [Date] 1. Problem Description The number of stairs refers to the combinations of ways to climb stairs by choosing either to go up or down. The given problem is as follows. Given an integer N, write a program to calculate the number of ways to climb N stairs. However, you can &hellip; \ub354 \ubcf4\uae30 \"C++ Coding Test Course, Counting Stairways\"","og_url":"https:\/\/atmokpo.com\/w\/34134\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:24:35+00:00","article_modified_time":"2024-11-01T10:58:28+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/34134\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34134\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C++ Coding Test Course, Counting Stairways","datePublished":"2024-11-01T09:24:35+00:00","dateModified":"2024-11-01T10:58:28+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34134\/"},"wordCount":475,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C++ Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34134\/","url":"https:\/\/atmokpo.com\/w\/34134\/","name":"C++ Coding Test Course, Counting Stairways - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:24:35+00:00","dateModified":"2024-11-01T10:58:28+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34134\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34134\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34134\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C++ Coding Test Course, Counting Stairways"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34134","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=34134"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34134\/revisions"}],"predecessor-version":[{"id":34135,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34134\/revisions\/34135"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}