1006.Do the Untwist

本文介绍了一个基于特定公式的密码转换算法实现,通过C语言代码详细展示了如何将密文转化为明文的过程,包括特殊字符的处理及按公式进行的位移转换。

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

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

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值