C程序显示数字和重复数字的数字 [英] C program to show digits of a number and repeated digits

查看:97
本文介绍了C程序显示数字和重复数字的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

程序应扫描59149这样的数字并打印出来:



5

9

1

4

9

5重复1次

9重复2次
$ b $ 1 1重复1次

4重复1次



但我的问题是重复次数部分。我想出了一个想法并编写了RepeatedDigits函数,但似乎这个函数有问题。结果显示一些无意义的数字,如5重复-1次或5重复23512623次

和是的我知道我是菜鸟。



我尝试过:



 #include< stdio.h> 
#include< math.h>
main()
{
int Number,R,P,i,AllNumbers [10];
scanf(%d,& Number);
P =数字(数字);

while(Number!= 0)
{

R = Number / P;
Number = Number%P;
P = P / 10;
printf(%d \ n,R);

}

for(i = 0; i< = 9; ++ i)
printf(%d重复%d次\ n I,AllNumbers [I]);

}

int数字(int数字)
{
int Q = 11,Counter = 0,P;

while(Q> 10)
{
Q = Number / 10;
Counter = Counter + 1;
Number = Q;
}
P = pow(10,Counter);
返回P;
}

int RepeatedDigits(int Number)
{
int AllNumbers [10],R,i;

for(i = 0; i< = 9; ++ i)
{
AllNumbers [i] = 0;
}
而(Number!= 0)
{
R = Number%10;
AllNumbers [R] ++;
Number = Number / 10;

}
返回AllNumbers [R];
}

解决方案

对于这样一个简单的任务,你真的不需要发明函数,数组就足够了:

  #include   <   stdio.h  >  

int main()
{
int count [ 10 ] = { 0 };
int n;
printf( 插入正数\ n);
if (scanf( %d ,& n)!= 1 return - 1 ;
if (n< = 0 返回 - 1 ;
while (n> 0
{
++数[n%10];
n / = 10 ;
}

for (n = 0 ; n< 10 ; ++ n)
if (count [n])
printf( %d重复%d次\ n,n,count [n]);

return 0 ;
}


您的代码错误,需要完全重新设计您的功能。对于数字,您可以使用log(10)值和重复数字。

  int  singleNumber =( number /( 10  ^ digit))% 10 ; 

学习使用调试器和编写一些测试函数,结果很明显。


嗯,你正在创建AllNumbers:

 int Number,R, P,i,AllNumbers [10]; 

但是在你尝试使用它来在主函数中将数字传递给printf之前你不做任何其他事情:

 for(i = 0; i< = 9; ++ i)
printf(%d重复%d次\ n,i,AllNumbers [i]);



您在 RepeatedDigits 函数中创建了同名的第二个变量,但是你没有甚至打电话给那个,新的将使该功能内的外部版本不可用。



所以你打印的数字是未定义的 - 它们可以归结为任何东西在应用程序运行的内存中(以及您使用的编译器/选择进行编译的选项)。



我强烈建议你熟悉调试器 - 你如果你有的话会发现这一点!


the program should scan a number like 59149 and print these:

5
9
1
4
9
5 is repeated 1 time
9 is repeated 2 time
1 is repeated 1 time
4 is repeated 1 time

but my problem is with the repeated times part. i've come up with a idea and wrote the "RepeatedDigits " function but it seems there is something wrong with the function. the result shows some nonsense numbers like " 5 is repeated -1 time" or "5 is repeated 23512623 time"
AND yes i know i'm a noob.

What I have tried:

#include <stdio.h>
#include <math.h>
main()
{
	int Number,R,P,i,AllNumbers[10];
	scanf("%d",&Number);
	P=Digits(Number);
	
	while(Number!=0)
	{
	
		R=Number/P;
		Number=Number%P;
		P=P/10;
		printf("%d\n",R);
		
	}
	
	for(i=0;i<=9;++i)
		printf("%d is repeated %d times\n",i,AllNumbers[i]);
		
}

int Digits (int Number)
{
	int Q=11,Counter=0,P;
			
	while(Q>10)
		{
		Q=Number/10;
		Counter=Counter+1;
		Number=Q;
		}	
	P= pow(10,Counter);
	return P;
}

int RepeatedDigits (int Number)
{
int AllNumbers[10],R,i;

	for(i=0;i<=9;++i)
		{
			AllNumbers[i]=0;
		}
	while(Number!=0)
 {
  R=Number%10;
  AllNumbers[R]++;
  Number=Number/10;  
  
 }
return AllNumbers[R];
}

解决方案

For such a trivial task you really don't need to invent functions, the array is just enough:

#include <stdio.h>

int main()
{
  int count[10]={0};
  int n;
  printf("insert a positive number\n");
  if ( scanf("%d", & n) != 1) return -1;
  if ( n <= 0 ) return -1;
  while  ( n > 0)
  {
    ++count[ n%10];
    n /= 10;
  }

  for (n=0; n<10; ++n)
    if ( count[n] )
      printf("%d is repeated %d times\n", n, count[n]);

  return 0;
}


You code is wrong and need to redesign your functions completly. For digits you can use the log(10) value and for Repeated digits.

int singleNumber = (number / (10^digit)) % 10;

Learn to use the debugger and write some test functions, where the results are obvious.


Well, you are creating AllNumbers:

int Number,R,P,i,AllNumbers[10];

But you do nothing else with it until you try to use it to pass a digit to printf in your main function:

for(i=0;i<=9;++i)
    printf("%d is repeated %d times\n",i,AllNumbers[i]);


You do create a second variable of the same name inside your RepeatedDigits function, but you don't even call that, and the new one will make the external version unavailable within the function.

So the numbers you print are undefined - they are down to whatever is in memory when you app runs (and the compiler you use / options you select for compilation).

I'd strongly suggest that you get familiar with the debugger - you would have spotted this in moments if you had!


这篇关于C程序显示数字和重复数字的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