我的程序停止循环,我无法弄清楚原因

时间:2014-04-03 21:37:57

标签: c loops

如果用户输入"值"如1223445所示,输出应如下所示:

改变后#1:12235 改变后#2:135

代码意味着取出两个具有相同值的连续数字。第一个循环工作,但然后停止,我无法理解为什么。这是代码:

{

int count;  
int y;
int z;
int b;
int c;
int d;
int a;
int ct;
y = 0;
z = 1;
d = 0;
count = 0;
if (value > 0)
  while ((z * 10 + z) != (y % 100))
  {
    y = value % 10 + y * 10;
    z = y % 10;
    value /= 10;
    count = count + 1;
  }
  value = value * pow(10, count - 2);
  y = y / 100;
  count = count - 3;
  while(y > 0)
  {
    b = y % 10;
    c = pow(10, count);
    d = d + c * b;
    y = y / 10;
    count = count - 1;
  }
value = value + d;
ct = 1;
printf("After change #%d: %d\n", ct, value);
a = value;
while (a > 1)
  {
  if((a % 100) - (a % 10) - (10 * (a % 10)) == 0)
    Change(value);
  else
    a = a / 10;
  }
  return; 
}

1 个答案:

答案 0 :(得分:0)

这是一个简单的解决方案:(不确定如何处理奇数重复,例如'111','11111')

    public static long strip(long value, long sum) {
        if(value==0) return sum;

        long tens = value % 100;
        long ones = value % 10;
        if(ones*10 == tens-ones) {
            return strip(value /100, sum);
        }

        if(sum==0) sum +=(value%10);
        else {
            long x = (long)Math.ceil((Math.log10(sum)));
            sum =(long) (Math.pow(10,x) * ones + sum);
        }
        return strip(value /10, sum);
   }