c++使用循环求出 1/3-2/5+3/7-4/9+……-100/201
时间: 2023-05-22 08:07:04 浏览: 181
求1+1/2+1/3+1/4+…….+1/100的值
可以通过以下代码来计算:
float sum = 0;
for(int i = 1; i <= 100; i++){
int numerator = i * 2 - 1;
int denominator = i * 2 + 1;
if(i % 2 == 0){
sum -= (float)numerator/denominator;
} else {
sum += (float)numerator/denominator;
}
}
printf("计算结果: %f", sum);
阅读全文