108年公務人員特種考試警察人員、一般警察人員考試及 108年特種考試交通事業鐵路人員、退除役軍人轉任公務人員考試試題
考 試 別:一般警察人員考試 等 別:三等考試
類 科 別:警察資訊管理人員 科 目:物件導向程式設計
考試時間: 2 小時 座號:
※注意: 禁止使用電子計算器。
不必抄題,作答時請將試題題號及答案依照順序寫在試卷上,於本試題上作答者,不予計分。
本科目除專門名詞或數理公式外,應使用本國文字作答。
代號:30520 頁次:3-1
一、若使用者輸入 17、18、19,請問輸出為多少?(25 分)
#include<iostream>
using namespace std;
int main(){
int n;
int i;
int ans;
int h;
while( cin >> n ) {
ans = 1;
h = n/2;
i = 2;
while( i <= h ) {
if( n%i == 0 ) {
ans = 0;
}
i = i+1;
}
if( ans == 1 )
{cout <<"Y"<<endl;}
else if (ans ==2) {cout <<"M"<<endl;}
else if (ans ==3) {cout <<"K"<<endl;}
else
{cout <<"N"<<endl;}
}
return 0;
}
代號:30520 頁次:3-2
二、請閱讀下列程式碼,並回答以下問題:
#include<iostream>
using namespace std;
int main() {
int nA =10;
int nB =50;
nA = nB;
nB = nA;
cout << "nA=" << nA << " nB=" << nB << endl;
return 0;
}
nA = ?(5 分)
nB = ?(5 分)
若 nA 與 nB 為相同數值,試問程式碼該如何修改,以達到 nA 與 nB
數值互換?(作答請用文字表達,答案不用程式碼)(15 分)
三、若使用者輸入 5,請問輸出值為何?(25 分)
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for(int i = 1;i <= n;++i) {
for(int j = 0;j < n-i;++j) cout << "/";
for(int j = 0;j < i*2-1;++j) cout << "*";
for (int j = 0 ; j < n-i ; ++j) cout << "\\";
cout << endl;
}
system("pause");
return 0;
}
代號:30520 頁次:3-3
四、
在下方程式碼中,若使用者輸入值為"520",請問輸出值為何?(25 分)#include <iostream>
using namespace std ; int main() {
int number_input , i , c ; cin >> number_input ;
cout << number_input << " = " ; i = 2 ;
while ( i <= number_input ) { c = 0 ;
while ( number_input % i == 0 ) { number_input /= i ;
++c ; }
if ( c > 0 ) { cout << i ;
if ( c > 1 ) cout << "^" << c ;
if ( number_input > 1 ) cout << " * " ; }
( i >= 3 ) ? ( i += 2 ) : ++i ; }
cout << endl ; return 0 ;
}