Created
July 24, 2012 14:42
-
-
Save logicmd/3170312 to your computer and use it in GitHub Desktop.
Problem C. Recycled Numbers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/************************************************************************* | |
Author: logicmd | |
Created Time: 2012/7/24 17:43:17 | |
File Name: RecycledNumbers.cpp | |
Description: | |
************************************************************************/ | |
#include <cassert> | |
#include <iostream> | |
#include <set> | |
#include <map> | |
#include <cstdio> | |
#include <string> | |
#include <utility> | |
#include <algorithm> | |
#define MAXT 50 | |
using namespace std; | |
typedef pair<int, int> RNumSets; | |
int shift(int x) | |
{ | |
int len = 0, forestep = 1; | |
for(int _x = x; _x > 0; _x /= 10) | |
{ | |
len ++; | |
forestep *= 10; | |
} | |
int last, fore, step = 1; | |
for(int i = 0; i < len; i++) | |
{ | |
step *= 10; | |
last = x % step; | |
fore = x / step; | |
if(last != 0) | |
break; | |
} | |
forestep /= step; | |
return last * forestep + fore; | |
} | |
set< RNumSets > findRecycledNumbers(int lower, int higher) | |
{ | |
set< RNumSets > container; | |
for(int x = lower; x <= higher; x++) | |
{ | |
// if(container.find(x) != container.end()); | |
int y = shift(x); | |
while(y != x) | |
{ | |
if( y >= lower && y <= higher ) | |
{ | |
container.insert(make_pair(x, y)); | |
container.insert(make_pair(y, x)); | |
break; | |
} | |
y = shift(y); | |
} | |
} | |
// | |
return container; | |
} | |
void getRecycledNumbers(set< RNumSets > container) | |
{ | |
cout << "Recycled Numbers: " << endl; | |
for( set< RNumSets >::iterator it = container.begin(); it != container.end(); it++ ) | |
{ | |
cout << "(" << (*it).first << ", " << (*it).second << ")" << "\t"; | |
container.erase( container.find( make_pair((*it).second, (*it).first)) ); | |
} | |
cout << endl; | |
} | |
int main() | |
{ | |
// cout << shift(123) << endl; | |
// cout << shift(987213) << endl; | |
// cout << shift(12) << endl; | |
// cout << shift(1002) << endl; | |
// cout << shift(2100) << endl; | |
// cout << shift(909098) << endl; | |
// cout << shift(989090) << endl; | |
int T, result[MAXT]; | |
cin >> T; | |
for(int ti = 0; ti < T; ti ++) | |
{ | |
int low, high; | |
cin >> low >> high; | |
set< RNumSets > allR = findRecycledNumbers(low, high); | |
assert(allR.size() % 2 == 0); | |
result[ti] = allR.size() / 2; | |
// getRecycledNumbers(allR); | |
} | |
for(int ti = 0; ti < T; ti ++) | |
{ | |
cout << "Case #" << ti + 1 << ": " << result[ti] << endl; | |
} | |
// system("PAUSE"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment