C. Polygon for the Angle
You are given an angle angang.
The Jury asks You to find such regular nn-gon (regular polygon with nn vertices) that it has three vertices aa, bb and cc (they can be non-consecutive) with ∠abc=ang∠abc=ang or report that there is no such nn-gon.
If there are several answers, print the minimal one. It is guarantied that if answer exists then it doesn't exceed 998244353998244353.
Input
The first line contains single integer TT (1≤T≤1801≤T≤180) — the number of queries.
Each of the next TT lines contains one integer angang (1≤ang<1801≤ang<180) — the angle measured in degrees.
Output
For each query print single integer nn (3≤n≤9982443533≤n≤998244353) — minimal possible number of vertices in the regular nn-gon or −1−1 if there is no such nn.
思路:模擬:最少三個點才能有角度,所以從3開始模擬
#include <bits/stdc++.h> #define ll long long #define INF 0x3f3f3f3f #define mod 998244353 using namespace std; double n; int main(){ int t; cin>>t; while(t--){ cin>>n;int i,f=0; for(i=3;i<400;i++){ double du=360.0/i; for(int j=1;j<i-1;j++){//i-1表示若分為i份,最多i-2個組成角度 int a=(int)j*du/2; if(a==n){ f=1; break; } } if(f)break; } cout<<i<<endl; } return 0; }
使用gcd,求出和180的最大公約數,
#include<bits/stdc++.h>
using namespace std;
int T,n,ans;
int main(){
cin>>T;
while(T--){
cin>>n;
int zz=__gcd(n,180);
zz=180/zz;
while(180*(zz-2)/zz<n) zz*=2;
cout<<zz<<endl;
}
return 0;
}