51Nod 1433 0和5(9的倍数理论)

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1433

思路:

数论中关于9的倍数的理论:若是一个数能被9整除,则各位数之和为9的倍数。

因为这题是90的倍数,所以至少得有一个0。

分别统计0和5的个数,9个5相加的话就是9的倍数,计算出能有几个9个5,剩下的0全排最后就可以了。

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<vector>
 6 #include<stack>
 7 #include<queue>
 8 #include<cmath>
 9 using namespace std;
10 
11 int n;
12 
13 int main()
14 {
15     //freopen("D:\input.txt","r",stdin);
16     int x;
17     while(scanf("%d",&n)!=EOF)
18     {
19         int sum_5=0,sum_0=0;
20         while(n--)
21         {
22             scanf("%d",&x);
23             if(x==5)  sum_5++;
24             else sum_0++;
25         }
26         if(sum_0==0)  {puts("-1");continue;}
27         sum_5-=sum_5%9;
28         if(sum_5)
29         {
30             while(sum_5--)
31                 printf("5");
32             while(sum_0--)
33                 printf("0");
34             printf("
");
35         }
36         else puts("0");
37     }
38     return 0;
39 }
原文地址:https://www.cnblogs.com/zyb993963526/p/6764781.html