这是一个用C编写的简单程序,它向用户询问int n
找出从2
到n
范围内的两个连续素数之间的最大差异的最佳方法是什么?
int main() {
int n, i, j, c;
clrscr();
printf("Enter Range To Print Prime Numbers:");
scanf("%d", &n);
printf("Prime Numbers Are Following:\n");
for(i=1, c=0; i<=n; i++) {
for (j=1; j<=i; j++)
if (i % j == 0)
c++;
if (c == 2)
printf("%d ", i);
}
printf("\n\n The highest difference between numbers is:");
/* logic i cant get */
getch();
}
答案 0 :(得分:1)
跟踪变量中两个连续素数之间的最大差异,如果新差异较大,则用新差异替换变量。
int maxDifference = 0;
int currentDifference;
// begin looping
currentDifference = calculateNewDifference();
if(currentDifference > maxDifference) {
maxDifference = currentDifference;
}