1 条题解

  • 0
    @ 2025-10-10 15:48:07

    C :

    #include<stdio.h>
    
    void main(){
    	int i,n,s=0;
    	scanf("%d",&n);
    	
    	for(i=1;i<=n;i++){
    		if((i%2==0&&i%3==0)||(i%2==0&&i%5==0)||(i%2==0&&i%7==0)||(i%3==0&&i%5==0)||(i%3==0&&i%7==0)||(i%5==0&&i%7==0)||(i%2==0&&i%3==0&&i%5==0)||(i%2==0&&i%3==0&&i%7==0)||(i%3==0&&i%5==0&&i%7==0)||(i%2==0&&i%3==0&&i%5==0&&i%7==0)){
    			s=s+i;
    		}
    	}
    	printf("%d",s);
    }
    

    C++ :

    #include <bits/stdc++.h>
    using namespace std;
    
    int main(){
    	int n,i,c,s = 0;
    	cin>>n;
    	for(i = 1;i <= n;i++){
    		c = 0;
    		if(i % 2 == 0){
    			c++;
    		}
    		
    		if(i % 3 == 0){
    			c++;
    		}
    		
    		if(i % 5 == 0){
    			c++;
    		}
    		
    		if(i % 7 == 0){
    			c++;
    		}
    		
    		if(c >= 2){
    			s = s + i;
    		}
    	}
    	
    	cout<<s<<endl;
    }
    
    
    

    Pascal :

    var n,i,s:longint;
    begin
    read(n);
    for i:=1 to n do
    if (i mod 6=0)or(i mod 10=0)or(i mod 14=0)or(i mod 15=0)or(i mod 21=0)or(i mod 35=0) then s:=s+i;
    write(s);
    end.
    
    

    Java :

    import java.util.*;
    public class Main{
    	public static void main(String[] args){
    		Scanner sc = new Scanner(System.in);
    		int n = sc.nextInt();
    		int a = 0;
    		for(int i = 1;i<=n;i++) {
    			if ((i%2==0&&i%3==0)||(i%2==0&&i%5==0)||(i%2==0&&i%7==0)||(i%3==0&&i%5==0)||(i%3==0&&i%7==0)||(i%5==0&&i%7==0))
    			{a += i ;
    			}
    
    		}
    			System.out.println(a);
    	}
    }
    //i%2==0&&i%3==0&&i%5==0&&i%7==0
    

    Python :

    n=int(input())
    i=1
    s=0
    for i in range(1,n+1):
        if i%6==0 or i%10==0 or i%14==0 or i%15==0 or i%21==0 or i%35==0:
            s+=i
            i+=1
    print(s)
    
    • 1

    信息

    ID
    429
    时间
    1000ms
    内存
    32MiB
    难度
    10
    标签
    递交数
    1
    已通过
    1
    上传者