如何计算长整数中不同位数的数量? [英] How do I calculate the number of different digits in a long long integer?

查看:73
本文介绍了如何计算长整数中不同位数的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于这个函数,我不能使用数组或字符串。



示例:给定整数4321432143,函数将输出4,因为有4个不同的数字数字。



12345将返回5,123将返回3等。



我尝试了什么:



  void  calcUnique(  long   long  num)
{
int 唯一; // 给定数字的不同位数
int 数字
long long ;
int 见过;
int count;

value = num;
见= 0 ;
unique = 0 ;
count = 0 ;

执行
{
digit = value 10 ;
if ((见&( 1 << digit))== < span class =code-digit> 0

{
++ count;
见过=见过( 1 << digit);
}
value = value / 10 ;
} while value > 0 < /跨度>);

printf( 总独特数字:%d \ n,独特);
return ;
}

解决方案

你已经走好了路。但是在显示之前,你永远不会将 unique 设置为除0之外的任何其他值。并且 count 除了在中增加 -statement ......之外没有用于任何事情。 br $>


(故意多个点)


你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到有一点它会停止你所期望的。

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

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]



试一试吧!

调试器是一个很好的工具,可以帮助你理解错误。

当你提出问题时,别忘了解释出了什么问题。

For this function, I cannot use arrays or strings.

Example: Given the integer 4321432143, the function would output 4, since there are 4 different digits in the number.

12345 would return 5, 123 would return 3, etc.

What I have tried:

void calcUnique(long long num)
{
  int unique; //number of different digits in given number   
  int digit
  long long value; 
  int seen;
  int count; 

  value = num;
  seen = 0;
  unique = 0;
  count = 0;
 
 do
  {
    digit = value % 10;
    if((seen & (1 << digit)) == 0)
    {
      ++count;
      seen = seen | (1 << digit);
    }
    value = value / 10;
  }while(value > 0);

  printf("Total Unique Digits: %d\n", unique);
  return;
}

解决方案

You're already on a good way. But you never set unique to any other value than 0 before you display it. And count isn't being used for anything except increasing it in that if-statement......

(multiple dots on purpose)


You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

Give it a try!
The debugger is a great tool to understand what is wrong.
Don't forget to explain what is wrong when you ask a question.


这篇关于如何计算长整数中不同位数的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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