7-6 字符表的操作——查找 (300分)

7-6 字符表的操作——查找 (300分)
有一字符型线性表List,假设表中无重复值,现要根据命令执行相应的查找操作。若读入的是数值1,则按位查找,查找成功时返回指定位置的值,否则输出“location error”; 若读入的是数值2,则按值查找,查找成功时返回指定值所在的位置,否则输出“not found”。

输入格式:
有多组数据,每组数据占三行,代表一次查找操作。 每组第一行为数值1或2,表示相应的命令; 第二行有两个数,第一个为表长n(0<n<50),第二个为位置i或者值x; 第三行为表的各元素值。

输出格式:
每组数据的输出占一行,具体输出如题。

输入样例:
在这里给出一组输入。例如:

1
5 4
abcde
2
5 d
abcde
输出样例:
在这里给出相应的输出。例如:

d
4

#include<iostream>
using namespace std;
#define N 50
typedef struct{
    char *elem;
    int length;
}List;
int main(){
    int k,n,x,i,j;
    char ch;
    List L;
    L.elem=new char[N];    
    while(cin>>k){
        if(k==1){
            cin>>n>>x;
            for(i=0;i<n;i++){
                cin>>L.elem[i];
            }
            if(x<=0||x>n){
                cout<<"location error"<<endl;
            }
            else{
                cout<<L.elem[x-1]<<endl;
            }
        }
        if(k==2){
            int flag=-1;
            cin>>n>>ch;
            for(i=0;i<n;i++){
                cin>>L.elem[i];
                if(L.elem[i]==ch){
                    flag=i;
                }
            }
            if(flag==-1){
                cout<<"not found"<<endl;
            }else
                cout<<(flag+1)<<endl;
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
置换密码是一种简单的加密方法,它通过将明文中的每个字母替换为另一个字母来实现加密。这个加密过程需要一个密钥,即替换。在这个替换表中,每个字母都对应着另一个字母。下面是一个使用C#实现的单置换密码加密程序: ``` using System; class Program { static void Main(string[] args) { string plaintext = "hello world"; string key = "qwertyuiopasdfghjklzxcvbnm"; // 替换 string ciphertext = Encrypt(plaintext, key); Console.WriteLine(ciphertext); string decryptedText = Decrypt(ciphertext, key); Console.WriteLine(decryptedText); } static string Encrypt(string plaintext, string key) { string ciphertext = ""; foreach (char c in plaintext) { if (Char.IsLetter(c)) { int index = Char.ToLower(c) - 'a'; if (index >= 0 && index < key.Length) { char encryptedChar = key[index]; if (Char.IsUpper(c)) encryptedChar = Char.ToUpper(encryptedChar); ciphertext += encryptedChar; } } else { ciphertext += c; } } return ciphertext; } static string Decrypt(string ciphertext, string key) { string plaintext = ""; foreach (char c in ciphertext) { if (Char.IsLetter(c)) { int index = key.IndexOf(Char.ToLower(c)); if (index >= 0 && index < 26) { char decryptedChar = (char)('a' + index); if (Char.IsUpper(c)) decryptedChar = Char.ToUpper(decryptedChar); plaintext += decryptedChar; } } else { plaintext += c; } } return plaintext; } } ``` 在这个程序中,我们定义了一个替换key,然后使用Encrypt方法将明文plaintext加密成密文ciphertext。在加密过程中,我们遍历明文的每个字符,如果它是一个字母,我们就查找它在替换表中位置,并将其替换为替换表中对应的字母。最后,我们返回加密后的密文。 在Decrypt方法中,我们将密文解密为明文。在解密过程中,我们遍历密文的每个字符,如果它是一个字母,我们就查找它在替换表中位置,并将其替换为替换表中对应的原字母。最后,我们返回解密后的明文。 请注意,这个加密方法并不安全,因为替换可以轻松地被破解。在实际应用中,我们需要使用更加安全的加密方法来保护数据的安全。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值