首页 › 题库 › C/C++编程考级 › C/C++编程二级 › 简答题
简答题 偏难
#7514

奇葩楼层 几乎每个国家都有自己忌讳的数字,有的大楼在给楼层编号的时候,会刻意跳过含有不吉利数字的编号。比如中国人比较忌讳 4,有的大楼就没有第 4 层、第 14…

奇葩楼层

几乎每个国家都有自己忌讳的数字,有的大楼在给楼层编号的时候,会刻意跳过含有不吉利数字的编号。比如中国人比较忌讳 4,有的大楼就没有第 4 层、第 14 层 等等,这样楼层的编号就会是 1、2、3、5、…… 即编号为 5 的楼层实际是第 4 层。那么在去掉了忌讳数字之后最高楼层编号为 n 的大楼,实际上有多少层呢?

时间限制:1000

内存限制:65536

输入

输入在一行中给出 2 个整数,依次为大楼最高层的编号 n(1 < n ≤ 1000),和某国人民忌讳的数字 d(0 ≤ d ≤ 9)。题目保证 n 中不包含 d。

输出

在一行中输出该大楼的实际楼层数。

样例输入

50 4

样例输出

36
未登录也能判分,登录后才会记录
答案与解析
正确答案:<pre class="brush:cpp;toolbar:false">//参考代码1 C++ #include&lt;bits/stdc++.h&gt; typedef long long ll; const int maxm = 1e5 + 5; const int infmax = INT_MAX; const int infmin = INT_MIN; using namespace std; int main() {     int n, m;     cin &gt;&gt; n &gt;&gt; m;     int sum = 0;     for (int i = 1; i &lt;= n; i++) {         int j = i, f = 1;         while (j) {             int k = j % 10;             j /= 10;             if (k == m) {                 f = 0;                 break;             }         }         if (f) sum++;     }     cout &lt;&lt; sum &lt;&lt; "\n";     return 0; }</pre><pre class="brush:cpp;toolbar:false">//参考代码2 C语言 #include &lt;stdio.h&gt; int main() {     int n, d;     scanf("%d %d", &amp;n, &amp;d);     int actualFloors = 0;     for (int i = 1; i &lt;= n; i++) {         int num = i;         int hasForbiddenDigit = 0;         // 检查数字是否包含禁止的数字         while (num &gt; 0) {             if (num % 10 == d) {                 hasForbiddenDigit = 1;                 break;             }             num /= 10;         }         if (!hasForbiddenDigit) {             actualFloors++;         }     }     printf("%d\n", actualFloors);     return 0; }</pre><p></p>
这道题暂未补解析。