Chinaunix

标题: 平衡数的判断 [打印本页]

作者: 黑苦咖啡    时间: 2005-11-14 11:31
标题: 平衡数的判断
写段程序,判断一个正整数是否是平衡数(如3,121, 3443,23532等均是平衡数),不能调用把数字转化为字符串的函数。

int BalanceNumber(unsigned int i)   
{
        int a[10];
        int k;
        int count = 0;
        int low, high;
        while (i) {
                k = i%10;
                a[count++] = k;
                i = i/10;
        }
        --count;
        low = 0; high = count;
        while (a[low] == a[high] && low<=high) {
                low++; high--;
        }
        if (low>high) {
                return 1;
        }else{
                return 0;
        }
}  
main()
{
        unsigned int i = 23532;
        if(BalanceNumber(i))
                printf("This is a balanceNumber.n");
        else
                printf("This is not a balanceNumber.n");
}

望各位大虾批评指正,总觉得 用数组不是太好。
作者: renstone921    时间: 2005-11-14 12:13
总记得当初老师讲这个东西时说:"当你考虑使用队列和栈来解决回文数这个问题,你就开始尝试使用数据结构的思考方法解决问题了。"
作者: whyglinux    时间: 2005-11-14 12:19
  1. /** If i is a balanced number, return 1; otherwise, return 0. */
  2. /**
  3. * Balanced number examples: 3, 33, 121, 3443, 23532
  4. */
  5. int balance_number( int i )
  6. {
  7.   int m = i;
  8.   int n = 0;

  9.   /* create an inversed-order number */
  10.   do {
  11.     n = n * 10 + m % 10;
  12.   } while ( m /= 10 );

  13.   /* are the original number and the inversed one equal? */
  14.   return !(i - n);
  15. }
复制代码

[ 本帖最后由 whyglinux 于 2005-11-14 12:34 编辑 ]
作者: dbcat    时间: 2005-11-14 12:37
原帖由 whyglinux 于 2005-11-14 12:19 发表
[code]/** If i is a balanced number, return 1; otherwise, return 0. */
/**
* Balanced number examples: 3, 33, 121, 3443, 23532
*/
int balance_number( int i )
{
  int m = i;
  int n = 0;

...



Just inverse the number:
$ function num(){ expr $(echo $1 | rev ) = $2; }
$ num 12345 12345
0
$ num 123454321 123454321
1
作者: 黑苦咖啡    时间: 2005-11-14 12:53
3楼的真高,佩服!
作者: yzc2002    时间: 2005-11-14 13:27
  1. int balance_number( int n )
  2. {
  3.   int m = 0;

  4.   while(m < n)
  5.   {
  6.     m = m * 10 + n % 10;
  7.     n /= 10;
  8.   }

  9.   return m > n ? !(m / 10 - n) : 1;
  10. }
复制代码

作者: whyglinux    时间: 2005-11-14 15:47
原帖由 yzc2002 于 2005-11-14 13:27 发表
  1. int balance_number( int n )
  2. {
  3.   int m = 0;

  4.   while(m < n)
  5.   {
  6.     m = m * 10 + n % 10;
  7.     n /= 10;
  8.   }

  9.   return m > n ? !(m / 10 - n) : 1;
  10. }
复制代码


高!
作者: mike_chen    时间: 2005-11-14 15:52
^_^, 以前华为公司的面试有这道题!
作者: 黑苦咖啡    时间: 2005-11-14 16:08
hehe...
TO 楼上的:偶正是去那里面试遇到的题,当时就这道题没有做好,遗憾讷!
作者: openX    时间: 2005-11-14 17:02
原帖由 whyglinux 于 2005-11-14 12:19 发表
[code]/** If i is a balanced number, return 1; otherwise, return 0. */
/**
* Balanced number examples: 3, 33, 121, 3443, 23532
*/
int balance_number( int i )
{
  int m = i;
  int n = 0;

...


beautiful code!
作者: 大司南    时间: 2005-11-14 17:04
原帖由 yzc2002 于 2005-11-14 13:27 发表
  1. int balance_number( int n )
  2. {
  3.   int m = 0;

  4.   while(m < n)
  5.   {
  6.     m = m * 10 + n % 10;
  7.     n /= 10;
  8.   }

  9.   return m > n ? !(m / 10 - n) : 1;
  10. }
复制代码

能不能解释一下??
作者: rainballdh    时间: 2005-11-14 17:08
6楼和3楼的CODE都漂亮,佩服
作者: yarco1    时间: 2005-11-14 18:39
恩...高. 看到差距了.

原帖由 大司南 于 2005-11-14 17:04 发表
能不能解释一下??


大侠们都比较忙.
    m = m * 10 + n % 10;    //m从低位加到高位, 逆序
    n /= 10;  //n不断去掉低位

佩服. 早看出这两位是高手了.




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2