紫书 例题8-5 UVa11054(等价转换)
这道题用到了等价转换的思想
所有要运到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;	
} 
                
            
        
浙公网安备 33010602011771号