64,660
社区成员
发帖
与我相关
我的任务
分享
long long int n = 3;
long long int arr[3] = {-2,1,3};
long long int totle = 0;
long long int mod = 0;
for(int i = 0; i < n; i++)
{
long long int a = arr[i];
totle += a / n;//totle不会超过64位
mod += a % n;//mod在[-20亿,+20亿]之间,也不会超过64位
totle += mod / n;
mod %= n;
}
printf("%f\n", ((double)mod)/n + totle);
#ifndef _SUMER_H_
#define _SUMER_H_
#include <stdint.h>
#include <math.h>
#define MAXUINT64 ((uint64_t)0xFFFFFFFFFFFFFFFF)
class sumer
{
public:
sumer(): high_(0), low_(0), count_(0){}
void add(uint64_t n)
{
uint64_t sum = n + low_;
if(sum < n && sum < low_)
{
// overflow
++high_;
}
low_ = sum;
++count_;
}
uint64_t avg()
{
double sqrtc = sqrt(count_);
return (high_/sqrtc)*(MAXUINT64/sqrtc)+(low_/count_);
}
uint64_t count(){return count_; }
void clear() { high_ = low_ = count_ = 0; }
private:
uint64_t high_;
uint64_t low_;
uint64_t count_;
};
#endif