我需要解决此代码的逻辑. [英] I need to solve the logic of this code..
问题描述
下面是我需要的代码,必须理解它的作用.请给我您对它的想法.
str1 ="ABCD"
str2 ="ACDS"
str3 ="KLMN"
Here is below the code that I need and have to understand what it does..Give me your ideas about it please..
str1 = "ABCD"
str2 = "ACDS"
str3 = "KLMN"
int getUnique(char str1[],char str2[],char str3[],char arr[])
{
int i,k,temp[26];
for(i = 0 ; i<26 ; temp[i]=0 , ++i);
for(i = 0 ; str1[i] ; ++i)
temp[str1[i] - 'A'] = 1;
for(i = 0 ; str2[i] ; ++i)
temp[ str2[i] - 'A'] = 1 ;
for(i = 0 ; str3[i] ; ++i)
temp[str3[i] - 'A'] = 1;
for(i = k = 0 ; i<26 ; ++i)
if(temp[i])
arr[k++] = i + 'A';
arr[k] = '\0';
return k;
}
推荐答案
for(i = 0; i< 26; temp [i] = 0,++ i);
for(i = 0 ; i<26 ; temp[i]=0 , ++i);
为temp
的每(26)个项目分配零.
Assigns zero to every (26) items of temp
.
for(i = 0; str1 [i]; ++ i)
temp [str1 [i]-``A''] = 1;
for(i = 0; str2 [i]; ++ i)
temp [str2 [i]-``A''] = 1;
for(i = 0; str3 [i]; ++ i)
temp [str3 [i]-"A"] = 1;
for(i = 0 ; str1[i] ; ++i)
temp[str1[i] - ''A''] = 1;
for(i = 0 ; str2[i] ; ++i)
temp[ str2[i] - ''A''] = 1 ;
for(i = 0 ; str3[i] ; ++i)
temp[str3[i] - ''A''] = 1;
为temp[i]
的元素分配一个索引,该元素的索引对应于所传递的字符串的字母,即
-
-
"ACDS" => temp[0]=temp[2]=temp[3]=temp[18]=1
-
...
"ABCD" => temp[0]=temp[1]=temp[2]=temp[3]=1
"ACDS" => temp[0]=temp[2]=temp[3]=temp[18]=1
...
Assigns one to the element of temp[i]
having index correpondig to the letters of the passed strings, that is
for(i = k = 0; i< 26; ++ i)
if(temp [i])
arr [k ++] = i +``A";
for(i = k = 0 ; i<26 ; ++i)
if(temp[i])
arr[k++] = i + ''A'';
仅使用与非零temp
项索引相对应的字母来构建字符串arr
.那是 if temp[i] == 1 => append character[i] to arr
(例如temp[0] == 1 => append ''A'' to arr
).
Builds the string arr
using only the letters corresponding to index of non-zero temp
items. That is if temp[i] == 1 => append character[i] to arr
(e.g temp[0] == 1 => append ''A'' to arr
).
arr [k] =``\ 0'';
return k;
arr[k] = ''\0'';
return k;
终止arr
字符串并返回arr长度.
总体结果是一个字符串,该字符串按字母顺序仅包含三个传递的字符串的每个字符的一个出现次数(因此为唯一"),即
{"ABCD"," ACDS","KLMN"} => "ABCDKLMNS"
我希望我的(大脑编译的)代码分析正确无误.
Terminates the arr
string and returns arr length.
The overall result is a string containing, in alphabetic order just one occurrence (hence ''unique'') of every character of the three passed string, that is
{"ABCD"," ACDS","KLMN"} => "ABCDKLMNS"
I hope my (brain compiled) code analysis is correct.
只需运行它!或者,您也可以自己调试代码.
Just run it! Or you can debug the code yourself.
这篇关于我需要解决此代码的逻辑.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!