1006.Do the Untwist

来源:互联网 发布:网络安全法逐条解读 编辑:程序博客网 时间:2024/05/16 13:43

题目大意就是根据字母的序号和给出的公式将密码翻译过来。

#include <stdio.h>#include <string.h>int main ( ){    int ciphercode[ 70 ] , plaincode[ 70 ] ;    char plaintext[ 70 ] , ciphertext[ 70 ] ;    int K ;    while ( scanf("%d",&K ) , K )    {        scanf("%s",ciphertext );        int i , n ;        n = strlen( ciphertext ) ;        for ( i = 0 ; i < n ; i ++ )        {            if ( ciphertext[ i ] == '.' )                ciphercode[ i ] = 27;            if ( ciphertext[ i ] == '_' )                ciphercode[ i ] = 0;            else if ( ciphertext[ i ] >= 'a' && ciphertext[ i ] <= 'z' )                ciphercode[ i ] = ciphertext[ i ] - 'a' + 1 ;        }        for ( i = 0 ; i < n ; i ++ )            plaincode[ ( i*K) % n ] = ( ( ciphercode[ i ] + i ) % 28 ) ;        for ( i = 0 ; i < n ; i ++ )        {            if ( plaincode[ i ] == 27 )                plaintext[ i ] = '.' ;            if ( plaincode[ i ] == 0 )                plaintext[ i ] = '_' ;            else if ( plaincode[ i ] > 0 && plaincode[ i ] < 27 )                plaintext[ i ] = plaincode[ i ] + 'a' - 1 ;        }        for ( i = 0 ; i < n ; i ++ )            printf( "%c", plaintext[ i ] );        printf("\n");    }    return 0;}

整体思路是很清晰的,在公式的转换那里一直卡壳。。。根据网上的代码改过来了。。。


原创粉丝点击