实现此PPT上的内容,算法要从PPT上提取;
代码
// author:seen
// thinks : instruction on string
#include <iostream>
#include <string>
using namespace std;
struct str{
string s;
}S[10];
bool trek[4];
void main()
{
string temp="11111111",s; //temp每次找第一个的数
for(int i=0;i<4;i++){ cin>>S[i].s;}
cout<<endl;
int pos = 1,count=4,flag=0; //pos用来定位查找了几次了,count用来记录数组中未被排除的还剩几个
memset(trek,true,4*sizeof(bool)); //初始化Bool数组
for(i=0;i<S[0].s.length();i++){ //超级大循环,对字符串进行查找
if(count==1){ cout<<" 结束!"<<endl;break;}
for(int j=1;j<4;j++){
if(trek[j]){
if(S[j].s[i]!=S[0].s[i]){ //按书上的说法,最后不会被排除掉的就是第一个,故用第一个和别的比即可
flag=1;
trek[j]=false;
count--;
}
}
}
if(flag==1){ //这个括号是信息打印啥的
for(int m=0;m<4;m++){
cout<<S[m].s;
if(trek[m]==false){
cout<<" ----->排除";
}
cout<<endl;
}
for(int k=0;k<=i;k++){ //操纵temp
temp[k]=S[0].s[k];
}
cout<<endl<<"R变成了"<<temp<<endl<<endl;
flag = 0;
cout<<"以上是第"<<pos<<"次比较"<<endl; //版面格式控制
cout<<"-------------------------------"<<endl;
pos++;
}
}
}