总结:写代码时间短了真的bug特多
思路:定义一个方法 其实是我觉得可以定义好多方法!
大体思路:先找str1 and str2相同的
找到一个就检查是否在str1 str2中都唯一
是就result++;
然后:或者先把两个str唯一的char先选出来,选择先做,这样的话肯定会快一些!但是不想写了,而且相对也麻烦一些,然后再比较!
题目:
#include "stdio.h"
//
/fun 输入两字符串
/ 输出相同且唯一的字符串
//
int char_same_one(char str1[], char str2[])
{
int str1_num = 0;
int str2_num = 0;
char temp;
int count1;
int count2;
int result = 0;
for(int i = 0;; ++i)
{
if(str1[i] == '\0') break;
str1_num++;
}
for(int i = 0;; ++i)
{
if(str2[i] == '\0') break;
str2_num++;
}
printf("str1Long:%d \nstr2Long:%d\n",str1_num,str2_num); //Test长度
for(int i = 0; i < str1_num; ++i)
{
if( str1[i] == ' ') continue;
for(int j = 0; j < str2_num; ++j)
{
if(str1[i] == str2[j])
{
count1 = count2 =0;
// printf("\nsss count1: %d \n count2: %d",count1,count2); //Test 置0
temp = str1[i];
for(int s1 = 0; s1 < str1_num; ++s1)
{
if(temp == str1[s1])
{
count1++;
}
}
for(int s2 = 0; s2 < str2_num; ++s2)
{
if(temp == str2[s2])
{
count2++;
}
}
// printf("\n count1: %d \n count2: %d",count1,count2); //Test 同时候count
if( 1 == count1 && 1 == count2 )
{
// printf("\n %c \n\n",temp);
result++;
}else continue;
}
else continue;
}
}
return result;
}
int main(){
int result;
char a[80],b[80];
// result = char_same_one("abc*defghijklmnos","a*scde");
// printf("\n%dsss",result);
while(1)
{
printf("Input1 ");
scanf("%s",a);
printf("Input2 ");
scanf("%s",b);
result = char_same_one(a,b);
printf("\n----------满足条件num:%d---------------------\n",result);
}
return 0;
}