找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 531|回复: 4

[求助]平方和排序

[复制链接]
发表于 2009-11-3 00:54:20 | 显示全部楼层 |阅读模式 IP:江苏扬州
http://acm.zjut.edu.cn/ShowProblem.aspx?ShowID=1038

题目见上网址

我写的两个版本都WA,望高人指点
1,我用字符串处理,当平方和相等按序号排序
#include <stdio.h>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

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;
}

2我用int处理,当平方和相等按数字大小排序(我也试过按序号排序,还是不行。。)

#include <stdio.h>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

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;
}
发表于 2009-11-3 00:54:22 | 显示全部楼层 IP:江苏扬州
#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:26 | 显示全部楼层 IP:江苏扬州
怕你看不清楚,重新打一遍.先前想错了。
#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:29 | 显示全部楼层 IP:江苏扬州
楼主的连接大家看得见吗?
回复

使用道具 举报

发表于 2009-11-3 00:54:31 | 显示全部楼层 IP:江苏扬州
//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;
}
}
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-30 05:22 , Processed in 0.244245 second(s), 13 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表