有没有大佬帮我看看哪里错了?

wjl2014 2024-05-30 20:36:59 20

#include<bits/stdc++.h> using namespace std; int main() { int y,u=0; cin>>y; for(int h=1;h<=y;h++) { if(h%3==0) { u=u+y; } } cout<<u; return 0; }

{{ vote && vote.total.up }}

共 3 条回复

wc02222
#include<bits/stdc++.h>
using namespace std;
int main() {
int y,u=0;
cin>>y;
for(int h=1;h<=y;h++){
   if(h%3==0){
      u=u+h;
   }
}
cout<<u;
return 0;
}

帮你改好了

wc02222

@wjl2014

你这代码最主要的问题在于:你要加h,别加y,

if(h%3==0) 
{ u=u+y;
}

这段代码错误

nfxx10

#include<bits/stdc++.h> using namespace std; int main(){ int n;cin>>n; int s=0; for(int i=0;i<=n;i++){ if(i%3==0){ s+=i; } } cout<<s; return 0; }