:)
1
firefffffffffly 2019-06-05 14:35:18 +08:00 53
if(input == "AA") {
System.out.println("AB"); } else if(input == "AZ") { System.out.println("BA 一到输出 ZZ"); } 自己的作业自己做 |
2
pcatzj 2019-06-05 14:52:17 +08:00 via Android 3
@firefffffffffly 像一楼这么优秀的人不多了
|
3
fzy0728 2019-06-05 14:56:10 +08:00
@firefffffffffly 没毛病
|
4
xmoiduts 2019-06-05 14:56:50 +08:00 via Android
只有我是把字母变成数值,然后+1 再变回字母吗?
|
5
smilenceX 2019-06-05 14:57:19 +08:00 1
可能我阅读理解不过关,没读懂题目。
|
6
ipwx 2019-06-05 14:57:21 +08:00
一楼不要这么绝情嘛~ 我来给楼主提个醒:
AA = 0 AB = 1 ... ZZ = 26 * 26 剩下的楼主自己想。 |
7
ipwx 2019-06-05 14:57:52 +08:00
不好意思,ZZ = 26 * 26 - 1
|
8
quericy 2019-06-05 15:03:10 +08:00
一楼完美断句逻辑严谨 2333
楼主可以从 ASCII 码着手思考一下 |
9
axbx 2019-06-05 15:04:02 +08:00
题目写清楚一点吧
|
10
mirrorman 2019-06-05 15:07:59 +08:00
取模运算
|
11
mirrorman 2019-06-05 15:08:41 +08:00
相当于 26 进制加法
|
12
maninfog 2019-06-05 15:10:26 +08:00 via iPhone
@firefffffffffly 还是不严谨阿,字符串比较要用 equals 吧
|
13
passerbytiny 2019-06-05 15:14:06 +08:00
char[] chars = new char[]{A, B, ... , Z};
for (char firstChar : chars) { for (char secondChar : chars ) { System.out.println(firstChar + secondChar ); } } |
14
passerbytiny 2019-06-05 15:15:48 +08:00
擦,跑题了
|
16
hhhsuan 2019-06-05 15:29:51 +08:00
26 进制
|
17
h8743 OP |
18
youngfish 2019-06-05 15:37:08 +08:00
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); String next = scanner.next(); if (next.length() != 2) { System.out.println("输入值有误"); return; } next = new StringBuilder(next).reverse().toString(); StringBuilder result = new StringBuilder(); int lastValue = 1; for (int i = 0; i < next.length(); i++) { lastValue = isAZ(next.charAt(i), lastValue, result); } System.out.println(result.reverse().toString()); } private static int isAZ(char inputChar, int isAdd, StringBuilder result) { if (inputChar < 'A' || inputChar > 'Z') { System.out.println("输入的字符有误,请输入 A-Z"); return 0; } else { if (isAdd == 0) { result.append(inputChar); return 0; } inputChar++; if (inputChar > 'Z') { inputChar = (char) ((inputChar - 65) % 26 + 65); result.append(inputChar); return 1; } else { inputChar = (char) ((inputChar - 65) % 26 + 65); result.append(inputChar); return 0; } } } |
19
passerbytiny 2019-06-05 15:45:31 +08:00
char[] inputChars = input.toCharArray();
String output = null; if (input.matches("[A-Z][A-Y]"){ output = new StringBuilder().append(inputChars [0]).append(getNextAlphabet(inputChars [1])); } else if (input.matches("[A-Y]Z") { output = new StringBuilder().append(getNextAlphabet(inputChars [0]).append("A"); } else if (input.equals("ZZ"){ output = "到底了"; } else { output = "Invalid input!"; } char getNextAlphabet(char c){ return (char)(((int)c) + 1); } |
20
AntiGameZ 2019-06-05 15:51:06 +08:00
Leetcode 168 ? https://leetcode.com/problems/excel-sheet-column-title/
|
21
passerbytiny 2019-06-05 16:22:46 +08:00
@AntiGameZ #20 这个不是严格的 26 进制,我暂时还不知道怎么搞。
|
22
brust 2019-06-05 16:25:21 +08:00
字母版的 26 进制吗
|
23
passerbytiny 2019-06-05 16:54:26 +08:00 1
@AntiGameZ #20 这东西要用矩阵去求取,不能用 26 进制。
public class ExcelSheetColumnTitle { static Map<Integer, Character> index2titleMap = new HashMap<>(); static { index2titleMap.put(1, 'A'); index2titleMap.put(2, 'B'); index2titleMap.put(3, 'C'); index2titleMap.put(4, 'D'); index2titleMap.put(5, 'E'); index2titleMap.put(6, 'F'); index2titleMap.put(7, 'G'); index2titleMap.put(8, 'H'); index2titleMap.put(9, 'I'); index2titleMap.put(10, 'J'); index2titleMap.put(11, 'K'); index2titleMap.put(12, 'L'); index2titleMap.put(13, 'M'); index2titleMap.put(14, 'N'); index2titleMap.put(15, 'O'); index2titleMap.put(16, 'P'); index2titleMap.put(17, 'Q'); index2titleMap.put(18, 'R'); index2titleMap.put(19, 'S'); index2titleMap.put(20, 'T'); index2titleMap.put(21, 'U'); index2titleMap.put(22, 'V'); index2titleMap.put(23, 'W'); index2titleMap.put(24, 'X'); index2titleMap.put(25, 'Y'); index2titleMap.put(26, 'Z'); } public String valueToTitle(int n) { int row = n / 26; int column = n % 26; if (column == 0) { row -= 1; column = 26; } if (row == 0) { return String.valueOf(index2titleMap.get(column)); } else if (row >= 1 && row <= 26) { return new StringBuilder().append(index2titleMap.get(row)).append(index2titleMap.get(column)).toString(); } else { return null; } } } |
24
ipwx 2019-06-05 16:55:52 +08:00
|
25
msaionyc 2019-06-05 17:03:04 +08:00
@passerbytiny 老哥你这 26 行 put 方法写的不累吗?万一哪行写错了怎么办
|
26
jason19659 2019-06-05 17:10:38 +08:00
@firefffffffffly
得用 equals if( "AA".equals(input)) { System.out.println("AB"); } else if("AZ".equals(input)) { System.out.println("BA 一到输出 ZZ"); } |
27
NoKey 2019-06-05 17:12:24 +08:00 1
一楼最优秀。。。
26 楼最认真。。。 over |
28
woscaizi 2019-06-05 17:20:19 +08:00 via iPhone
转为 char 数组,直接 char +1,逢 27 进 1
|
29
yoke123 2019-06-05 17:21:31 +08:00
秀的头皮发麻 老铁 没毛病
|
30
vulgur 2019-06-05 17:39:57 +08:00
|
31
MineDog 2019-06-05 17:47:09 +08:00 via Android
应该是 2 位 27 进制自增返回才对
|
32
yumenawei 2019-06-05 18:08:08 +08:00
竟然真有老哥给答案。
|
33
MineDog 2019-06-05 18:17:24 +08:00
想错了,还是 26 进制
public String func (String str){ char[] cs = str.toCharArray(); if(cs.length!=2 || cs[0]< 'A' || cs[0] < 'Z' || cs[1]< 'A' || cs[1] > 'Z' ){ throw new IllegalArgumentException(String.format("参数错误:%s", str)); } cs[1]++; if(cs[1]>'Z'){ cs[0]++; cs[1]='A'; } if(cs[0]>'Z'){ throw new IllegalArgumentException(String.format("参数错误 : %s", str)); } return String.valueOf(cs[0]) + String.valueOf(cs[1]); } |
34
zjp 2019-06-05 18:37:53 +08:00 via Android
通用说是字典序,不过这里不用这么麻烦
|
35
zsdroid 2019-06-05 19:15:21 +08:00
<?php
//php 是世界上最好的语言 $input = 'AA'; echo ++$input;//AB $input = 'ZY'; echo ++$input;//ZZ ?> |
36
oneisall8955 2019-06-05 19:59:57 +08:00 via Android
@firefffffffffly 除了不用 equals,input 输入变量不在在后面防止空指针外,没挑出毛病了!
|
37
fmumu 2019-06-05 20:54:20 +08:00 via Android
26 进制?
|
38
small2Sun 2019-06-05 21:13:03 +08:00 via Android
@firefffffffffly 它要输出 AB..
|
39
wenzhoou 2019-06-06 09:04:44 +08:00
public static int c2i(String s) {
return s.length() == 1 ? s.charAt(0)-'A' : c2i(s.substring(0, s.length()-1))*26 + (s.charAt(s.length()-1)-'A'); } public static String i2c(int i) { return i < 26 ? ("" + (char)('A' + i)) : i2c(i/26) + i2c(i%26); } public static String padTo2Digit(String s) { return ("A" + s).substring(s.length()-1); } String input = "AA"; System.out.println(padTo2Digit(i2c(c2i(input)+1))); |
40
passerbytiny 2019-06-06 14:51:43 +08:00
|
41
Aresxue 2019-06-08 13:37:27 +08:00
26 进制,自己不能好好做作业嘛
|