1 条题解

  • 0
    @ 2021-6-15 9:42:37

    C :

    #include<stdio.h>
    #include<string.h>
    int main()
    {
    	int i,j,t,k;
    	char m[1005],c[105];
    	while(scanf("%s%s",c,m)==2)
    	{
    		t=strlen(c);
    		for(i=0;i<t;i++)
    			if(c[i]<='Z')c[i]-='A';else c[i]-='a';
    		for(i=0,j=0;i<strlen(m);i++)
    		{
    			k=m[i]-c[j++];
    			if(m[i]>='a')if(k>='a')printf("%c",k);else printf("%c",k+26);
    			else if(k>='A')printf("%c",k);else printf("%c",k+26);
    			if(j==t)j=0;
    		}
    		printf("\n");
    	}
    	return 0;
    }
    

    C++ :

    #include<iostream>
    #include<string.h>
    using namespace std;
    int main()
    {
    	char k[100]={0},c[1000]={0},m[1000]={0};
    	while(cin>>k>>c)
    	{
    		for(int i=0,j=0;i<strlen(c);i++,j++)
    		{
    			if(c[i]<='z'&&c[i]>='a'&&k[j]<='z'&&k[j]>='a')
    			{
    				m[i]=c[i]-(k[j]-97);
    				if(m[i]>122) m[i]=m[i]-26;
    				if(m[i]<97) m[i]=m[i]+26;
    			}
    			if(c[i]<='Z'&&c[i]>='A'&&k[j]<='Z'&&k[j]>='A')
    			{
    				m[i]=c[i]-(k[j]-65);
    				if(m[i]>90) m[i]=m[i]-26;
    				if(m[i]<65) m[i]=m[i]+26;
    			}
    			if(c[i]<='Z'&&c[i]>='A'&&k[j]<='z'&&k[j]>='a')
    			{
    				m[i]=(c[i]+32)-(k[j]-97)-32;
    				if(m[i]>90) m[i]=m[i]-26;
    				if(m[i]<65) m[i]=m[i]+26;
    			}
    			if(c[i]<='z'&&c[i]>='a'&&k[j]<='Z'&&k[j]>='A')
    			{
    				m[i]=c[i]+-(k[j]-97+32);
    				if(m[i]>122) m[i]=m[i]-26;
    				if(m[i]<97) m[i]=m[i]+26;
    			}
    			if(j==strlen(k)-1) j=-1;
    		}
    		cout<<m<<endl;
    	}
    }
    

    Pascal :

    program vigenere;
    var
      k,c:ansistring;
      lenk,lenc,i,t,x,y,f:longint;
      b:array[1..2000]of boolean;
      ch:char;
    begin
      readln(k);
      readln(c);
      lenk:=length(k);
      lenc:=length(c);
      fillchar(b,sizeof(b),false);
      for i:=1 to lenc do
        if c[i] in ['a'..'z'] then
          b[i]:=true;
      f:=0;
      k:=upcase(k);
      c:=upcase(c);
      for i:=1 to lenc do
      begin
        f:=f+1;
        x:=ord(c[i]);
        if f>lenk then f:=1;
        y:=ord(k[f]);
        t:=x-(y-ord('A'));
        if t<ord('A') then t:=t+26;
        ch:=chr(t);
        if b[i] then write(chr(ord(ch)+32))
          else write(ch);
      end;
      writeln;
    end.
    
    

    Java :

    import java.util.Scanner;
    
    public class Main {
    	public static void main(String[] args) {
    		Scanner scanner = new Scanner(System.in);
    		while (scanner.hasNextLine()) {
    			String key = scanner.nextLine(), encoded = scanner.nextLine();
    			System.out.println(decode(encoded, key));
    		}
    	}
    
    	public static String decode(String encoded, String key) {
    		key = key.toLowerCase();
    		String decoded = "";
    		for (int i = 0; i < encoded.length(); i++) {
    			char c = encoded.charAt(i);
    			int shift = key.charAt(i % key.length()) - 'a';
    			if (c >= 'A' && c <= 'Z') {
    				int offset = c - 'A';
    				c = (char)('A' + (offset + 26 - shift) % 26);
    			} else if (c >= 'a' && c <= 'z') {
    				int offset = c - 'a';
    				c = (char)('a' + (offset + 26 - shift) % 26);
    			}
    			decoded += c;
    		}
    		return decoded;
    	}
    }
    
    • 1

    信息

    ID
    305
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者