qhchen2022 @ 2022-08-19 20:23:42
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,i,answer=0;
cin>>n;
for (i=1;i<=n;i++)
{
if (n-i>=0)
{
n-=i;
answer+=i*i;
}
}
answer+=(i+1)*n;
cout<<answer;
return 0;
}
本题只得了40分,不知道是哪里出了问题。麻烦指出错误,并给出正确改正方案。谢谢!
by wjryyds666 @ 2022-08-19 20:31:22
那你就再弄两个变量,不要靠i?
by qhchen2022 @ 2022-08-19 20:33:53
@wjryyds666 我是初学者,码力很弱。可否修改好我的代码后,发给我看一看?
by wjryyds666 @ 2022-08-19 20:34:51
@qhchen2022 私发?
by wjryyds666 @ 2022-08-19 20:38:20
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,ans=0,l=1,j=1;
cin>>n;
for(int i=1;i<=n;i++){
ans+=j;
if(l==j){
j++;
l=0;
}
l++;
}
cout<<ans;
return 0;
}
by wjryyds666 @ 2022-08-19 20:38:41
@qhchen2022
by liuqichen121 @ 2022-08-19 20:40:40
@qhchen2022 您好,你代码第15行的“+1”没有必要,可以改成一下的
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,i,answer=0;
cin>>n;
for (i=1;i<=n;i++)
{
if (n-i>=0)
{
n-=i;
answer+=i*i;
}
}
answer+=(i)*n;
cout<<answer;
return 0;
}
by qhchen2022 @ 2022-08-19 20:40:45
@wjryyds666 收到,谢谢!
by qhchen2022 @ 2022-08-19 20:41:22
@liuqichen121 收到,谢谢!
by wjryyds666 @ 2022-08-19 20:41:36
@qhchen2022 沒逝