C++ 计数整数间隔(或整数数组)中每个数字的出现次数
使用循环,如何查找整数间隔[n,m]中每个数字的出现次数 例如:C++ 计数整数间隔(或整数数组)中每个数字的出现次数,c++,C++,使用循环,如何查找整数间隔[n,m]中每个数字的出现次数 例如: 输入n,m=[19,23]=19,20,21,22,23 输出应为: 0次发生:1次 1次发生:2次 2次:5次 3次:1次等 #include <iostream> using namespace std; int main() { int i, j, z, count, n, m; cin >>n >>m; for(int i=0; i<10; i++) // LOOP F
- 输入n,m=[19,23]=19,20,21,22,23
- 输出应为:
#include <iostream>
using namespace std;
int main()
{
int i, j, z, count, n, m;
cin >>n >>m;
for(int i=0; i<10; i++) // LOOP FOR DIGITS
{
cout << i <<"occurences: ";
count=0;
for(int j=n; j<m; j++) // LOOP INTEGER INTERVAL
{
while (z!=0)
{
z = j % 10; // LAST DIGIT OF FIRST NUMBER IN INTERVAL
if (z == i) count++;
z /= 10;
}
}
cout << count <<" times"<< endl;
}
}
#包括
使用名称空间std;
int main()
{
int i,j,z,count,n,m;
cin>>n>>m;
对于(int i=0;i我看到的一些问题:
z=j%10;
你需要在while循环外初始化z
到j,你还想得到mod,但不想将其设置为z
。尝试将结果放入一个临时变量,而不是z
for循环不包括最后一个数字。for(int j=n;jn>>m;
对于(int i=0;i基本上,模运算用于检索任何数字的最低有效位。将该数字除以基数将删除最低有效位,使下一个数字成为新的最低有效位
int main(int argc, char *argv[])
{
int radix = 10;
int x, y;
printf("Lower bound: ");
scanf("%d, &x);
printf("Upper bound: ");
scanf("%d, &y);
int digits[radix];
count_digit_occurence(x, y, radix, digits);
int i;
for (i = 0; i < radix; ++i)
{
int occ = digits[i];
printf("%d occurred %d times\n", i, occ);
}
}
void count_digit_occurence(int x, int y, int radix, int digits[radix])
{
int i, n;
for (i = x; i <= y; ++i)
{
n = i;
while (n > 0)
{
++(digits[n % radix]);
n /= radix;
}
}
}
intmain(intargc,char*argv[])
{
int基数=10;
int x,y;
printf(“下限:”);
scanf(“%d,&x”);
printf(“上限:”);
scanf(“%d,&y”);
整数数字[基数];
计数数字出现(x、y、基数、数字);
int i;
对于(i=0;i<基数;++i)
{
int occ=数字[i];
printf(“%d发生了%d次\n”,i,occ);
}
}
无效计数\u数字\u出现(整数x、整数y、整数基数、整数位数[基数])
{
inti,n;
对于(i=x;i 0)
{
++(数字[n%基数]);
n/=基数;
}
}
}
您不需要在该范围内循环10次
int n, m;
cin >> n >> m;
counts = int[10];
for(int i = 0; i < 10; ++i) {
counts[i] = 0;
}
for(int j = n; j <= m; j++) {
int z = j;
do {
int digit = z % 10; // LAST DIGIT OF FIRST NUMBER IN INTERVAL
counts[digit]++;
z /= 10;
} while (z != 0);
}
for(int i = 0; i < 10; ++i) {
cout << i << " occurrences " << counts[i] << " times";
}
intn,m;
cin>>n>>m;
计数=整数[10];
对于(int i=0;i<10;++i){
计数[i]=0;
}
对于(int j=n;j,您可以使用std::stringstream获取数字中的每个数字,如下所示:
constexpr int n = 19;
constexpr int m = 23;
std::array<int, 10> digit_count = {0};
for (int i = n; i <= m; i++)
{
std::stringstream s;
s << i;
unsigned char digit;
while (s >> digit) digit_count[digit - '0']++;
}
constexpr int n=19;
constexpr int m=23;
std::数组数字_计数={0};
对于(整数i=n;i位)数字_计数[数字-'0']++;
}
迄今为止,所有答案都提供了复杂的算法:代码>O(m n)<代码>,即从<代码> n>代码>到<代码> m >代码>的线性关系。这里,我提供了一个具有强对数复杂度的方法。基本思想是先考虑每个数字的最后一个数字,然后再考虑第二个最后的等等。
为了简化代码,我稍微改变了问题,考虑范围<代码> [n,M-1 ] < /代码>,即排除<代码> M< /代码> .<
在此范围内有
m-n
个数字;如果这是10的倍数,则最后一个数字正好出现(m-n)/10
次。否则,我们必须考虑边缘。以下例程将计数
单位
乘以从n
到m-1
范围内所有数字中最后一位的出现次数
void count_last_digits(int n, int m, std::array<int,10> count&, int unit=1)
{
// 1 increment n until it has the same last digit as m
for(int dn=n%10, dm=m%10; n<m && dn!=dm; dn=++n%10)
count[dn] += unit;
// 2 add unit*(m-n)/10 to all counts
if(int cnt = unit*(m-n)/10) // avoid to add nothing
for(int d=0; d!=10; ++d)
count[d] += cnt;
}
函数count\u last\u digits()
和count\u digits()
分别具有复杂度O(1)
和O(ln(x))
。这两个函数都被称为O(ln(m))
次,因此后者控制着总体复杂度,即O(ln(m)^2)
请注意,这些函数假定0m
比n
大得多,那么任何在差异m-n
中具有线性复杂度的方法都会丢失).你能解释一下这里发生了什么吗?或者是一些评论?或者甚至是为什么这是对“为什么我的代码不工作?”这个问题的回答吗?当你发布了完全不同的代码时?代码工作,谢谢-你能澄清一下为什么需要临时的“z”或“mod”吗?我直接用了“j”“-错误在哪里,这里发生了什么?for(int j=n;jj用于循环比较jthank you@tdpi,您的解决方案更好。但是,如果您能解释我,我想知道我的原始循环中的错误在哪里-即,在我的“while(z!=0)”循环中发生了什么,-为什么它不在那里计数数字?
constexpr int n = 19;
constexpr int m = 23;
std::array<int, 10> digit_count = {0};
for (int i = n; i <= m; i++)
{
std::stringstream s;
s << i;
unsigned char digit;
while (s >> digit) digit_count[digit - '0']++;
}
void count_last_digits(int n, int m, std::array<int,10> count&, int unit=1)
{
// 1 increment n until it has the same last digit as m
for(int dn=n%10, dm=m%10; n<m && dn!=dm; dn=++n%10)
count[dn] += unit;
// 2 add unit*(m-n)/10 to all counts
if(int cnt = unit*(m-n)/10) // avoid to add nothing
for(int d=0; d!=10; ++d)
count[d] += cnt;
}
void count_digits(int x, std::array<int,10> &count, int unit=1)
{
for(; x; x/=10)
count[x%10] += unit;
}
std::array<int,10> count_all_digits(int n, int m)
{
std::array<int,10> count={0};
for(int unit=1; n<m; n/=10,m/=10,unit*=10) {
// count last digits
count_last_digits(n, m, count, unit);
// increment n to the next multiple of 10, but not above m
if(int inc = std::min(10-(n%10), m-n)) {
count_digits(n/10, count, unit*inc);
n += inc;
}
// decrement m to the previous multiple of 10, but not below n
if(int dec = std::min(m%10, m-n)) {
count_digits(m/10, count, unit*dec);
m -= dec; // not really necessary
}
}
return count;
}