Wednesday, December 12, 2007

Mobile Casanova

ACCEPTED !! Yet another opener...this one from last year's Dhaka regionals
____________________________________________________________________
Question: Mobile Casanova
____________________________________________________________________

#include "iostream.h"
#include "math.h"

int compareEnds(int start, int end) {
int n = floor(log(start)) + 1;
int p = 1;
for(int i = 0; i <= n; i++) {
if(start / p == end / p) return i;
p *= 10;
}
}

void printRange(int start, int count) {
cout << "0";
if(count == 0) cout << start << endl;
else {
int end = start + count;
int d = compareEnds(start, end);
cout << start << "-" << end % (int)pow(10, d) << endl;;
}
}

int main()
{

int t = 1;
while(1) {
int n;
cin >> n;
if(n == 0) break;
cout << "Case " << t << ":\n";
int count = 0,start;
cin >> start;
for(int i = 1; i < n; i++) {
int curr;
cin >> curr;
if(curr == start + count + 1) count++;
else {
printRange(start, count);
count = 0;
start = curr;
}
}
printRange(start, count);
cout << endl;
t++;
}
return 0;
}
____________________________________________________________________

No comments:

Contributors