答案1(可能运行超时,但是比较保守):
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int main(){
freopen("crazy.in","r",stdin);
freopen("crazy.out","w",stdout);
long int n,cnt = 0,tmp = 0,f = 0;
cin >> n;
string s;
for(int i = 0;i < n;i++){
f++;tmp = 0;
s[i] = (char)(f + 48);
for(int j = 0;j <= i;j++){
tmp += (int)(s[j] - 48);
}
if(tmp % 3 == 0){
cnt++;
}
}
cout << cnt;
return 0;
}
答案2(最简单,能拿满分的方法):
#include<cstdio>
using namespace std;
int main(){
freopen("crazy.in","r",stdin);
freopen("crazy.out","w",stdout);
int n,tmp = 0,cnt = 0;
scanf("%d",&n);
for(int i = 1;i <= n;i++){
tmp += i;
if(tmp % 3 == 0){
cnt++;
}
}
printf("%d",cnt);
return 0;
}