芯片测试(穷举)

芯片测试

测试方法: 将2片芯片(A和B)置于 测试台上,互相进行测试,测试报告是“好”或“坏”,只取其一。
假设: 好芯片的报告一定是正确的,坏芯片的报告是不确定的(可能会出错)。
要求: 设计一种测试方法,通过测试从n 片芯片中挑出 1 片好芯片。
在这里插入图片描述

#include<iostream>
#include<time.h>
using namespace std;
//---------------------
struct Node{    //因为计数值在递归里一直传不了,所以直接建立一个结点来记录算了
int time;
int len;
int L;
Node* next;
};
//---------------------------
Node* CreateList()
{
	Node *h;
	//
	h = new  Node;
	h->next=NULL;
	//
	return h;
}

//-------------------
int UseRand(int A[],int i,int j)
{
	int x1,y1;
	x1=y1=1;
	//-----------------------------------------rand()%2  up
	if (A[i]==1)
	{
		x1=(A[j]&&1);
	}
	else
		x1=rand()%2;
//-----------------------
	if (A[j]==1)
	{
		y1=(A[i]&&1);
	}
	else
		y1=rand()%2;
	//---------------------------rand()%2 down
	if((x1==1)&&(y1==1))
		return 1;
	else return 0;
}

int CompareItAll(int A[],Node* time)
{
	time->L++;
	int n=0;
	int m=0;
	int k=time->len;
	for(int i=0;i<k;i++)
	{
		if(UseRand(A,i,k-1)==1)
			n++;
		else m++;
	}
	if(n>=(k/2)) return 1;
    else if(m>=((k/2)+1)) return 0;
	else return 0;
}
//---------------------------------
int  TextIt(int A[],Node* time)
{
	time->time++;
//	int x1,y1;
	int B[100];
	int j=0;
	int k=time->len;
	time->len=0;    //赋值给k后置0重新计数
	if(k%2==0)
	{
		if(k==2)
		{
			cout<<"第"<<time->time<<"次:";
			cout<<A[0]<<endl;
			return 0;
		}
		if(k==4)
		{
			for(int i=0;i<(k/2);i++)
			{	
				if(UseRand(A,2*i,2*i+1)==1)
				{
					cout<<"第"<<time->time<<"次:";
					cout<<A[i]<<endl;     
					return i+1;
				}           
			}
		}
		for(int i=0;i<(k/2);i++)
		{
			if(UseRand(A,2*i,2*i+1)==1)
			{
				B[j++]=A[2*i]; 
				time->len++;
			}           
		}
	}

	else if(k%2!=0)
	{
		if(k==1)
		{
			cout<<A[k-1]<<endl;
			return k;
		}
		if(k>3)
		{
			int e=time->len;
			time->len=k;
			if(CompareItAll(A,time)==1)
			{
				cout<<"大于3的单数直接一轮比较得:  ";
				cout<<A[k-1]<<endl;              //
				return k;
			}
			time->len=e;
		}
		if(k==3)
		{
			for(int i=0;i<(k/2);i++)
			{
				if(UseRand(A,2*i,2*i+1)==1)
				{
					cout<<"第"<<time->time<<"次:";
					cout<<A[i]<<endl;        //
					return i+1;
				} 
				else
				{
					cout<<"第"<<time->time<<"次:";
					cout<<A[2]<<endl;        //
					return 3;
				}
			}
		}
		//------------------------
		for(int i=0;i<(k/2);i++)
		{
			if(UseRand(A,2*i,2*i+1)==1)
			{
				B[j++]=A[2*i];
				time->len++;
			}            
		}
	}
	cout<<"第"<<time->time<<"次:";
	for(int i=0;i<time->len;i++)
	{
		cout<<B[i]<<"  ";
	}
	cout<<endl;
	TextIt(B,time);                        //递归
	return 0;
}
//-----------------------------------------
int main()
{
	srand(time(NULL));
//	int A[10]={1,0,0,1,1,1,0,0,1,1};  // 1==good // 0==bad
//	int A[11]={1,0,0,1,1,1,0,0,1,1,1};
	int A[60]={1,0,0,1,1,1,0,0,1,1,0,1,0,0,1,1,1,0,0,0,0,0,0,1,1,1,1,1,
	0,1,1,1,1,0,0,1,1,1,0,0,1,1,1,1,1,1,1,1,0,0,0,1,1,0,0,1,0,1,1,1};
//	int A[58]={1,0,0,1,1,1,0,0,1,1,0,1,0,0,1,1,1,0,
//	int A[16]={0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1};
	Node *time;
	time=CreateList();
	time->len=0;
	time->time=0;
	time->L=0;
	int i=0;
	cout<<"A[]=";
	while(A[i]==0||A[i]==1)
	{
		cout<<A[i]<<"  ";
		time->len++;
		i++;
	}
	cout<<endl;
	int t=TextIt(A,time);
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值