[英]How do I extract missing digits from an integer?
I'm trying to get the following output: 我正在尝试获得以下输出:
Enter integer: 87240
Missing digits: 1 3 5 6 9
Here is my code so far: 到目前为止,这是我的代码:
// Extract numbers inside integer n
while (numOfDigits > 0)
{
int digit = n % 10;
int missing = 0;
while ((digit != missing) && (missing < 10))
{
cout << missing << " ";
missing++;
}
numOfDigits--;
n /= 10;
}
which prints out 打印出来
Enter integer: 87240
Missing digits: 0 1 2 3 0 1 0 1 2 3 4 5 6 0 1 2 3 4 5 6 7
Is there any way to go about doing this without using an array? 有没有不用数组就可以做到这一点的方法?
Here is an implementation that uses a single integer ( acc
) to remember which digits had been seen: 这是一个使用单个整数(
acc
)来记住查看过哪些数字的实现:
#include <iostream>
using uuint = unsigned long long int;
uuint p(uuint n) { ++n; return n * n - n + 41; }
void print_missing(uuint n)
{
std::cout << "Number " << n << " is missing the following digits:";
uuint acc = 1;
while (n)
{
uuint q = p(n % 10);
if (acc % q != 0) acc *= q;
n /= 10;
}
for (int i = 0; i != 10; ++i)
{
if (acc % p(i) != 0) std::cout << " " << i;
}
std::cout << "\n";
}
int main()
{
for (uuint n; std::cin >> n; )
print_missing(n);
}
Are you interested in using a string
instead of an array, perchance? 您是否对使用
string
而不是数组感兴趣? Those are more or less the same idea, but given the input string n
we could find any unused digits like this: 这些都或多或少相同的想法,但考虑到输入
string n
我们能找到的任何未使用的数字是这样的:
const auto digits = "0123456789"s;
sort(begin(n), end(n));
set_difference(cbegin(n), cend(n), cbegin(digits), cend(digits), ostream_iterator<char>(cout, " "));
You can use a for
loop, no need for an array: 您可以使用
for
循环,不需要数组:
#include <string>
#include <iostream>
#include <cstdlib>
int main()
{
std::string integer;
std::cout << "Enter integer: ";
std::getline(std::cin, integer);
for (unsigned int i = 0; i < 10; ++i)
{
const char c = '0' + i;
if (integer.find(c) == std::string::npos)
{
cout << i << " ";
}
}
return EXIT_SUCCESS;
}
The above version keeps the number in text form, easier to search for digits. 上面的版本将数字保留为文本形式,以便于搜索数字。
You could use a nested loop to check every digit of the number. 您可以使用嵌套循环来检查数字的每个数字。
Here the version using a single integer to accumulate the results (still, technically array of bits): 这里的版本使用单个整数来累加结果(技术上仍然是位数组):
#include <iostream>
#include <cmath>
void check(int n)
{
unsigned short bits = 0;
while (n) {
bits |= 1 << std::abs(n % 10);
n /= 10;
}
std::cout << "Missing digits:";
for (unsigned i = 0; i != 10; ++i) {
if (((bits >> i) & 1) == 0)
std::cout << " " << i;
}
std::cout << std::endl;
}
int main()
{
int n;
std::cout << "Enter a number: ";
std::cin >> n;
check(n);
return 0;
}
No Arrays in this one. 这个没有数组。
#include <stdio.h>
int NumHasDigit(int num, int digit)
{
while (num)
{
if (num%10 == digit)
{
return 1;
}
num /= 10;
}
return 0;
}
int main(void) {
int x;
printf("Enter int: ");
scanf("%d\n", &x);
printf("Missing Digits:\n");
for(int i=0; i<10; ++i)
{
if (!NumHasDigit(x,i)) printf("%d ", i);
}
return 0;
}
Sample Input: 输入样例:
34567
Sample Output: 样本输出:
Missing Digits
0 1 2 8 9
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.