因為數字只含有5或0,如果要被90整除的話必須含有0,否則輸出-1
如果含有0的話,就只需考慮組合的數字之和是9的倍數,只需要看最大的5的個數能否被9整數
#include <iostream> #include <vector> using namespace std; int main(){ int n; cin >> n; int numOfZero = 0, numOfFive = 0,tmp; for(int i = 0 ; i < n; ++ i){ cin>>tmp; ( tmp == 5 ) ? (++numOfFive) : (++numOfZero); } if(numOfZero == 0) cout<<-1<<endl; else{ while(numOfFive*5%9 != 0 && numOfFive-- ); for(int i = 0 ; i < numOfFive; ++i ) cout<<5; if(numOfFive == 0 ) cout<<0; else { for(int i = 0; i < numOfZero; ++ i) cout<<0; } cout<<endl; } return 0; }