一道有趣的算法题:仿照Excel的列编号,给定一个数字,输出该列编号字符串

       By Long Luo

最近遇到一个算法题:

仿照Excel的列编号,给出一个数字,输出该列编号字符串

例如:A对应1,Z对应26,AA对应27,AZ对应52 ......

这个题目是一个典型的26进制思路去处理,但是这个题目里面有很多陷阱,在1, 26, 52等特殊情况进行考虑,经过晚上接近1个小时的编写,完成的代码如下:

C++代码如下:

#include <iostream>
#include <string.h>

using namespace std;

//函数itos:正整数到编号转换
//num:输入的正整数,pcout:输出,Max:输出控件最大长度
void itos(int num, char *pcout )
{
     char *res = new char[255];
     int m = 0, n = 0;

     while((num >= 1) && (n < 255))
    {
         m = num % 26;
          if (m != 0)
         {
             res[n] = ‘A‘ + m - 1;
         }
          else
         {
             res[n] = ‘Z‘;
              num--;
         }

          num /= 26;
         n++;
    }

     for(m = n; m > 0; m--)
    {
          pcout[n - m] = res[m - 1];
    }

     pcout[n] = ‘\0‘ ;
     delete[] res;

     return;
}

//soti:字符串到数字的转换
int stoi(char *cha)
{
     int m = 0, n = 0, i = 0, val = 0, a = 0;
     char *pc = cha ;

     while(*pc != ‘\0‘ )
    {
          //后移到个位
         pc++;
         n++;
    }

     for(i = 1; i <= n; i++)
    {
          //位循环
         pc--;
         a = i;
         m = 1;

          while(a > 1)
         {
              //位权
             m *=26;
             a--;
         }

         m *= (*pc - ‘A‘ +1);
         val += m;
    }

     return val;
}

int main()
{
     char out[255] = {0};
    printf( "out = %s\n", out);
    itos(32, out);
    printf( "out = %s\n", out);

    getchar();

     return true ;
}

Java代码如下:

package com.Algorithms.excelrow;

/*
 * @author: Long Luo
 * @Created By Frank Luo @2014.05.01
 */
public class ExcelRow {
	public static void main(String args[]) {

		System.out.println("25=" + int2Str(5) + ",28=" + int2Str(28) + ",123="
				+ int2Str(123));
		System.out.println("C=" + str2Int("C") + ",ZA" + str2Int("ZA")
				+ ",AAF=" + str2Int("AAF"));
	}

	/*
	 * @Description: covert the String to Integer.
	 */
	public static int str2Int(String input) {
		int val = 0;
		int len = input.length();
		int mul = 0;

		for (int i = len - 1, j = 0; i >= 0; i--, j++) {
			mul = 1;

			int temp = input.charAt(i) - ‘A‘ + 1;
			double weiquan = Math.pow(10, j);
			mul = (int) (temp * weiquan);
			val += mul;

			System.out.println("temp=" + temp + ",weiquan=" + weiquan + ",mul="
					+ mul + ",val=" + val);
		}

		return val;
	}

	/*
	 * @Description: covert the Integer to String.
	 */
	public static String int2Str(int rowNum) {
		StringBuffer temp = new StringBuffer(255);
		char ch;

		while (rowNum >= 1) {

			int i = rowNum % 26;
			if (i != 0) {
				ch = (char) (‘A‘ + i - 1);
				temp = temp.append(ch);
			} else {
				ch = ‘Z‘;
				temp = temp.append(ch);
				rowNum--;
			}

			System.out.println("temp=" + temp + ",ch=" + ch + ",rowNum="
					+ rowNum);
			rowNum /= 26;
		}

		return temp.reverse().toString();
	}

}

以上代码均测试通过。

如有不当错误之处,敬请批评指正,如有更好的方法,也请共同探讨, Thx:-)

Long Luo Created at PM22:25 ~ 22:40 @May 02nd, 2014 at Shenzhen, China.

独立博客链接:点击打开链接

一道有趣的算法题:仿照Excel的列编号,给定一个数字,输出该列编号字符串

时间: 2024-08-06 19:59:19

