0%

APCS-2024/01/07

第一題

https://zerojudge.tw/ShowProblem?problemid=m931


提示:

$step1.$ 把兩數字存到a陣列跟b陣列,c陣列存平方和

$step2.$ c[i]=a[i]*a[i]+b[i]*b[i]

$step3.$ 找到最大的c[i]值,刪掉他

$step4.$ 再找一次最大的c[i]值,同時記錄c的位置在哪(記錄i是多少)

$step5.$ 輸出a[i]和b[i]

範例(以example1為例):

Step 1

$a_i$ 3 5 1
$b_i$ 1 2 4
total 10 29 17

Step 2

$a_i$ 3 5 1
$b_i$ 1 2 4
total 10 29 17

Step 3

$a_i$ 3 5 1
$b_i$ 1 2 4
total 10 29 17

Step 4

$a_i$ 3 5 1
$b_i$ 1 2 4
total 10 29 17

array解 AC(2ms, 356KB)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(0);cin.tie(0); //IO優化
int n,mx=0,tmp=0;
int v[3][100]; //這邊選擇開二維陣列 也可開三個一維
cin>>n;

for(int i=0;i<n;i++){
cin>>v[0][i]>>v[1][i];
v[2][i]=(v[0][i])*(v[0][i])+(v[1][i])*(v[1][i]); //計算平方和 丟到v[2][i]裡
if(v[2][i]>mx) mx=v[2][i],tmp=i;
}

v[2][tmp]=0,mx=0,tmp=0;

for(int i=0;i<n;i++){
if(v[2][i]>mx) mx=v[2][i],tmp=i;
}
cout<<v[0][tmp]<<" "<<v[1][tmp];
}

vector解 AC(2ms, 364KB)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(0);cin.tie(0);
int n,mx=0,tmp=0;
vector<vector<int>> v;
cin>>n;
v.resize(3,vector<int>(n));

for(int i=0;i<n;i++){
cin>>v[0][i]>>v[1][i];
v[2][i]=(v[0][i])*(v[0][i])+(v[1][i])*(v[1][i]);
if(v[2][i]>mx) mx=v[2][i],tmp=i;
}

v[2][tmp]=0,mx=0,tmp=0;

for(int i=0;i<n;i++){
if(v[2][i]>mx) mx=v[2][i],tmp=i;
}
cout<<v[0][tmp]<<" "<<v[1][tmp];
}

第二題

https://zerojudge.tw/ShowProblem?problemid=m932

提示:

其實真正圖形長這樣

移動路徑方向:

5 0 X
4 1
X 3 2

對照:

[x-1][y-1] [x-1][y] [x-1][y+1]
[x][y-1] [x][y] [x][y+1]
[x+1][y-1] [x+1][y] [x+1][y+1]

完整圖形:

5 0 X
4 1
X 3 2
範例(以example1為例):

INPUT

2 4 5
TyuI
ABaB
0 1 2 3 0

OUTPUT

Tyaau
4

step1:

T(step1) y u I
A B a B

step2:

T(step1) y(step2) u I
A B a B

step3:

T(step1) y(step2) u I
A B a(step3) B

step4:

T(step1) y(step2) u I
A B a(step3,step4) B
邊(step4)

step5:

T(step1) y(step2) u(step5) I
A B a(step3,step4) B

m932解答AC(2ms, 368KB)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(0);cin.tie(0); //IO優化
vector<vector<char>> v;
bool t[123]={0}; //統計用陣列 (輸出第二行時用的到)
int m,n,k,d; //m,n,k為題目敘述 d代表的是input最後一行的整數
string s;
cin>>m>>n>>k;
int nowx=m-1,nowy=0,cnt=0; //nowx,nowy記錄現在位置的x和y cnt記錄種類
v.resize(m,vector<char>(n)); //動態開記憶體
for(int i=0;i<m;i++){ //把string丟進char陣列裡
cin>>s;
for(int j=0;j<n;j++){
v[i][j]=s[j];
}
}

for(int i=0;i<k;i++){ //先判斷現在往哪個方向 再判斷if(沒超過邊界)就更新現在位置;
cin>>d;
if(d==0){
if((nowx-1)>=0) nowx--;
}
else if(d==1){
if((nowy+1)<n) nowy++;
}
else if(d==2){
if((nowx+1)<m&&(nowy+1)<n) nowx++,nowy++;
}
else if(d==3){
if((nowx+1)<m) nowx++;
}
else if(d==4){
if((nowy-1)>=0) nowy--;
}
else if(d==5){
if((nowx-1)>=0&&(nowy-1)>=0) nowx--,nowy--;
}
cout<<v[nowx][nowy];
t[(int)(v[nowx][nowy])]=1; //這邊使用ASCII 轉成int 丟到陣列
}

for(int i='A';i<='Z';i++){ //跑陣列 統計多少種類
if(t[i]==1) cnt++;
}
for(int i='a';i<='z';i++){
if(t[i]==1) cnt++;
}
cout<<"\n"<<cnt;
}

網站計數器