1 solutions

  • 0
    @ 2023-1-16 14:36:36
    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
    	int n;
    	cin >> n;
    	double s = 0;//定义小数变量代表n项的和
    	//循环遍历1到n
    	for(int i = 1; i <= n; i++){
    		//如果i是奇数用加法,i是偶数用减法
    		if(i%2 == 0){
    			s -= 1.0/i;
    		}
    		else {
    			s += 1.0/i;
    		} 
    		
    	} cout << fixed << setprecision(4)<< s << endl;
    	return 0;
    }
    
    
    • 1

    Information

    ID
    858
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    10
    Tags
    # Submissions
    2
    Accepted
    2
    Uploaded By