#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef unsigned long long int ulli;
struct val_info{
ulli value;
int place;
};
bool greatermark(const val_info& s1,const val_info& s2) //使用sort算法将val_info以value进行排序
{
return s1.value >= s2.value;
}
int place(ulli val){
if(val>=1&&val<=9) return 1;
if(val>=10&&val<=99) return 2;
if(val>=100&&val<=999) return 3;
if(val>=1000&&val<=9999) return 4;
if(val>=10000&&val<=99999) return 5;
if(val>=100000&&val<=999999) return 6;
if(val>=1000000&&val<=9999999) return 7;
if(val>=10000000&&val<=99999999) return 8;
}
ulli ten(int n){
ulli val=1;
for(int i=0;i<n;i++)
val=10*val;
return val;
}
ulli fill_in(ulli a,ulli b){ //补位,将每个元素扩充与最大元素位数相同,例如897,8765432,89,89654
if(a==b) return a; //补位后为8978978 ,8765432,8989898,8965489,将补位后数进行排序,排序后再根据位信息恢复为
int aw=place(a); //原始数字,最后按照原始数字顺序,组合即为最大数
int bw=place(b);
int differ_w=bw-aw;
int result=0;
int flag=0;
while(flag!=1){
if(differ_w>=0)
result+=a*ten(differ_w);
else result+=a/ten(-differ_w);
if(differ_w<=0) flag=1;
differ_w=differ_w-aw;
}
return result;
}
ulli recover(val_info a,int max_place ){ //根据位信息恢复为原始数字
if((max_place-a.place)== 0) return a.value;
else return a.value/ten(max_place-a.place);
}
int main(){
int cin_value;
int max_place;
int total_place=0;
int max_value=0;
vector<val_info> iivec;
cin>>cin_value;
max_place=0;
while(cin_value!=0){
if(max_place<=place(cin_value)) max_place=place(cin_value);
if(max_value<=cin_value) max_value=cin_value;
total_place+=place(cin_value);
val_info val_pla={cin_value,place(cin_value)};
iivec.push_back(val_pla);
cin>>cin_value;
}
int count=iivec.size();
cout<<max_place<<"\n";
for(int i=0;i<count;i++){ //例,输入878,88,8789,9
iivec[i].value=fill_in(iivec[i].value,max_value); //补齐4位后为 8788, 8888,8789,9999
cout<<iivec[i].value<<endl; //补位后排序为9999,8888,8789,8788
//恢复9,88,8789,878,最大组合数字即为9888789878
}
sort(iivec.begin(), iivec.end(),greatermark);
for(int i=0;i<count;i++){
iivec[i].value=recover(iivec[i],max_place);
cout<<iivec[i].value<<" "<<iivec[i].place<<endl;
}
ulli result=0;
int high_place=0;
for(int i=0;i<count;i++){
int differ=max_place-iivec[i].place;
high_place+=iivec[i].place;
result+=iivec[i].value*ten(total_place-high_place);
}
cout<<result;
return 0;
}
网易实习,任意n个数组合求最大
最新推荐文章于 2022-03-16 17:26:53 发布