我目前在 Project Euler 的 Probelm 57 中工作:
https://projecteuler.net/problem=57
我的问题是,虽然我相信给出了正确的答案,但数字的计数似乎不正确。我不确定错误可能是什么。我已经包含了注释,希望能让我的代码更清晰。
我的代码正确识别 1393/985 分数,但在大约 i=30 之后,它似乎变得混乱(可能在某处溢出?)。
提前致谢!
PS显然答案是153,而我得到253
#include <stdio.h>
long unsigned int fraction(long unsigned int *x, long unsigned int *y);
int main(){
int i, j, count=0; //initialise loop counters
for (i=0; i<1000; i++){ //number of iterations
long unsigned int x=1, y=2; //this is the starting position values
for (j=1; j<i; j++){ //iterate through "fraction" function i-1 times
fraction(&x, &y);
}
x+=y; //add the extra "1" onto the final answer
long unsigned int xt=0, t1=x, t2=y, yt=0; //xt and yt are digit counters
while(t1!=0){ //count the number of digits in the numerator
t1 /= 10;
xt++;
}
while(t2!=0){ //count the number of digits in the denominator
t2 /= 10;
yt++;
}
if (xt>yt){ //compare length of numerator and denominator
count++;
}
}
printf("Count is %i\n",count);
}
long unsigned int fraction(long unsigned int *x, long unsigned int *y){ //function to derive a fraction based on the number of iterations
long unsigned int temp;
*x+=2*(*y);
temp=*x;
*x=*y;
*y=temp; //modify pointers to reflect new values
}