| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 752 人关注过本帖
标题:[求助]平方和排序
取消只看楼主 加入收藏
tekkie
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2006-2-7
收藏
 问题点数:0 回复次数:0 
[求助]平方和排序

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


搜索更多相关主题的帖子: 平方和 include acm int 
2006-04-11 12:31
快速回复:[求助]平方和排序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011594 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved