Thursday, February 2, 2012

Consecutive Letters

This is an answer for test case 2 given by PT. GDI Indonesia, as can be seen from its website (http://www.ptgdi.com/#Career).... The code is in C++

//
// main.cpp
// Consecutive Letters
//
// Created by Handra on 5/28/11.
// Copyright 2011 Handra. All rights reserved.
//



#include <iostream>

using namespace std ;

int main ( int argc , const char * argv [ ] )
{
char letters [ ] = "AAAAAAAAAA" ;
int number = 0 ;

do
{
if ( number == 1000 )
{
number = 0 ;

for ( int i = ( int ) strlen ( letters ) - 1 ; i >= 0 ; i -- )
{
if ( letters [ i ] != 'Z' )
{
letters [ i ] ++ ;

break ;
}
}
}

cout << letters << ( ( number < 10 ) ? "00" : ( number < 100 ) ? "0" : "" ) << number << endl ;

number ++ ;

if ( strcmp ( letters , "ZZZZZZZZZZ" ) == 0 && number == 1000 )
break ;
} while ( true) ;

return 0 ;
}

No comments:

Post a Comment