http://acm.hdu.edu.cn/showproblem.php?pid=1001

这题是等差求和,不能直接sum=(1+n)*n/2;因为题目只是说结果在32bit之内,但(1+n)*n很可能超出32bit,所以这样写WA。

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <queue>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;

int main()
{
    __int64 n;
    while(scanf("%I64d",&n)!=EOF)
    {
        cout<<n*(1+n)/2<<endl<<endl;
    }
    return 0;
}

 http://acm.hdu.edu.cn/showproblem.php?pid=1008

简单模拟题

#include <iostream>
#include <queue>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#define inf 0x3f3f3f3f
using namespace std;
int main()
{
    int f[102],n,sum,t;
    while(scanf("%d",&n)!=EOF&&n!=0)
    {
        sum=0;
        memset(f,0,sizeof(f));
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&f[i]);
            t=f[i]-f[i-1];
            if(t>0)
            {
                sum+=t*6+5;
            }
            else if(t<0)
                sum+=(-t)*4+5;
            else sum+=5;
        }
        printf("%d\n",sum);
    }
    return 0;
}
View Code

相关文章:

  • 2022-12-23
  • 2022-03-08
  • 2021-12-12
  • 2021-07-10
  • 2022-12-23
  • 2021-06-30
  • 2022-12-23
  • 2021-10-20
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-01
  • 2022-02-13
相关资源
相似解决方案