C/ C++,科園實中資科教甄109,B7
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.7, 2020
問題解決(Problem Solving)之逻輯思維 =
心法: 程式設計哲思(Philosophical Thinking for Programming) +
技法:程式設計工法(Skills for Programming)
7. 下列程式碼執行後的輸出結果為(7)
解析:
#include <iostream>
#include <iomanip>
using namespace std;
#define ARY_LENGTH 10
void print_array(int fri[]) {
int i = 0;
cout <<fri[] = ";
for (i = 0; i < ARY_LENGTH; i++) {
cout << fri[i] << " ";
}
cout << endl;
}
void find(int fri[], int n) {
int group=0;
int i;
for (i = 0; i < n; ++i) { //整個陣列掃一輪
if (fri[i] != -1) { //開追完串列
++group; //串列數
int j = i;
while (fri[j] != -1) { //根据當前索引位置的陣列內容跳至那個item
int next = fri[j];
fri[j] = -1;
j = next;
print_array(fri);
}
}
}
cout << group << endl;
}
int main(void) {
int fri[10]={4, 7, 2, 9, 6, 0, 8, 1, 5, 3};
print_array(fri);
find(fri, 10);
return 0;
}
#include <iomanip>
using namespace std;
#define ARY_LENGTH 10
void print_array(int fri[]) {
int i = 0;
cout <<fri[] = ";
for (i = 0; i < ARY_LENGTH; i++) {
cout << fri[i] << " ";
}
cout << endl;
}
void find(int fri[], int n) {
int group=0;
int i;
for (i = 0; i < n; ++i) { //整個陣列掃一輪
if (fri[i] != -1) { //開追完串列
++group; //串列數
int j = i;
while (fri[j] != -1) { //根据當前索引位置的陣列內容跳至那個item
int next = fri[j];
fri[j] = -1;
j = next;
print_array(fri);
}
}
}
cout << group << endl;
}
int main(void) {
int fri[10]={4, 7, 2, 9, 6, 0, 8, 1, 5, 3};
print_array(fri);
find(fri, 10);
return 0;
}
沒有留言:
張貼留言