#include <stdio.h>
int count_cycle(int n);
int main() {
int N; scanf("%d", &N);
printf("%d",count_cycle(N));
return 0;
}
int count_cycle(int n) {
int N = n; int _N;
int count = 0;
int x, y;
while(_N!=N) {
x = N / 10;
y = N % 10;
_N = 10 * y + ((x+y)%10);
count++;
}
return count;
}
我一直在练习关于上面表示的具有循环的特定自然数的基本算法。但是这段代码的结果是无限循环。我想要的伪代码是这样的。
function count_cycle
count = 0
==loop==
N = 10x + y (0 <= x,y <= 9)
N' = 10 * y + (x+y) mod 10
if N' == N then
break;
else then
N = N'
count++
continue;
====
return count
我应该如何使此代码正常工作?
你错过了 _N 的初始化,所以它永远不会等于 N,你将永远停留在 while 循环中
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句