Problem B. Beer Refrigerator

http://codeforces.com/gym/241680/problem/B
比赛的时候考虑的是,它们3个尽可能接近,然后好麻烦,不如暴力枚举,这里不需要质因数分解,而是两重循环枚举所有因数,第3个因数也就随之确定

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<ctime>
 7 #include<set>
 8 #include<map>
 9 #include<stack>
10 #include<cstring>
11 #define inf 2147483647
12 #define ls rt<<1
13 #define rs rt<<1|1
14 #define lson ls,nl,mid,l,r
15 #define rson rs,mid+1,nr,l,r
16 #define N 100010
17 #define For(i,a,b) for(int i=a;i<=b;i++)
18 #define p(a) putchar(a)
19 #define g() getchar()
20 
21 using namespace std;
22 int x;
23 int a[1010];
24 int cnt;
25 int k;
26 int ans=inf;
27 int ANS[10];
28 void in(int &x){
29     int y=1;
30     char c=g();x=0;
31     while(c<'0'||c>'9'){
32         if(c=='-')y=-1;
33         c=g();
34     }
35     while(c<='9'&&c>='0'){
36         x=(x<<1)+(x<<3)+c-'0';c=g();
37     }
38     x*=y;
39 }
40 void o(int x){
41     if(x<0){
42         p('-');
43         x=-x;
44     }
45     if(x>9)o(x/10);
46     p(x%10+'0');
47 }
48 int main(){
49     freopen("beer.in","r",stdin);
50     freopen("beer.out","w",stdout);
51     in(x);
52     For(i,1,x)
53         if(x%i==0)
54             a[++cnt]=i;
55     For(i,1,cnt)
56         For(j,1,cnt){
57             k=x/a[i]/a[j];
58             if(x!=a[i]*a[j]*k)
59                 continue;
60             if(ans>a[i]*a[j]+a[i]*k+a[j]*k){
61                 ans=a[i]*a[j]+a[i]*k+a[j]*k;
62                 ANS[1]=a[i];
63                 ANS[2]=a[j];
64                 ANS[3]=k;
65             }
66         }
67     o(ANS[1]);p(' ');
68     o(ANS[2]);p(' ');
69     o(ANS[3]);
70     return 0;
71 }
View Code

 

posted @ 2019-04-05 09:59  WeiAR  阅读(108)  评论(0编辑  收藏  举报