当前位置: 代码迷 >> 综合 >> 紫书 例题8-5 UVa11054(等价转换)
  详细解决方案

紫书 例题8-5 UVa11054(等价转换)

热度:109   发布时间:2023-09-20 22:19:08.0
这道题用到了 等价转换的思想

所有要运到a1的酒, 都要经过a2, 所以不如把a2的值改成a1+a2,然后依次以此类推。

#include<cstdio>
#include<cmath> 
#define REP(i, a, b) for(int i = (a); i < (b); i++)
using namespace std;int main()
{int n;while(~scanf("%d", &n) && n){long long ans = 0, last = 0, x;REP(i, 0, n){scanf("%lld", &x);ans += abs(last);last += x;}printf("%lld\n", ans);}return 0;	
}