输入一个待编码的字符串,字符串长度小于等于100.
输出这个编码的index
baca
16331
import java.util.Scanner; public class Main{ static final int SIZE = 4; public static void main(String[] args){ Scanner input = new Scanner(System.in); String str = input.next(); input.close(); if(str.length() > SIZE) System.out.println(-1); else System.out.println(Solution(str ,SIZE)); } public static int Solution(String s, int size) { if(size == 1) { int f = s.charAt(0) - 'a'; return f; }else if(s.length() == size){ int f = s.charAt(0) - 'a'; String str = ""; for(int i = 0 ; i < size - 1; i++) str += "y"; return f*(2 + Solution(str, size - 1)) + Solution(s.substring(1, size), size-1) + 1; }else { if(!s.equals("a")){ if(s.charAt(s.length()-1) == 'a') { return 1 + Solution(s.substring(0, s.length()-1), SIZE); }else { String str = ""; str += s.substring(0, s.length()-1); str += (char)(s.charAt(s.length()-1) - 1); for(int i = 0; i < SIZE - s.length(); i++) str += "y"; return 1 + Solution(str, SIZE); } }else { return 0; } } } }
//这是一个25进制的编码,每个字符在其位置上都是有权值的,找出权值就好,对比10进制之类的
参考最高赞
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
System.out.println(encode(str));
}
public static int encode(String str) {
if (str.equals("a")) {
return 0;
}
if (str.equals("aa")) {
return 1;
}
if (str.equals("aaa")) {
return 2;
}
if (str.equals("aaaa")) {
return 3;
}
int res = 0;
int one,two,three,four;
int mod = 25;
one = mod * mod * mod + mod * mod + mod + 1;
two = mod * mod + mod + 1;
three = mod + 1;
four = 1;
int []arr = new int[4];
arr[0] = one;
arr[1] = two;
arr[2] = three;
arr[3] = four;
int i = 1;
while (i <= str.length()) {
int cnt = Integer.parseInt(String.valueOf(str.charAt(i - 1) - 'a'));
if (i == 1) {
res += cnt * arr[i - 1];
}else {
//此处加一是因为,a = 0,aa = 1,所以除了第一位每一位都要加1
res += cnt * arr[i - 1] + 1;
}
i ++;
}
return res;
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
int count = 0;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
int w = c - 'a';
if (i == 0) {
count += w * (25*25*25 + 25*25 + 25 + 1) + 1;
} else if (i == 1) {
count += w * (25*25 + 25 + 1) + 1;
} else if (i == 2) {
count += w * (25 + 1) + 1;
} else {
count += w + 1;
}
}
System.out.println(count - 1);
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] factor = new int[] {25*25*25+25*25+25+1, 25*25+25+1, 25+1, 1};
while (in.hasNext()) {
String str = in.nextLine();
int loc = str.length() - 1;
for (int i = 0; i < str.length(); i++)
loc += (str.charAt(i) - 'a') * factor[i];
System.out.println(loc);
}
}
}