poj 1107 (jobdu 1485) W's Cipher AC代码

题目链接:http://bailian.openjudge.cn/practice/1107

题目链接:http://ac.jobdu.com/problem.php?pid=1485


这道题其实不难,但做了很久,一开始题没看清,题目要求三组分别移位,一开始全局移位了。 当然还需注意的就是字符位置和值得变化,先保存每个字符的位置,然后改变值而不改变位置,最后将改变后的值映射回原来的位置。详细看代码。

#include <stdio.h>
#include <math.h>
#include <string.h>

typedef struct{
	char data;
	int loc;
}node;

int main(){
	char str[80];
	node str1[80];
	node str2[80];
	node str3[80];
	int k1,k2,k3;
	int num;
	int n1,n2,n3;

	while(scanf("%d%d%d",&k1,&k2,&k3)!=EOF && k1!=0 && k2!=0 && k3!=0){
		scanf("%s", str);
		num=strlen(str);
		n1=n2=n3=0;
// 分类
	for(int j=0;j<num;j++){
		if(('a'<= str[j])&&(str[j] <= 'i')){
			str1[n1].data=str[j];
			str1[n1].loc=j;
			n1++;
		}else if(('j'<= str[j])&&(str[j] <= 'r')){
			str2[n2].data=str[j];
			str2[n2].loc=j;
			n2++;
		}else{
			str3[n3].data=str[j];
			str3[n3].loc=j;
			n3++;
		}
	}

// 译码
	for(int j=0;j<n1;j++){
		str[str1[(j+k1)%n1].loc]=str1[j].data;
	}
	for(int j=0;j<n2;j++){
		str[str2[(j+k2)%n2].loc]=str2[j].data;
	}
	for(int j=0;j<n3;j++){
	    str[str3[(j+k3)%n3].loc]=str3[j].data;
	}
 //输出
	str[num]='\0';
	printf("%s\n", str);
	
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值