查找数字中特定的重复数字
Finding specific repeated digits in a number
我正在尝试编写一个程序,该程序将计算数字中5
s的数量并显示它。所以如果用户输入:
52315
程序应该输出:
Yes, there are '5's and they're 2
这是我的代码,但是有一些问题。
{
int n,m;
int count = 0;
cout << "Enter an: ";
cin >> n;
int *arr;
arr = new int[count];
// Getting digits from number
while (n > 0)
{
count++;
m = n%10;
n = n/10;
//Trying to put every digit in an array
for (int i = 0; i<count; i++)
{
cin>>arr[i];
arr[i] = m;
cout << arr[i];
}
int even = 5;
//Trying to see if there's a digit 5 in inputed number and how many if so.
for (int j = 0; j<count; j++)
{
if (arr[j]==even)
{
cout << "Yes, there's a digit '5' " << endl;
s++;
}
}
cout << "Count of '5's : " << even;
delete[] arr;
}
return 0;
}
This
for (int i = 0; i<count; i++)
{
cin >> arr[i];
}
您正在尝试用另一个用户输入而不是现有的输入填充数组。
也可以不使用数组:
int count = 0;
int n;
cin >> n;
do {
if (n%10 ==5) count++;
n /= 10;
} while (n);
cout << "Count of '5's : " << count;
对于每个数字都应该这样做。如果你只想知道一个特殊的数字,比如5,只需删除for循环并打印count[theNumberYouWantToKnow]。
#define BASE 10
void output() {
int givenNumber;
cout << "Please enter a number: ";
cin >> givenNumber;
int count[BASE] = { 0 };
while(givenNumber > 0) {
int digit = givenNumber % BASE;
givenNumber /= BASE;
count[digit]++;
}
for(int i = 0; i < BASE; i++) {
cout << "Found the number " << i << " " << count[i] << " times." << endl;
}
}
相关文章:
- 查找最接近的大于当前数字的数字的索引
- 在 c++ 中查找字符串中没有循环的数字总和
- 程序以使用 C++ 中的 while 循环查找一组数字的最小值
- 在C++的一系列数字中查找重复模式
- 为什么使用数组元素查找最大数字的程序不起作用?
- C++ 函数,用于查找传入的 N 个数字的平均值、总和、最小值和最大值
- 查找数字是否为 2 的幂的时间复杂度
- 如何在不使用 C++ 中的数组或函数的情况下查找 N 位数字的所有排列
- 使用 XOR 逻辑查找缺失的数字
- 查找中间两个数字的正则表达式的匹配项
- 查找数组中的最小数字
- 查找数组中重复(重复)数字的索引
- 程序 查找数字的重复
- 如何查找数字大于 5 的数字总和
- 查找数组中给定数字的出现次数
- 在 C++ 中查找 2 个数字的 GCD
- 代码以查找数组中的最大数字,但它仅将第一个数字显示为最大数字. 有人请告诉我为什么
- 如何制作一个查找数字平方值的 c++ 程序
- 查找整数数组中最接近的数字
- C ++递归与大数字查找mod