1 条题解

  • 0
    @ 2023-6-11 12:17:58

    C :

    #include<stdio.h>
    
    void main(){
    	int n,i,j,t=1,s=0,f;
    	scanf("%d",&n);
    	
    	f=1;
    	for(i=1;i<=n;i++){
    		t=t*i;
    		s=s+t*f;
    		f=f*-1;
    		}
    	printf("%d",s);
    }
    

    C++ :

    #include <bits/stdc++.h>
    using namespace std;
     
    int main(){
        int n,i,j,f,s = 0,x;
        f = 1;
        cin>>n;
        for(i = 1;i <= n;i++){
        	x = 1;
        	for(j = 1;j <= i;j++){
        		x = x * j;
    		}
        	s = s + f * x;
        	
        	f = f * -1;
    	}
    	
    	cout<<s<<endl;
    }
    

    Java :

    import java.util.Scanner;
    
    public class Main {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int n = sc.nextInt();
    		int i,sum1=0,sum2=0;
    		sc.close();
    		for(i=1;i<=n;i++) {
    			if(i%2==0) {
    				sum1+=jiecheng(i);
    			}else {
    				sum2+=jiecheng(i);
    			}	
    		}
    		System.out.println(sum2-sum1);
    	}
    	public static int jiecheng(int n) {
    		int i,s=1;
    		for(i=1;i<=n;i++) {
    			s*=i;
    		}return s;
    	}
    }
    

    Python :

    n = int(input())
    s = 0
    for i in range(1, n + 1):
        t = 1
        for j in range(1, i + 1):
            t = t * j
        if i % 2 == 1:
            s = s + t
        else:
            s = s - t
    print(s)
    
    
    • 1

    信息

    ID
    2438
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者