首页 > 试题广场 >

假定一种编码的编码范围是a-y的25span

[问答题]
假定一种编码的编码范围是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,如:
输入:baca
输出:16331
#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;
}



发表于 2017-09-12 17:15:03 回复(8)
#include <iostream>
#include <vector>
#include <string> 

using namespace std;
vector<int> formedStr(const string &str){
vector<int> res(4);
for(int i = 0; i < str.size(); i++){
res[i] = str[i]-'a'+1;
}
return res;
}
int getVal(const string &str){
vector<int> num = formedStr(str);
int ans = 0;
ans += (num[0]-1)*(1+25+25*25+25*25*25);
ans += (num[1]==0? 0:1)*(num[1]-1)*(2+25+25*25);
ans += (num[2]==0? 0:1)*(num[2]-1)*(2+25);
ans += (num[3]==0? 0:1)*(num[3]-1)*(2);
return ans+1;
}
int main(){
string str;
while (cin >> str){
cout<< getVal(str)<< endl;
}
return 0;
1
}
发表于 2017-08-18 14:19:44 回复(13)

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);
    }
}

发表于 2018-03-23 16:57:53 回复(0)
#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;
}
发表于 2017-09-11 21:39:22 回复(0)
#include<iostream> #include <stdio.h> #include<string>
#include <vector>
using namespace std;

int main()
{
string str;
getline(cin,str); 

int nlength = str.length();
int cnt = 0;
for(int i = 0;i < nlength;i ++)
{
switch(i)
{
case 0:
cnt += (str[i]-'a')*(1+25+25*25*26);
break;
case 1:
cnt +=(str[i]-'a')*(1+25*26) + 1;
break;
case 2:
cnt +=(str[i]-'a')*(26) + 1;
break;
case 3:
cnt +=(str[i]-'a') + 1;
}
}
cout <<cnt;
int tt;
cin>>tt;
return 0;

}

发表于 2017-09-11 15:01:34 回复(0)
#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;
}

编辑于 2017-08-25 16:12:34 回复(1)
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();
    }
}

发表于 2017-08-19 13:50:56 回复(0)
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);
    }
}

发表于 2017-09-12 18:48:21 回复(1)
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;
}

发表于 2020-08-22 00:41:59 回复(0)
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);
            }
            
        }
    }
}


发表于 2020-06-22 15:56:34 回复(0)
#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());
    }
}

编辑于 2020-04-25 18:18:58 回复(0)
public class NewTest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String in = sc.next();
        sc.close();
        
        String[] strarr = {"q","w","e","r","t","y","u","i","o","p","l","k","j","h","g","f","d","s","a","x","c","v","b","n","m"};
        
        ArrayList<String> list = new ArrayList<>();
        

        for(int i=0;i<25;i++) {
            String a = strarr[i];
            list.add(a);
        }
        
        
        for(int i=0;i<25;i++) {
            for(int j=0;j<25;j++) {
                String a = strarr[i]+strarr[j];
                list.add(a);
            }
        }
        
        for(int i=0;i<25;i++) {
            for(int j=0;j<25;j++) {
                for(int k=0;k<25;k++) {
                    String a = strarr[i]+strarr[j]+strarr[k];
                    list.add(a);
                }
            }
        }
        
        
        for(int i=0;i<25;i++) {
            for(int j=0;j<25;j++) {
                for(int k=0;k<25;k++) {
                    for(int p=0;p<25;p++) {
                        String a = strarr[i]+strarr[j]+strarr[k]+strarr[p];
                        list.add(a);
                    }
                }
            }
        }
        
        String[] tmp = list.toArray(new String[list.size()]);
        Arrays.sort(tmp);
        
        //查看排序结果
        for(int i=0;i<tmp.length;i++) {
            System.out.println(tmp[i]);
        }
        
        //结果
        for(int i=0;i<tmp.length;i++) {
            if(tmp[i].equals(in)) {
                System.out.println(i);
            }
        }
        
        
    }
}




发表于 2019-10-28 12:38:26 回复(1)
J = list(input())
S = 0
for i in range(4):
    if len(J):
        S += ord(J.pop(0)) - ord('a') + 1
    if i < 3:
        S *= 26
print(S-17576)

发表于 2019-09-05 09:59:07 回复(0)
看了很久看不出数学规律,那就暴力破解吧,用数组存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;
}

发表于 2019-08-16 11:56:12 回复(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;
    }
}

编辑于 2019-06-13 07:08:25 回复(0)
26进制?
发表于 2019-04-11 03:23:45 回复(0)
#include<iostream>
#include<cmath>
using namespace std;
int main(){
    string s;
    cin>>s;
    int index = s.length() - 1;
    int zhishu = 0;
    int result[4];
    for(int i = index; i >= 0; i--){
        char num = s[i];
        int cha = (num - 'a');
        result[i] = cha;
    }
    //四位串 
    int four = result[3] + result[2] * 25 + result[1] * pow(25,2) + result[0] * pow(25,3);
    //三位串 
    int three = result[2] + result[1] * 25 + result[0] * pow(25,2);
    //两位串 
    int two = result[1] + result[0] * 25;
    //一位串 
    int one = result[0];
    cout<<(four + three + two + one)<<endl;
    return 0;
}
发表于 2019-03-09 16:34:34 回复(0)
#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;
}
编辑于 2019-03-08 23:31:25 回复(0)
站在巨人的肩膀上,题目中baca的输出应该是16331,题目给出的答案有误
https://blog.csdn.net/Shayne_S/article/details/80243068

#include <iostream>
#include <vector>
#include <string> 

using namespace std;
vector<int> formedStr(const string &str) {
    vector<int> res(4);

    for (int i = 0; i < str.size(); i++) {
        res[i] = str[i] - 'a';
    }
    return res;
}
int getVal(const string &str) {
    vector<int> num = formedStr(str);
    int ans = 0;
    ans += num[0]*(1 + 25 + 25 * 25 + 25 * 25 * 25);
    ans += num[1] *(1 + 25 + 25 * 25);
    ans += num[2] *(1 + 25);
    ans += num[3] *1;
    ans += str.size() - 1;
    return ans;
}
int main() {
    string str;
    while (cin >> str) {
        cout << getVal(str) << endl;
    }
    return 0;
}

编辑于 2019-01-26 10:36:30 回复(0)
本题有一个误区,其实baca不等于16328,而应该等于16331
附上代码,不对跳楼。仅以本题纪念明天微信面试

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);  }
}



编辑于 2018-04-11 01:21:55 回复(0)