Inspirated

 
 

June 30, 2008

Programming Challenges: The Trip

Filed under: Blog — krkhan @ 8:31 am

PC/UVa IDs: 110103/10137, Popularity: B, Success rate: average, Level: 1

I have more wrong submissions for this problem than any other one until now. The reason? I was oblivious to the fact that the default rules for type-conversion between double and long in C++ include floor()ing positive values and ceil()ing negative ones.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <cmath>
#include <vector>
#include <numeric>
 
using namespace std;
 
int main(int argc, char *argv[])
{
	cout.setf(ios::fixed | ios::showpoint);
	cout.precision(2);
 
	int count;
	cin>>count;
	while(count != 0) {
		vector< double > v;
		for(int i = 0; i < count; i++) {
			v.push_back(*istream_iterator< double >(cin));
		}
 
		double avg = accumulate(v.begin(), v.end(), 0.0) / v.size();
		double low = 0, high = 0;
		for(int i = 0; i < count; i++) {
			double diff = v.at(i) - avg;
 
			if(diff < 0) {
				low -= ceil(diff * 100) / 100;
			} else {
				high += floor(diff * 100) / 100;
			}
		}
 
		cout<<"$"<<(low > high? low : high)<<endl;
 
		cin>>count;
	}
 
	return 0;
}
Tags: , , , , ,

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.