168 Excel Sheet Column Title

Source: Internet
Author: User

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

For example:

    1, A    2, B    3, C    ...    AA, Z        

The meaning of the topic is probably like this, given a positive number, return the column headings in the Excel table
In fact, from the pest can be seen in fact is a decimal to 26 binary problem
Here's my solution:
Programme one:
Using the Loop method, each time the remainder is converted into the corresponding character form, added to the string and finally the string is reversed
1 classSolution {2  Public:3    stringConverttotitle (intN) {4           if(n<1)return "";5           Else{6           stringresult="";7           Charch;8            while(n) {9n--;TenCH = n% -+'A'; Oneresult+=ch; AN/= -; -           } - Reverse (Result.begin (), Result.end ()); the           returnresult; -           } -     } -};

It is noteworthy that the n--is one of the problems that arises when N is a multiple of 26, and if it is the case that ch=n%26+64 will appear ' @ '

The second scenario:

In a recursive way

1#include <iostream>2#include <string>3 using namespacestd;4 classSolution {5  Public:6     stringConverttotitle (intN) {7           Static strings="";8           intp=0;9           if(n) {Tenn--; Onep = n% -; AConverttotitle (n/ -); -              if(P) -s+= (Char) (p+'A'); the           } -           returns; -     } - }; + intMain () { -Solution *s =Newsolution; +    //cout<<s->converttotitle () <<endl; ACout<<s->converttotitle ( -); at     return 0; -}

Because the static keyword is used, the function has a problem with successive calls, so there is no AC, just a way of thinking.

The first option is to be AC-

168 Excel Sheet Column Title

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.