continue改为break,然后j<b改为j<n,即将数组下标b的元素与数组b下标之外的其他元素进行比较,如果相同,则重复元素标志count置1,然后再break退出循环即可。
修改如下:
#include <iostream>
using namespace std;
void printA(int A[],int n){
int j,count;
for(int b=0;b<n;b++){
count=0;
// 再次遍历数组每一个数,然后将除数组下标b元素之外的元素 ,与
//数组下标b的元素进行比较,如果相同 ,则将相同标志置1,表示此数为重复数字,
//然后break退出 ,不需要再比较后面的元素
for(j=0;j<n;j++){
if(A[j]==A[b]&&j!=b){
count=1;
break;
//continue;
}else{
count=0;
}
}
if(count==0){
cout<<A[b]<<" ";
}
}
}
int main(void){
int a[100];
int y;
y=10; // 测试以10个数组元素进行测试
cout<<"请输入"<<y<<"个整数(每个数均位于0~100之间):"<<endl;
for(int i=0;i<y;i++){
cin>>a[i];
}
printA(a,y);
return 0;
}
第一道题目袋盖如下:(我只记得题目的大致意思)
一个满二叉树有n层,请你输出这个二叉树的镜像,测试用例如下:
输入
输出:
代码如何下:大概意思就是将一个二叉树的左右孩子进行交换即可。
#include<iostream>
//二叉树镜像
using namespace std;
typedef struct Node{
char data;
struct Node *Lchild;
struct Node *Rchild;
}*Tree;
void out(Tree &T){
if(T){
cout<<T->data<<" ";
out(T->Lchild);
out(T->Rchild);
}
}
二叉树的左右孩子进行交换即可
void out2(Tree &T){
if(T){
Tree temp=T->Lchild;//交换是用到的中间变量
//交换左右节点
T->Lchild=T->Rchild;
T->Rchild=temp;
//再次递归即可
out2(T->Lchild);
out2(T->Rchild);
}
}
void input(Tree &T){
char ch;
cin>>ch;
if(ch=='#'){
T=NULL;
}
else{
T=new Node;
T->data=ch;
input(T->Lchild);
input(T->Rchild);
}
}
int main(){
while(true){
Tree T;
input(T);
out(T);
cout<<"交换后"<<endl;
out2(T);
out(T);
}
return 0;
}