Excel Sheet

PROBLEM :
Given a positive integer, return its corresponding column title as appear in an Excel sheet.

For example:

    1 -> A
    2 -> B
    3 -> C
    ...
    26 -> Z
    27 -> AA
    28 -> AB

NOTE: The alphabets are all in uppercase.

Input:
The first line contains an integer T, depicting total number of test cases. Then following T lines contains an integer N.

Output:
Print the string corrosponding to the column number.

Constraints:
1 = T = 40
1 = N = 10000000

Example:
Input
1
51

Output
AY

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
#include <string>
#include <algorithm>
string Excel_Sheet(int ) ;
int main()
{
int t ;
long no ;
cin>>t ;
while(t--)
{
   cin>>no ;
   cout<<Excel_Sheet(no)<<endl ;
}
return 0;
}

string Excel_Sheet(int no)
{
    string str="" ;
    int rem=0;
   
    while(no>0)
    {
        rem=no%26 ;
       
        if(rem==0)
        {
            str=str+'Z' ;
            no=(no/26)-1 ;
        }
        else
        {
            char temp=rem-1+'A' ;
            str=str+temp ;
            no=no/26 ;
        }
    }
    reverse(str.begin(),str.end()) ;
    return str ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )