Codeforces 214b hometask

Source: Internet
Author: User

In this question, a large number composed of N numbers must be a multiple of 2, 3, and 5.

Because it is a multiple of 2 and 5, the single digit is 0. If n is not 0,-1 is output directly;

The difficulty is that it must be a multiple of 3.

If a number is a multiple of three, the sum of all its numbers must be a multiple of three.

When the sum of N numbers is a multiple of 3, it can be output from large to small;

When the sum of N numbers is not a multiple of 3, if the number of N has the same number as the remainder of mod 3 and the remainder of mod 3, remove the smallest of them, otherwise, the remainder of the two modulo 3 values is not 0, and the remainder of the two modulo 3 values is the smallest. If there is no such number, output-1;

Note that when the result is 0, leading 0 cannot be output;

 

PS: I started to write a very complex code. Later I read the 60 ms algorithm of the cool guys and then I typed it again... The result is 92 ms. Kneeling orz for Daniel ~~ O (>_<) O ~~

Appendix: (the second time is a little concise code)

# Include <iostream>
# Include <cstring>
Using namespace STD;

Int main (){
Int N;
Int sign [5];
Int num [20];
Int sum;
While (CIN> N ){
Sum = 0;
Sign [0] = sign [1] = sign [2] = 100;
Memset (Num, 0, sizeof num );
For (INT I = 0; I <n; I ++ ){
Int X;
Cin> X;
Num [x] ++;
Sum + = X;
Sign [x % 3] = min (sign [x % 3], X );
}
If (Num [0] = 0) {// n does not contain 0;
Cout <-1 <Endl;
Continue;
}
If (sum % 3 ){
If (sign [Sum % 3] = 100 ){
Int temp = 2;
For (INT I = 0; I <10; I ++) {// cout <I <"" <num [I] <Endl;
If (I % 3! = 0 & num [I]) {
Num [I] --;
Temp --;
N --; // prepare for removing leading zeros from the end;
}
If (I % 3! = 0 & num [I]) {
Num [I] --;
Temp --;
N --;
}
If (temp = 0)
Break;
}
If (temp ){
Cout <-1 <Endl;
Continue;
}
}
Else num [sign [Sum % 3] --, n --;
}
If (Num [0] = N) {// if the result is 0, leading zero cannot be output;
Cout <0 <Endl;
Continue;
}
For (INT I = 9; I> = 0; I --)
While (Num [I] --)
Cout <I;
Cout <Endl;
}
Return 0;
}

 

 

 

Appendix: (complex code for the first time)

# Include <iostream>
# Include <cstring>
# Include <algorithm>

# Define maxn 100000 + 10

Using namespace STD;

Int sum, ans;
Int N;
Int sign [5];
Int A [maxn];

Int main (){
While (CIN> N ){
Sum = 0;
Sign [0] = sign [1] = sign [2] = 100;
For (INT I = 0; I <n; I ++ ){
Cin> A [I];
Sum + = A [I];
Int temp;
Temp = A [I] % 3;
If (temp ){
Sign [temp] = min (sign [temp], a [I]);
}
}
Sort (A, A + n );
If (A [0]! = 0 ){
Cout <"-1" <Endl;
Continue;
}
If (sum % 3 = 0 ){
Int flag = 0;
For (INT I = n-1; I> = 0; I --){
If (flag = 0 & A [I] = 0)
Break;
Cout <A [I];
Flag = 1;
}
If (flag = 0)
Cout <"0 ";
Cout <Endl;
Continue;
}
Else if (sum % 3 = 1 ){
If (sign [1] = 100 ){
Int temp = 2;
For (INT I = 0; I <n; I ++ ){
If (A [I] % 3 = 2 ){
A [I] = 100;
Temp --;
}
If (temp = 0)
Break;
}
If (temp ){
Cout <"-1" <Endl;
Continue;
}
Int flag = 0;
For (INT I = n-1; I> = 0; I --){
If (A [I] = 100)
Continue;
If (flag = 0 & A [I] = 0)
Break;
Cout <A [I];
Flag = 1;
}
If (flag = 0)
Cout <"0 ";
Cout <Endl;
}
Else {
Int temp = 1;
Int flag = 0;
For (INT I = n-1; I> = 0; I --){
If (A [I] = sign [1] & temp ){
Temp = 0;
Continue;
}
If (flag = 0 & A [I] = 0)
Break;
Cout <A [I];
Flag = 1;
}
If (flag = 0)
Cout <"0 ";
Cout <Endl;
}
}
Else if (sum % 3 = 2 ){
If (sign [2] = 100 ){
Int temp = 2;
For (INT I = 0; I <n; I ++ ){
If (A [I] % 3 = 1 ){
A [I] = 100;
Temp --;
}
If (temp = 0)
Break;
}
If (temp ){
Cout <"-1" <Endl;
Continue;
}
Int flag = 0;
For (INT I = n-1; I> = 0; I --){
If (A [I] = 100)
Continue;
If (flag = 0 & A [I] = 0)
Break;
Cout <A [I];
Flag = 1;
}
If (flag = 0)
Cout <"0 ";
Cout <Endl;
}
Else {
Int temp = 1;
Int flag = 0;
For (INT I = n-1; I> = 0; I --){
If (A [I] = sign [2] & temp ){
Temp = 0;
Continue;
}
If (flag = 0 & A [I] = 0)
Break;
Flag = 1;
Cout <A [I];
}
If (flag = 0)
Cout <"0 ";
Cout <Endl;
}
}
}
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.