Result: Accepted
Time: 874ms
Memory: 13436kB
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct num{
char N[6];
int digitroot;
}a[1000020];
bool CMP(const num &x,const num &y){
if(x.digitroot==y.digitroot){
if(strcmp(x.N,y.N)<0){
return true;
}
else return false;
}
return x.digitroot<y.digitroot;
}
int cal_digitroot(int n){
int sum=0;
while(n){
sum+=n%10;
n/=10;
}
return sum;
}
void init(){
for(int i=0;i<=999999;i++){
a[i].digitroot=cal_digitroot(i);
a[i].N[0]=char(i/100000+'0');
a[i].N[1]=char((i%100000)/10000+'0');
a[i].N[2]=char((i%10000)/1000+'0');
a[i].N[3]=char((i%1000)/100+'0');
a[i].N[4]=char((i%100)/10+'0');
a[i].N[5]=char(i%10+'0');
}
}
int main(){
init();
sort(a,a+1+999999,CMP);
for(int i=0;i<=999999;i++){
printf("%s\n",a[i].N);
}
}