--- tags: [Zerojudge,解題,解題心得,TOI,202205,新手練習賽,C++] --- # i378: 垂直對稱 (Symmetry) C++解法 ### 題目 https://zerojudge.tw/ShowProblem?problemid=i378 ### 解題思路 1. 照y的大小排序 2. 如果y的大小一樣,檢查y是比對稱軸大還是小。如果大,則x大的放後面;如果小,則x大的放前面 3. 如果n%2 (奇數) , 則中間點必在對稱軸上,而其他點必兩兩對稱,或在對稱軸上;如果n為偶數,點必兩兩對稱,或在對稱軸上 ### 程式碼 ``` #include <bits/stdc++.h> using namespace std; struct points{ int x, y; } point[501]; int main() { int n, line; cin>>n; bool check; for(int i=0;i<n;i++){ cin>>point[i].x>>point[i].y; } struct points tmp; //照y排 for(int i=n;i>0;i--){ for(int j=0;j<i-1;j++){ if(point[j].y > point[j+1].y){ tmp = point[j]; point[j] = point[j+1]; point[j+1] = tmp; } } } //sort完長這樣 // cout<<"sort= "; // for(int i=0;i<n-1;i++){ // cout<<point[i].x<<" "<<point[i].y<<", "; // } // cout<<point[n-1].x<<" "<<point[n-1].y<<endl; // line = (point[0].y + point[n-1].y)/2; for(int i=n;i>0;i--){ for(int j=0;j<i-1;j++){ if(point[j].y == point[j+1].y){//跟隔壁的同y if(point[j].y < line){//比對稱軸的y大 if(point[j].x > point[j+1].x){ tmp = point[j]; point[j] = point[j+1]; point[j+1] = tmp; } } else if(point[j].y > line){ if(point[j].x < point[j+1].x){ tmp = point[j]; point[j] = point[j+1]; point[j+1] = tmp; } } } } } if(n%2 == 1){ //odd if(line != point[n/2].y) check = false; else{ for(int i=0;i<n;i++){ if((point[i].x == point[n-i-1].x) && ((point[i].y + point[n-i-1].y)/2) == line){ check = true; } else if(point[i].y == line) check = true; else{ check = false; break; } } } } else{//even for(int i=0;i<n;i++){ if((point[i].x == point[n-i-1].x) && ((point[i].y + point[n-i-1].y)/2) == line){ check = true; } else if(point[i].y == line) check = true;//如果在線上 else{ check = false; break;//繼續跑可能會洗掉 } } } if(check) cout<<"success"<<endl; else cout<<"failure"<<endl; return 0; } ``` <br> <div> <hr> <p style="margin-bottom: 0;">如果覺得這篇文章有幫助的話,免費點讚吧!( •̀ ω •́ )✧</p> <iframe width="100%" height="230px" scrolling="no" frameborder="0" id="player" src="https://button.like.co/in/embed/nataliepjlin/button/?referrer=<%- post.permalink %>" allowfullscreen="true"></iframe> </div>
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up