博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA代码—算法基础:计算Excel 工作表的标题列
阅读量:4041 次
发布时间:2019-05-24

本文共 831 字,大约阅读时间需要 2 分钟。

计算Excel表的标题列

问题描述

给定一个正整数,返回与其数值相应的Excel工作表的标题列

例如:

1 -> A2 -> B3 -> C...26 -> Z27 -> AA28 -> AB

问题分析

首先了解一下在JAVA中,与字符相关的特点,请看下面的代码:

for(char i='A';i<'Z';i++) {            System.out.print(i+"\t");        }        System.out.println();

这段代码的输出结果为:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

如果将字符通过类型强制转换为其对应的整数:

for(char i='A';i<='Z';i++) {            System.out.print((int)i+"\t");        }        System.out.println();

则,输出结果为:

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

即 字符 A 对应的整数值为 65;字符 B 对应的整数值为 66;字符 Z 对应的整数值为 90。

给出一段算法代码:

public static String convertToTitle(int n) {        StringBuilder result = new StringBuilder();        while(n>0){            n--;            result.insert(0, (char)('A' + n % 26));            n /= 26;        }        return result.toString();    }

(完)

转载地址:http://rgvdi.baihongyu.com/

你可能感兴趣的文章
HQL语句大全(转)
查看>>
几个常用的Javascript字符串处理函数 spilt(),join(),substring()和indexof()
查看>>
javascript传参字符串 与引号的嵌套调用
查看>>
swiper插件的的使用
查看>>
layui插件的使用
查看>>
JS牛客网编译环境的使用
查看>>
9、VUE面经
查看>>
关于进制转换的具体实现代码
查看>>
Golang 数据可视化利器 go-echarts ,实际使用
查看>>
mysql 跨机器查询,使用dblink
查看>>
mysql5.6.34 升级到mysql5.7.32
查看>>
dba 常用查询
查看>>
Oracle 异机恢复
查看>>
Oracle 12C DG 搭建(RAC-RAC/RAC-单机)
查看>>
Truncate 表之恢复
查看>>
Oracle DG failover 后恢复
查看>>
mysql 主从同步配置
查看>>
Oracle Database 12c 新特性:RAC Cluster Hub Node 和 Leaf Node
查看>>
Understanding Oracle Flex Clusters
查看>>
Oracle 12.2.0.1 新增的与Oracle数据库性能相关的功能
查看>>