编写程序实现线性移位寄存器

题目:

【输入形式】

输入为3行:

第1行:线性移位寄存器的阶数n;

第2行:反馈函数的系数c1,c2,...,cn;

第3行:初始状态a0,a1,...,an-1。

【输出形式】

输出为2行:

第1行:为周期p;

第2行:输出序列(输出n+p位)。

【样例输入】

5

10010

10011

【样例输出】

31

100110100100001010111011000111110011

程序代码:

#include <iostream>
#include <string.h>
using namespace std;
char c[100];
char a[100];	
char b[100];
char a1[100];
int n,x;

void yiwei(int n){
	int i;
	for(i=1;i<n;i++){
		a1[i-1]=a1[i];
	}
}

int yihuo(int x){
	int i;
	char temp;
	temp=a1[b[0]];
	for(i=1;i<=x;i++){
		temp=((temp-48)^(a1[b[i]]-48))+48;
	}
	return temp;
}

int zhouqi(){
	int t;
	t=strcmp(a,a1);
	
	if(t==0){
		return 0;	
	}else{
		return 1;
	}
	
}

int main(){
	
	int n;
	cin>>n;
	
	char out[100];
	char temp;
	int x=-1;
	int i;	
	int p;
	
	cin>>c;
	cin>>a;
	
	for(i=0;i<n;i++){
		if(c[i]=='1') {
			x++;
			b[x]=i;
		}
	}
	
	out[0]=a[0];
	int j=0;
	for(i=1;i<n;i++){
		a1[i-1]=a[i];
	}
	
	temp=a[b[0]];
	for(i=1;i<=x;i++){
		temp=((temp-48)^((a[b[i]]-48)))+48;
	}
	a1[n-1]=temp;
	
	while(zhouqi()){		
		j++;
		out[j]=a1[0];
		temp=yihuo(x);
		yiwei(n);
		a1[n-1]=temp;		
	}
	
	p=j+1;
	cout<<p<<endl;
		
	for(i=0;i<p+n;i++){
		cout<<out[i%p];
	}
	
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值