求和(c)
求 :1 - 1/2 + 1/3 - 1/4 + … +1/99 - 1/100
#include <iostream>
#include <process.h>
int main(int argc, char *argv[])
{
using namespace std ;
int sign = -1 ;
long double sum = 0 ;
int val ;
for (val = 1; val <= 100; ++val)
{
sign = (-1) * sign ; //正负符号处理办法
sum += sign * (1 / val) ;
}
cout << sum << endl ;
system("pause") ;
return 0 ;
}
#include <process.h>
int main(int argc, char *argv[])
{
using namespace std ;
int sign = -1 ;
long double sum = 0 ;
int val ;
for (val = 1; val <= 100; ++val)
{
sign = (-1) * sign ; //正负符号处理办法
sum += sign * (1 / val) ;
}
cout << sum << endl ;
system("pause") ;
return 0 ;
}