#include <iostream>
#include <string>
#include <math.h>
using namespace std;
int main(){
string s;
while(cin>>s){
int len=s.length();
int index=0;
for(int i=0; i<len; i++,index++){
int n=s[i]-'a';
for(int j=0; j<4-i; j++)
index+=n*pow(25,j);
}
cout<<index-1<<endl;
}
return 0;
}
| 1 | } |
import java.util.*;
public class DicNew {
public static int number(String str) {
int add = str.length()-1;//要考虑到字符串的长度
int mul1 = 1 + 25 + 25*25 + 25*25*25;//从左往右,第一位(进位所需要乘以的数值)
int mul2 = 1 + 25 + 25*25;//第二位
int mul3 = 1 + 25;//第三位
int mul4 = 1;//第四位
int[] mul = new int[4];//把上面的四个数放到数组里,以便在下面的循环中使用
mul[0] = mul1;
mul[1] = mul2;
mul[2] = mul3;
mul[3] = mul4;
char[] ch = str.toCharArray();
int result = 0;
for(int i = 0;i < ch.length;i++) {
result += (ch[i] - 'a')*mul[i];
}
result += add;//最后把长度考虑进去,不然会出现错误,比如a和aa都会返回0
return result;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.next();
scanner.close();
int result = number(str);
System.out.println(result);
}
}
#include<stdio.h>int main(){char word[4]={0,0,0,0};scanf("%s",word);int carry[4]={25+25*25+25*25*25,//第一个字母进位所乘的比例25+25*25,//第二个字母进位所乘的比例25,//第三个字母进位所乘比例1,//第四个字母进位所乘比例};int answer=0;if(word[0]-'a'>=0&&word[0]-'z'<0{answer=(word[0]-'a')*carry[0];if(word[1]-'a'>=0&&word[1]-'z'<0{answer=(word[1]-'a')*carry[1]+1;if(word[2]-'a'>=0&&word[2]-'z'<0{answer=(word[2]-'a')*carry[2]+1;if(word[3]-'a'>=0&&word[2]-'z'<0{answer=(word[3]-'a')*carry[3]+1;}}}}printf("%d",answer);return answer;}
#include<iostream> #include <stdio.h> #include<string> #include<stdio.h>
int main(){
char word0[4] = {
0, 0, 0, 0
};
scanf("%s", word0);
int carry[4] = {
1+25+25*25+25*25*25, //此为第一个字母“进位”所需数值;
1+25+25*25, //此为第二个字母“进位”所需数值;
1+25, //此为第三个字母“进位”所需数值;
1
};
//eg: index(a)-index(b) = carry[0]
//eg: index(aa)-index(ab) = carry[1];
//eg: index(abc)-index(abd) = carry[2];.....
int ans =0;
if(word0[0]-'a'>=0 && word0[0]-'z'<0){
ans = (word0[0] - 'a')*carry[0];
if(word0[1]-'a'>=0 && word0[1]-'z'<0){
ans += (word0[1] - 'a')*carry[1] + 1;
if(word0[2]-'a'>=0 && word0[2]-'z'<0){
ans += (word0[2] -'a')*carry[2] +1;
if(word0[3]-'a'>=0 && word0[4]-'z'<0){
ans += (word0[3] - 'a')*carry[3] +1;
}
}
}
}
printf("%d", ans);
return ans;
} public class LexicographicalCode {
/**
* 假定一种编码的编码范围是a ~ y的25个字母,从1位到4位的编码,
* 如果我们把该编码按字典序排序,形成一个数组如下:
* a, aa, aaa, aaaa, aaab, aaac, … …, b, ba, baa, baaa, baab, baac … …, yyyw, yyyx, yyyy
* 其中a的Index为0,aa的Index为1,aaa的Index为2,
* 以此类推。 编写一个函数,输入是任意一个编码,
* 输出这个编码对应的Index.
*
* @param args
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String i = scanner.next();
System.out.println(codeIndex(i));
}
/**
* 思路:排列组合的思想<br>
* a和b之间 有25+25*25+25*25*25个数 (25:aa,ab……ay),<br>
* (25*25: aaa,aab……aba……ayy);(25*25*25: aaaa,……ayyy)<br>
* 所以b的位置是 a+ 25+25*25+25*25*25+1<br>
* 以此类推:ab = aa +25+25*25 +1 <br>
* aab = aaa + 25 + 1<br>
* aaab + aaaa + 1;<br>
* 然后索引从a, aa,aaa开始 0,1,2,可以认为是长度-1<br>
* @param code
* @return
*/
public static int codeIndex(String code) {
int factor[] = {1 + 25 + 25 * 25 + 25 * 25 * 25, 1 + 25 + 25 * 25, 1 + 25, 1};
char[] codeArray = code.toCharArray();
int index = 0;
int len = 0;
for (int i = 0; i < codeArray.length; i++) {
index += factor[len++] * (codeArray[i] - 'a');
}
return index + (len - 1);
}
public static String deCode(int index) {
int factor[] = {1 + 25 + 25 * 25 + 25 * 25 * 25, 1 + 25 + 25 * 25, 1 + 25, 1};
StringBuilder stringBuilder = new StringBuilder();
int i = 0;
while (index > 0) {
stringBuilder.append((char) ('a' + index / factor[i]));
index %= factor[i++];
index--;
}
return stringBuilder.toString();
}
} import java.util.Scanner;
public class Index {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
getIndex(in.next());
}
public static void getIndex(String s){
double index = 0;
double[] a = {1+25+Math.pow(25, 2)+Math.pow(25, 3),1+25+Math.pow(25, 2),1+25,1};
for(int i=0;i<s.length();i++){
index = index + a[i]*(s.charAt(i)-'a');
}
System.out.println((int)index);
}
} int fun (string str) {
if (str.size() > 4 || str.size() == 0) {
return -1;
}
int arr[4];
int arr[0] = 1 + 25 + 25 * 25 + 25 * 25 * 25;
int arr[1] = 1 + 25 + 25 * 25;
int arr[2] = 1 + 25;
int arr[3] = 1;
int res = 0;
for (int i = 0; i < str.size(); i++) {
res += (str[i].c_str() - 'a') * arr[i];
++res;
}
return res;
} import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] base = new int[]{25 * 25 * 25 + 25 * 25 + 25 + 1,
25 * 25 + 25 + 1,
25 + 1,
1};
while(sc.hasNext()) {
String str = sc.nextLine();
int len = str.length();
if(len == 1){
int idx = str.charAt(0) - 'a') * base[0] + 1;
System.out.println(idx);
}
else if(len == 2) {
int idx = (str.charAt(0) - 'a') * base[0] +
(str.charAt(1) - 'a') * base[1] + 1;
System.out.println(idx);
}
else if(len == 3) {
int idx = (str.charAt(0) - 'a') * base[0] +
(str.charAt(1) - 'a') * base[1] +
(str.charAt(2) - 'a') * base[2] + 1;
System.out.println(idx);
}
else if(len == 4) {
int idx = (str.charAt(0) - 'a') * base[0] +
(str.charAt(1) - 'a') * base[1] +
(str.charAt(2) - 'a') * base[2] +
(str.charAt(3) - 'a') * base[3] + 1;
System.out.println(idx);
}
}
}
}
#include <iostream>
#include <cmath>
using namespace std;
/***
* 输入字符串和字符串长度,返回编码位置
* @param s
* @param len
* @return
*/
int index(const char* s, int len){
int ans = 0;
for (int j = 0; j < len; ++j) { // for each letter
int summary = 0;
for (int k = 0; k < 4 - j; ++k) {
summary += pow(25, k);
}
ans += (s[j] - 'a') * summary;
if(j > 0)
ans += 1;
}
return ans;
}
int main(){
string s;
while(cin>>s){
cout<<index(s.c_str(), s.length());
}
} 看了很久看不出数学规律,那就暴力破解吧,用数组存4个数字,先存为-1,与原数字比较
#include <iostream>
#include <string>
using namespace std;
bool eq(int *a,int *b){
for(int x=0;x<4;x++){
if(a[x]!=b[x])
return false;
}
return true;
}
int isLessThanZero(int *a){
for(int x=0;x<4;x++){
if(a[x]==-1)
return x;
}
return -1;
}
void clear(int *a,int b){
for(int x=b;x<4;x++){
a[x]=-1;
}
}
void up(int *a){
for(int x=3;x>=0;x--){
if(a[x]>=25){
clear(a,x);
a[x-1]++;
}
}
}
int main()
{
//97
string a;
cin>>a;
a+="```";
int arrAnswer[4]={(int)a.at(0)-97,(int)a.at(1)-97,(int)a.at(2)-97,(int)a.at(3)-97};
int arr[4]={-1,-1,-1,-1};
int i=0;
while(true){
int index=isLessThanZero(arr);
if(index!=-1){
arr[index]=0;
}
else{
arr[3]++;
up(arr);
}
if(eq(arr,arrAnswer)){
break;
}
i++;
}
cout<<i<<endl;
return 0;
}
#include<iostream>
#include<math.h>
int main(int argc,char* argv[])
{
std::string s;
while(std::cin>>s)
{
int len=s.length();
int temp=0;
for(int i=0;i<len;i++)
{
int temp2=s[i]-'a';
for(int j=3-i;j>=0;j--)
{
temp+=temp2*pow(25,j);
}
temp+=1;//注意要加1
}
std::cout<<temp-1<<std::endl;
}
} #include<iostream>
using namespace std;
int main() {
int pow[4]{1, 26, 625 + 25 + 1, 625 * 25 + 625 + 25 + 1};
string a;
int res = 0, i = 0, j = 3;
cin >> a;
while (i < a.length())res += (a[i++] - 'a') * pow[j--];
res = (res == 0 ? (int) a.length() - 1 : res + 3);
cout << res << endl;
return 0;
}
站在巨人的肩膀上,题目中baca的输出应该是16331,题目给出的答案有误 https://blog.csdn.net/Shayne_S/article/details/80243068#include <iostream>
public class Test { public static void main (String [] arge) { String s = "b"; int len = s.length(); double index = 0; double[] a = {1+25+Math.pow(25, 2)+Math.pow(25, 3),1+25+Math.pow(25, 2),1+25,1}; for(int i=0;i<len;i++){ index += a[i]*(s.charAt(i)-'a'); } System.out.println((int)index+len-1); } }