分類  >  編程 >

一路有趣的演算法題:仿照Excel的列編號,給定一個數字,輸出該列編號字元串

tags:    時間:2014-05-04 18:39:42
一道有趣的演算法題:仿照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.


獨立博客鏈接:點擊打開鏈接










推薦閱讀文章

Bookmark the permalink ,來源:互聯網