我正在编写程序来计算出现次数 [英] I am writing program to count number occurance

查看:75
本文介绍了我正在编写程序来计算出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  #include   <   stdio.h  >  
#include < conio.h >

int a,i, b,NUM;
int main()
{

scanf( %d,& a);

for (i = 0 ; i< 10; i ++)
{

while (a!= 0
{
num =(a%10);
if (num == i)
{
b ++;
}
a = a / 10;

}

printf( %d number%d次\\\
,I,b);

}

return 0 ;
}





我的尝试:



是的但不工作可以请你帮忙搞清楚它的错误

解决方案

学会正确缩进你的代码,它显示它的结构和它有助于阅读和理解。它还有助于发现结构错误。

  #include   < span class =code-keyword><   stdio.h  >  
#include < conio.h >

int a,i,b,num;
int main()
{
scanf( %d,&安培;一个);

for (i = 0 ; i< 10; i ++)
{

while (a!= 0
{
num =(a%10);
if (num == i)
{
b ++;
}
a = a / 10;
}
printf( %d number%d次\ n ,I,b);
}
return 0 ;
}



专业程序员的编辑器具有此功能,其他功能包括括号匹配和语法高亮。

Notepad++主页 [ ^ ]

ultraedit [ ^ ]

-----

你算法效率不高,它正在做10倍的工作。

使用数组和计数数字会更有效。

-----

建议:学习使用调试器,它是一个很好的学习工具,可以帮助你找到错误。



有一个工具可以让你看到你的代码正在做,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与什么进行比较应该这样做。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期效果时,你就接近了一个bug。


两个问题:



  1. 'a'被分为零但值永远不会重置。
  2. 'b'永远不会重置为零。



要解决这些问题,您可以创建一个额外的变量 c ,用于执行div除以10并复制 a 在循环开始时到它,并在循环开始时将 b 设置为零,如下所示:

< pre lang =c> int a,i,b,c,num;
int main()
{

scanf( %d,& a);

for (i = 0 ; i< 10 ; i ++)
{
b = 0 ;
c = a;
while (c!= 0
{
num = (c% 10 );
if (num == i)
{
b ++;
}
c = c / 10 ;
}
printf( %d number%d次\ n ,我,b);
}
return 0 ;
}



请注意,如果输入大于整数的最大值,您将看到意外的结果:整数将 溢出 [ ^ ],该值不是您输入的值。如果你想对一个数字不受限制的数字执行此操作,你必须将值存储在一个字符串中,然后对字符串的字符进行比较。


你应该发生每个数字以 a 的相同值开头,也就是说,你需要备份它。尝试

  #include   <   stdio.h  >  

int main()
{

int a,i;
scanf( %d,& a);

for (i = 0 ; i< 10; i ++)
{
int tmp = a;
int cnt = 0 ;
int dgt;

while (tmp!= 0
{
dgt =(tmp% 10 );
if (dgt == i)
{
cnt ++;
}
tmp = tmp / 10 ;
}

printf( %d digit%d次\ n ,我,cnt);

}

return 0 ;
}


#include<stdio.h>
#include<conio.h>

int a,i,b,num;
int main()
{

scanf("%d",&a);

	for(i=0;i<10;i++)
{
	
	while(a!=0)
	{
		num=(a%10);
		if(num==i)
		{
			b++;
		}
		a=a/10;
	
	}

	printf(" %d number  %d  times\n",i,b);

}
	
	return 0;
}



What I have tried:

yes but not working can u please help to figure out whats wrong in it

解决方案

Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.

#include<stdio.h>
#include<conio.h>

int a,i,b,num;
int main()
{
  scanf("%d",&a);

  for(i=0;i<10;i++)
  {

    while(a!=0)
    {
      num=(a%10);
      if(num==i)
      {
        b++;
      }
      a=a/10;
    }
    printf(" %d number  %d  times\n",i,b);
  }
  return 0;
}


Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
-----
You algorithm is not efficient, it is doing 10 times the work.
It is more efficient to use an array and count digits as they appears.
-----
Advice: Learn to use the debugger, it is a great learning tool that help you to find bugs.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


Two problems:


  1. 'a' gets divided down to zero but the value never gets reset.
  2. 'b' never gets reset to zero.


To fix these problems, you can create an extra variable c that you'll use to perform the divison by 10 and copy a to it at the start of the loop, and to set b to zero at the start of the loop, like this:

int a, i, b, c, num;
int main()
{

	scanf("%d", &a);

	for (i = 0; i < 10; i++)
	{
		b = 0;
		c = a;
		while (c != 0)
		{
			num = (c % 10);
			if (num == i)
			{
				b++;
			}
			c = c / 10;
		}
		printf(" %d number  %d  times\n", i, b);
	}
	return 0;
}


Note that you'll see unexpected results if the input is larger than the max value of an integer: the integer will overflow[^] and the value wouldn't be what you inputted. If you want to do this for a number with unlimited digits, you'll have to store the value in a string instead and perform comparisons on the characters of the string.


You should the occurrence of every digit starting with the same value of a, that is, you need to backup it. Try

#include <stdio.h>

int main()
{

  int a, i;
  scanf("%d",&a);

  for(i=0;i<10;i++)
  {
    int tmp = a;
    int cnt = 0;
    int dgt;

    while(tmp != 0)
    {
      dgt = (tmp % 10);
      if ( dgt == i)
      {
        cnt++;
      }
      tmp = tmp / 10;
    }

    printf(" %d digit  %d  times\n", i, cnt);

  }

  return 0;
}


这篇关于我正在编写程序来计算出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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