C++,麗山高中資科教甄109,專業檢定:子字串
FB2:http://gg.gg/TedLeeMicrobitFB/
URL2:http://gg.gg/TedLeeMicrobit/
Line:ted2016.kpvs
Email:Lct4246@gmail.com
FB1:http://gg.gg/TedLeeFB/
Blog:http://gg.gg/TedLeeBlog/
URL1:http://gg.gg/TedLeeURL/
URL1:http://gg.gg/TedLeeURL/
May. 13, 2020
問題解決(Problem Solving)之逻輯思維 =
心法: 程式設計哲思(Philosophical Thinking for Programming) +
技法:程式設計工法(Skills for Programming)
子字串(substrings) 找出字串1中是否包含子字串2,子字串2的每個元素需依序出現在字串1中,例如:str1 = "AbcYmn",str2 = "AY",str1 Ê str2。
解析:
#include <iostream>
using namespace std;
int main(void) {
char str1[]={'A', 'b', 'c', 'Y', 'm', 'n'};
//char str2[]={'A', 'Y'};
char str2[]={'b', 'c', 'Y'};
int i=0, j=0;
int found=0;
for (i=0; i<3; i++) {
for (j=0; j<6; j++) { //把str2[]內的元素和str1[]相比
if (str2[i] == str1[j]) {
found++;
break;
}
}
}
if (found==3) {
cout << "Found.\n";
} else {
cout << "Not found.\n";
}
return 0;
}
using namespace std;
int main(void) {
char str1[]={'A', 'b', 'c', 'Y', 'm', 'n'};
//char str2[]={'A', 'Y'};
char str2[]={'b', 'c', 'Y'};
int i=0, j=0;
int found=0;
for (i=0; i<3; i++) {
for (j=0; j<6; j++) { //把str2[]內的元素和str1[]相比
if (str2[i] == str1[j]) {
found++;
break;
}
}
}
if (found==3) {
cout << "Found.\n";
} else {
cout << "Not found.\n";
}
return 0;
}
沒有留言:
張貼留言