1 条题解

  • 0
    @ 2023-6-11 12:20:56

    C :

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

    C++ :

    #include<bits/stdc++.h>
    using namespace std;
    int main(){
    	int sum=0,n=0,a,i;
    	cin>>a;
    	for(i=1;i<=a;i++){
    		if(i%2==0&&i%3!=0){
    			sum=sum+i;
    			n=n+1;
    		}
    	}
    	cout<<n<<endl;
    	cout<<sum<<endl;
    	return 0;
    }
    

    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 s=0;
           int num=0;
           for(int i=1;i<=n;i++) {
                if(i%2==0 && (i%3==1 || i%3==2)) {
                	num++;
                	s=s+i;
                   }
             } 
             System.out.println(num);
             System.out.println(s);
                
       }
    }
    

    Python :

    n = int(input())
    c = 0
    s = 0
    for i in range(1, n + 1):
        if i % 2 == 0 and i % 3 != 0:
            c += 1
            s += i
    print(c)
    print(s)
    
    
    • 1

    【入门】求出1~n中满足条件的数的个数和总和?

    信息

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