用于查找特殊数中数字总数的 C++ 代码


假设我们给定一个整数 k。如果数字中的所有数字都相同,则称该数字为特殊数字。例如, 1、11、1111 是特殊数字。我们按顺序计算特殊数字 1、11、111、1111、2、22、222、2222、3、33、333、3333,以此类推。我们必须找出 k 以内的特殊数字中包含的数字总数。k 的值不超过 10000。

因此,如果输入类似于 k = 9999,则输出将为 90。

步骤

若要解决此问题,我们将遵循以下步骤 −

s := convert k to string Define an array v of size: := {0, 1, 3, 6, 10} print(((s[0] - '0') - 1) * 10 + v[length of s])

示例

让我们看看以下实现以获得更好的理解 −

Open Compiler
#include <bits/stdc++.h> using namespace std; #define N 100 void solve(int k) { string s = to_string(k); int v[] = {0, 1, 3, 6, 10}; cout<< ((s[0] - '0') - 1) * 10 + v[s.length()] << endl; } int main() { int k = 9999; solve(k); return 0; }

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

输入

9999

输出

90

更新于: 11-Mar-2022

765 次浏览

开始您的 职业

通过完成课程获得认证

开始
广告