Codeforces Round #278 (Div. 2)

A

给出一个数a, b为一个正数,如果a+b含有8,则称为吉祥的数字,找出最小的b

枚举b就可以了-

 1 #include<iostream>  

 2 #include<cstdio>  

 3 #include<cstring>  

 4 #include<algorithm>  

 5 using namespace std;

 6 

 7 int num[10]={1000000000,100000000,10000000,1000000,100000,10000,1000,100,10};

 8 

 9 int judge(long long x)

10 {

11     if(x<0)  x=-x;

12     

13     if(x/num[0]==8||x/num[1]%10==8||x/num[2]%10==8||x/num[3]%10==8||x/num[4]%10==8||x/num[5]%10==8||x/num[6]%10==8||x/num[7]%10==8||x/num[8]%10==8||x%10==8)

14     return 1;

15     return 0;

16 }

17 int main()

18 {

19     long long a,x,y,i;

20     int flag=1;

21     cin>>a;

22     for(i=1;i<=1000000000;i++)

23     {

24         x=a+i;

25         if(judge(x)||judge(y))

26         {

27             flag=1;

28             break;

29         }

30         

31     }

32     if(flag) printf("%I64d\n",i);

33     else printf("-1\n");

34 }
View Code

 B

现在给出n(0<=n<=4),a1,a2,--,an, 判断该盒子是否满足上述关系,如果满足,输出其余盒子的值

由上面的关系可以算出 a1+a4=a2+a3; a4=3*a1;

又因为题目里面说了给出的盒子的值不一定是他们的顺序,所以枚举可能地顺序,依次判断是否满足关系

  1 #include<iostream>  

  2 #include<cstdio>  

  3 #include<cstring>  

  4 #include<algorithm>  

  5 using namespace std;

  6 

  7 int a[15],b[15];

  8 int judge()

  9 {

 10     int x=a[1]+a[4]-a[2]-a[3];

 11     int y=a[4]/a[1];

 12     if(x==0&&y==3) return 1;

 13     return 0;

 14 }

 15 int check()

 16 {

 17     if(a[4]-a[3]>=0&&a[3]-a[2]>=0&&a[2]-a[1]>=0) return 1;

 18     return 0;

 19 }

 20 

 21 int main()

 22 {

 23     int i,j,n,x,y,z,flag=1;

 24     scanf("%d",&n);

 25     if(n==0)

 26     {

 27      printf("YES\n");

 28      printf("1\n1\n3\n3\n");    

 29     }

 30     if(n==1)

 31     {

 32         scanf("%d",&x);

 33         printf("YES\n");

 34         printf("%d\n%d\n%d\n",x,3*x,3*x);

 35         

 36     }

 37     if(n==2)

 38     {

 39         scanf("%d %d",&x,&y);int c=min(x,y);int d=max(x,y);

 40         x=c;y=d;

 41         for(i=1;i<=6;i++)

 42         {

 43             if(i==1)

 44             {

 45                 a[1]=x;a[2]=y;a[3]=4*x-y;a[4]=3*x;

 46             //    printf("i=%d\n",i);

 47                 if(judge()&&check()) {printf("YES\n");printf("%d\n%d\n",a[3],a[4]);flag=1;;break;}

 48                 else {flag=0;}

 49             }

 50             if(i==2)

 51             {

 52                 a[1]=x;a[3]=y;a[2]=4*x-y;a[4]=3*x;

 53             //    printf("i=%d\n",i);

 54                 if(judge()&&check()) {printf("YES\n");printf("%d\n%d\n",a[2],a[4]);flag=1;;break;}

 55                 else flag=0;

 56             }

 57             if(i==3)

 58             {

 59                 a[1]=x;a[4]=y;

 60             //    printf("i=%d\n",i);

 61                 for(j=x;j<=y;j++)

 62                 {

 63                     a[2]=j;a[3]=x+y-j;

 64                     if(judge()&&check()) {printf("YES\n");printf("%d\n%d\n",a[2],a[3]);flag=1;break;}

 65                     else flag=0;

 66                 }                

 67             }

 68             if(i==4)

 69             {

 70                 a[2]=x;a[3]=y;

 71             //    printf("i=%d\n",i);

 72                 for(j=1;j<=x;j++)

 73                 {

 74                     a[1]=j;a[4]=3*a[1];

 75                     if(judge()&&check()) {printf("YES\n");printf("%d\n%d\n",a[1],a[4]);flag=1;break;}

 76                     else flag=0;

 77                     

 78                 }

 79             }

 80             if(i==5)

 81             {

 82                 a[2]=x;a[4]=y;a[1]=y/3;a[3]=a[1]+a[4]-a[2];

 83             //    printf("-----------");

 84             //    printf("i=%d\n",i);

 85                 if(judge()&&check()) {printf("YES\n");printf("%d\n%d\n",a[1],a[3]);flag=1;break;}

 86                 else flag=0;

 87             }

 88             if(i==6)

 89             {

 90                 a[3]=x;a[4]=y;a[1]=y/3;a[2]=a[1]+a[4]-a[3];

 91             //    printf("i=%d\n",i);

 92             //    printf("flag=%d\n",flag);

 93                 if(judge()&&check()) {printf("YES\n");printf("%d %d\n",a[1],a[2]);flag=1;break;}

 94                 else flag=0;

 95             }                        

 96         }

 97         if(flag==0) {

 98                 printf("NO\n");

 99             }        

100     }

101     if(n==3)

102     {

103         scanf("%d %d %d",&b[1],&b[2],&b[3]);sort(b+1,b+4);

104         for(i=1;i<=4;i++)

105         {

106             if(i==1)

107             {

108                 a[1]=b[1];a[2]=b[2];a[3]=b[3];a[4]=3*a[1];

109             //    printf("i=%d\n",i);

110                 if(judge()&&check()) {printf("YES\n");printf("%d\n",a[4]);flag=1;break;}

111                 else flag=0;

112             }

113             if(i==2)

114             {

115                 a[1]=b[1];a[2]=b[2];a[4]=b[3];a[3]=a[1]+a[4]-a[2];

116             //    printf("i=%d\n",i);

117                 if(judge()&&check()) {printf("YES\n");printf("%d\n",a[3]);flag=1;break;}

118                 else flag=0;            

119             }

120             if(i==3)

121             {

122                 a[1]=b[1];a[3]=b[2];a[4]=b[3];a[2]=a[1]+a[4]-a[3];

123                 if(judge()&&check()) {printf("YES\n");printf("%d\n",a[2]);flag=1;break;}

124                 else flag=0;                

125             }

126             if(i==4)

127             {

128                 a[2]=b[1];a[3]=b[2];a[4]=b[3];a[1]=b[3]/3;

129                 if(judge()&&check()) {printf("YES\n");printf("%d\n",a[1]);flag=1;break;}

130                 else flag=0;

131             }

132             

133         }

134             if(flag==0) {

135                 printf("NO\n");

136             }    

137     }

138     if(n==4)

139     {

140         scanf("%d %d %d %d",&a[1],&a[2],&a[3],&a[4]);

141         sort(a+1,a+5);

142         if(judge()&&check()) printf("YES\n");

143         else printf("NO\n");

144     }

145     return 0;

146 }
View Code

 

 

 

话说这一题= =是既写得挫= =还改得久= =啊 啊 啊

你可能感兴趣的:(codeforces)