Thursday 9 February 2012

TopCoder SRM PROBLEM

    Installation programs often run several tasks, one after another. Each task is assigned an integer, the expected execution time of this task. During the installation, a progress bar shows the user what percentage of the installation time has elapsed. In this problem, the bar will be represented by a string containing exactly 20 characters. If X% of the installation has been completed, then the leftmost X% of these characters should be a '#', while the remaining characters should be '.'. If necessary, round down the number of '#' characters to the nearest integer less than or equal to the actual value (see example 0). The bar starts at 0% and is only updated each time a task finishes execution.
Create a class ProgressBar containing the method showProgress which takes a vector <int> taskTimes, the expected execution time for each task in the order they are run, and an int tasksCompleted, the number of these tasks that have been completed. The method should return a string containing exactly 20 characters showing the progress bar according to the descriptions above.

I HAVE SOLVE THIS QUESTION IN A VERY SIMPLE WAY...:)




#include<iostream>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<queue>
#include<list>
#define show(x) copy(x.begin(),x.end(),output)
#include<iterator>
#define sort(x) sort(x.begin(),x.end())
#include<string>
#define vs vector<string>
#define vi vector<int>
#define pb push_back
#include<map>
#include<algorithm>
#include<sstream>
#include<stack>
#include<cmath>
#include<cstring>
#include<iomanip>
#include<cctype>
using namespace std;


class ProgressBar
{
public:
string showProgress (vi  taskTimes , int tasksCompleted)
{
string ret;
int ctotal = 0;
double per = 0;
int total = 0;
for(int i = 0 ; i <taskTimes.size() ; i++)
{
total = total + taskTimes[i];
if( i == tasksCompleted - 1)
{
ctotal = total;
}
}
per = double(ctotal / (total * 1.000));
total = int(20 * per);
for(int j = 0 ; j < 20 ; j++)
{
if( j < total)
{
ret = ret + "#";
}
else
{
ret = ret + ".";
}
}
return ret;
}
};
int main()
{
ProgressBar p;
vi a;
a.pb(19);
a.pb(20);
a.pb(25);
a.pb(35);
a.pb(39);
int c = 3;
string d = p.showProgress(a,c);
cout<<d<<endl;
cin.get();
return 0;
}

BY SAURABH BHATIA ( GNDU , RC , JAL)

No comments:

Post a Comment