UVa : 11398 (The Base-1 Number System)



// http://uva.onlinejudge.org/external/113/11398.html
// Runtime: 0.008s
// Tag: Binary conversion, Adhoc

// @BEGIN_OF_SOURCE_CODE

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>

#define INF 2147483647
#define pi acos(-1.0)
#define N 1000000
#define LL long long

#define For(i, a, b) for( int i = (a); i < (b); i++ )
#define Fors(i, sz) for( size_t i = 0; i < sz.size (); i++ )
#define Fore(it, x) for(typeof (x.begin()) it = x.begin(); it != x.end (); it++)
#define Set(a, s) memset(a, s, sizeof (a))
#define Dump(x) for(typeof (x.begin()) it = x.begin(); it != x.end (); it++) cout << *it << " "; cout << endl;
using namespace std;

int power (int b, int p)
{
	int ret = 1;

	for ( int i = 1; i <= p; i++ ) ret *= b;

	return ret;
}

int binaryToDecimal (vector <int> &v)
{
	reverse (v.begin(), v.end());
	int ret = 0;

	Fors (i, v) {
		ret += power (2, i) * v [i];
	}

	return ret;
}

int main ()
{
	char inp [100];
	int flag;
	vector <int> v;

	while ( scanf ("%s", inp) ) {
		if (!strcmp (inp, "~")) break;
		v.clear();

		while ( strcmp (inp, "#") ) {
			int len = strlen (inp);

			if ( len == 1 ) flag = 1;
			else if ( len == 2 ) flag = 0;
			else for ( int i = 2; i < len; i++ ) v.push_back(flag);
			// Dump(v);

			scanf ("%s", inp);
		}

		printf ("%d\n", binaryToDecimal (v));
	}

	return 0;
}

// @END_OF_SOURCE_CODE
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s