谦上谦 @ 2021-08-17 15:42:15
#include<bits/stdc++.h>
using namespace std;
int n,t=1,s;
int main()
{
cin>>n;
for (int i=1;i<=n;)
{
for (int j=1;j<=t;j++)
{
s+=t,i++;
}
t++;
if(i==n)
break;
}
cout<<s;
return 0;
}
一个小学生疑问
by Starlight_StationOI @ 2021-08-17 15:48:33
你输入5看看你就知道错哪儿了 正确输出是11
by NastiY_iN_saNitY @ 2021-08-17 15:49:44
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,ans,i;
cin>>n;
for(i=1;i<=n;n-=i++)ans+=i*i;
cout<<ans+i*n;
return 0;
}
by Starlight_StationOI @ 2021-08-17 15:50:00
在j的循环内没有考虑j<t但i==n的情况
by NastiY_iN_saNitY @ 2021-08-17 15:50:22
思路参见第一篇题解
by Starlight_StationOI @ 2021-08-17 15:51:17
@谦上谦
by jyb666 @ 2021-08-17 15:51:35
@谦上谦
#include<bits/stdc++.h>
using namespace std;
int n,t=1,s;
int main()
{
cin>>n;
for (int i=1;i<=n;)
{
for (int j=1;j<=t;j++)
{
s+=t,i++;
if(i>n)
break;
}
t++;
}
cout<<s;
return 0;
}
if(i>n)break;
在循环里判
by jyb666 @ 2021-08-17 15:54:04
@谦上谦 有可能出现 i>n 却继续 s+=t
by vincent06 @ 2021-08-17 15:56:46
加完i的每一步都要判断i是否==n,否则会多加
by 谦上谦 @ 2021-08-18 20:46:01
#include<bits/stdc++.h>
using namespace std;
int n,t=1,s;
int main()
{
cin>>n;
for (int i=1;i<=n;)
{
for (int j=1;j<=t;j++)
{
if(i>n)
break;
s+=t,i++;
}
t++;
if(i>n)
break;
}
cout<<s;
return 0;
}