zoukankan      html  css  js  c++  java
  • Excel Sheet Column Title

    Excel Sheet Column Title

     Total Accepted: 17772 Total Submissions: 100508

    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 

    Credits:
    Special thanks to @ifanchu for adding this problem and creating all test cases.

    开始看到这个题目,最开始是凭直觉就往26进制方面去想:

    while(n>0)
            {
                c=(n-1)%26+1;
                n/=26;
                cstr[len++]=c+'A'-1;
            }

    但是后来发现输入26的时候会输出AZ,这是因为,本来26进制是0~25,但是,这里应该是每位1~26,用n/=26的

    方式并不能将26的低位舍弃,需要手动把低位减去,这样就可以了

    class Solution {
    public:
        string convertToTitle(int n) {
            int len=0,Count=1,c;
            char cstr[100],temp;
            while(n>0)
            {
                c=(n-1)%26+1;
                n-=c;
                n/=26;
                cstr[len++]=c+'A'-1;
            }
            for(int i=0;i<len/2;i++)
            {
                temp=cstr[i];
                cstr[i]=cstr[len-i-1];
                cstr[len-i-1]=temp;
            }
            cstr[len]='';
            return string(cstr);
        }
    };


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    uva 1584.Circular Sequence
    成为Java顶尖程序员 ,看这11本书就够了
    java 线程同步 原理 sleep和wait区别
    xargs -r
    java
    事故分析
    各大互联网公司架构演进之路汇总
    char 汉字
    nginx优化之request_time 和upstream_response_time差别
    学习进度05
  • 原文地址:https://www.cnblogs.com/Thereisnospon/p/4768498.html
Copyright © 2011-2022 走看看