elf465504453 2011-04-30 07:12:00
点我顶贴
收藏
删除
关键公式:等差数列前n项之和Sn=(Astart+Aend)*n/2
有了这个公式就很好办了,利用宏定义,定义一个I常量代表上式中的n
#define I (end-start+1)
这个I就是n的值
后面就简单了,直接贴出代码,不懂可提问
#include<stdio.h>
#define I (end-first+1)
int main()
{
/*(first+end)*(end-first+1)/2*/
long int m,first,end,temp;
scanf("%ld",&m);
first=0;end=1;
while(first<m){
temp=(first+end)*I;
temp=temp/2;
if(temp==m)printf("%ld %ld\n",first,end);
end++;
if((first+end)>m){
first++;
end=first+1;
}
}
return 0;
}
在Ubuntu10.10版Vim编辑,Gcc编译通过。
提交结果:
状态: Accepted
测评机: Xeost[5]
得分: 100分
提交日期: 2011-4-30 22:06:00
有效耗时: 891毫秒
测试结果1: 通过本测试点|有效耗时141ms
测试结果2: 通过本测试点|有效耗时47ms
测试结果3: 通过本测试点|有效耗时47ms
测试结果4: 通过本测试点|有效耗时62ms
测试结果5: 通过本测试点|有效耗时594ms