一道有趣的算法题:仿照Excel的列编号,给定一个数字,输出该列编号字符串的相关文章

一道有趣的算法题。。。

题目意思: 用1, 2, 3 ,4 ,5, 6, 7, 8, 9 组成3个三位数 abc, def 和 ghi, 每个数字恰好使用一次,要求abc:def:ghi = 1:2:3.输出所有解. 分析: 模拟所有三位数,判断条件有二: 一.i(abc):j(def):k(ghi)=1:2:3 二.判断是否出现的1~9之间的所有数字 代码: /** *一道有趣的算法题 * */ #include<iostream> #include<cstdio> using namespace st

【华为2015暑期实习生上机题】仿照Excel的列编号

这是本人上个月做的,武汉地区的上机题中的第三个,三个题目都是字符串的,共450分,两个小时内完成. 仿照Excel的列编号,给出该列编号字符串,输出一个数字. 例如:a对应1,z对应26,aa对应27,az对应52 -- #include <iostream> #include <string> using namespace std; //字符串到数字的转换,相当于26进制 int stoi(char *s) { int n=0;//字符串长度 int i=0;//循环变量 in

一道简单的算法题(三角形数阵)

今天同学给了几个算法题,看了一遍,觉得第一道题简单,试着写了一下,果然很简单. 题目是 核心代码只有 for(var i= 0; i< N; i++){ str += '<div class="item">'; for(var j= 0; j< N-i; j++){ str += '<div class="list">'+ start +'</div>'; start++; if(start > T){ star

百度的一道二面算法题分析:补全括号序列

算法题:补全括号序列 百度二面遇到的一个问题 大概意思就是 给出一个中括号序列,在序列前后可以加中括号字符,补全它... 当时没想起来解决办法,然后凉凉了,后来专门去搞了这道题,终于搞定 思路在注释里写的比较详细了,此处不再赘述(用了类似栈的思想) <?php /** * 字符串转数组 * @param $str string 输入的字符串 * @return array 转换之后的结果数组 */ function strToArray($str) { // 强制转换为字符串 $str = (s

记一道有意思的算法题Rotate Image(旋转图像)

题出自https://leetcode.com/problems/rotate-image/ 内容为: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up: Could you do this in-place? 简单的说就是给出一个n*n的二维数组,然后把这个数组进行90度顺时针旋转,而且不能使用额外的存储空间. 最初拿到这道题

一道有趣的js题以及个人的理解

var number = 2; var obj = { number : 4, fn1 : ( function() { this.number *= 2; number=number*2; var number=3; return function() { this.number *= 2; number*=3; alert(number); } } )(), db2:function(){this.number*=2} }; var fn1 = obj.fn1; alert(number);

BOBSLEDDING(一道有趣的贪心题 nyoj309)

BOBSLEDDING 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 Dr.Kong has entered a bobsled competition because he hopes his hefty weight will give his an advantage over the L meter course (2 <= L<= 1000). Dr.Kong will push off the starting line at 1 meter per

一个有趣的算法题

以下解题方法来自于他人! 题目: 一副牌,序号从1到n,每次从牌堆顶部拿出一张牌放在桌子上,并将下一张牌放到牌堆地下,依次循环,最终在桌子上的牌从1到n的有序.设计程序实现 1.模拟这个过程: 建立一个队列,将数字1到n依次入队,作为牌堆中从牌堆顶部到最后一张牌的次序.按照题意从数组中取数,比如拿出第一张牌,即从队列头部取出1放入临时数组t中,并取出数字2放进队列末尾,依次进行,直至队列为空.此时得到的数组t即为取牌的次序.根据题意,按照此顺序得到的牌的序号应该是从1到n有序的. 将t中的元素作

一道有趣的签到题

题目链接 题目描述 写一个程序,使其能输出自己的源代码. 代码中必须至少包含十个可见字符. 输入格式 输入文件为空. 输出格式 你的源代码. 从来没想过还可以这么玩φ(゜▽゜*)? 看着别人的题解写了一份代码: #include<bits/stdc++.h> #define kk(x) #x using namespace std; char s[]=kk(int main(){puts("#include<bits/stdc++.h>");puts("