想问问循环条件这么写为何程序不会终止?
while (answer != ture && 0<j<3)
{
j++;
printf("答错了\t请再输入一次\t你还有%d次机会", 3 - j);
scanf_s("%d", &answer);
}
哪有0<j<3这样的,要写成(j>0&&j<3)
#include<stdio.h>
//方法一
int main()
{
int n;
scanf("%d",&n);
double sum;
for(int i=1; i<=n; i++)
{
sum += 1.0/double(i);
}
printf("%f\n",sum);
return 0;
}
/*
10
2.928968
Process returned 0 (0x0) execution time : 3.110 s
Press any key to continue.
*/
#include<stdio.h>
int x,y;
int gcd(int a, int b)
{
if(b==0) return a;
return gcd(b, a%b);
}
void add(int a1, int b1,int a2, int b2)
{
x = a1*b2+a2*b1;
y = b1*b2;
int temp;
if(x>y)
temp = gcd(x,y);
else
temp = gcd(y,x);
//printf("%d/%d\ %dn", x,y, );
x = x/temp;
y = y/temp;
}
//方法二结果为分数
int main()
{
int n;
scanf("%d",&n);
x=1,y=1;
for(int i=2; i<=n; i++)
{
add(x,y,1,i);
}
printf("%d/%d\n",x,y);
return 0;
}
/*
10
7381/2520
Process returned 0 (0x0) execution time : 1.452 s
Press any key to continue.
*/