struct p
{
int xh;
int he;
};
p x[5000];
string a[5000];
bool rule (p a,p b)
{
if (a.he==b.he)
return (a.xh<b.xh);
return (a.he<b.he);
}
int main (void)
{
int i,j,n;
while (scanf("%d",&n)==1)
{
if (n==0)
break;
for (i=0;i<n;++i)
{
cin>>a[i];
x[i].xh=i;
x[i].he=0;
for (j=0;j<a[i].size();j++)
{
if (a[i][j]=='-') continue;
x[i].he+=(a[i][j]-'0')*(a[i][j]-'0');
}
}
sort(&x[0],&x[n],rule);
for (i=0;i<n-1;++i)
{
cout<<a[(x[i].xh)]<<" ";
}
cout<<a[(x[n-1].xh)]<<endl;
}
return 0;
}
struct p
{
int xh;
int he;
};
p x[10000];
bool rule (p a,p b)
{
if (a.he==b.he)
return (a.xh<b.xh);
return (a.he<b.he);
}
int main (void)
{
int i,j,n,t,k;
while (scanf("%d",&n)==1)
{
if (n==0)
break;
for (i=0;i<n;++i)
{
scanf("%d",&x[i].xh);
x[i].he=0;
k=x[i].xh;
while (k!=0)
{
t=k%10;
x[i].he+=t*t;
k/=10;
}
}
sort(&x[0],&x[n],rule);
for (i=0;i<n-1;++i)
{
printf("%d ",x[i].xh);
}
printf("%d\n",x[i].xh);
}
return 0;
}作者: 致命のdu 时间: 2009-11-3 00:54
#include<iostream>
using namespace std;
int P_add(int num)
{ int sum=0,temp;
for(i=0;i<n;i++) {temp=num[i]%10;num[i]/=10;sum+=(temp*temp);}
return sum;
}
void change(int &x,int &y){int temp;temp=x;x=y;y=temp;}
int main()
{
cout<<"Enter the dataNumber:";
cin>>n;
while(n!=0)
{
int num[n],i;
for(i=0;i<n;i++)
{cout<<"Enter a number:";cin>>num[i];sum[i]=P_add(num[i]);}
for(i=0;i<n;i++)
{ for(int t=i;t<n;t++)
{ if(sum[i]>sum[t])
{ for(;i<t;i++){change(sum[i],sum[t]);change(num[i],num[t]);}
}
}cout<<num[i]<<" ";
}
cout<<"\n\nEnter the dataNumber:";
cin>>n;
}
system("pause");
return 0;
}作者: 帅精灵 时间: 2009-11-3 00:54
怕你看不清楚,重新打一遍.先前想错了。
#include<iostream>
using namespace std;
int P_add(int num)
{
int sum=0,temp;
for(i=0;i<n;i++){temp=num[i]%10;num[i]/=10;sum+=(temp*temp);}
return sum;
}
void change(int &x,int &y){int temp;temp=x;x=y;y=temp;}
int main()
{
cout<<"Enter the dataNumber:";
cin>>n;
while(n!=0)
{
int num[n],i;
for(i=0;i<n;i++)
{cout<<"Enter a number:";cin>>num[i];sum[i]=P_add(num[i]);}
for(i=0;i<n;i++)
{ for(int t=i;t<n;t++)
{ if(sum[i]>sum[t])
{ for(;i<t;i++){change(sum[i],sum[t]);change(num[i],num[t]);}
}
}cout<<num[i]<<" ";
}
cout<<"Enter the dataNumber:";
cin>>n;
}
system("pause");
return 0;
}作者: 无形的天空 时间: 2009-11-3 00:54
楼主的连接大家看得见吗?作者: 成骏针织机械 时间: 2009-11-3 00:54
//AC了
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct apple{
string s;
int sum;
};
bool comp(const apple &x,const apple &y){
return x.sum<y.sum;
}
int main(){
for(int n; cin>>n&&n; cout<<endl){
vector<apple> va(n);
for(int i=0; i<n; i++){
apple a;
a.sum=0;
cin>>a.s;
int l=a.s.length(),temp;
for(int j=0; j<l; j++){
temp=a.s[j]-'0';
a.sum+= temp*temp;
}
va[i]=a;
}
sort(va.begin(),va.end(),comp);
for(int i=0; i<n; i++){
if(i) cout<<" ";
cout<<va[i].s;
}
}
}