注册 登录
编程论坛 C++教室

[求助]按照数组内整数的各项合排序~

六道 发布于 2007-10-16 17:32, 571 次点击

对给出的整数组,按照数组中整数的各位和的大小来排序.

#include<iostream.h>
#include<iomanip.h>

int qsort(int *array[],int len);

void main()
{
int a[]={12,32,42,51,8,16,51,21,19,9};
int b[10],s[10],*a1[10]; //*a1[]是存放a[]中每项整数的合的数组
int i,j;

for(i=0;i<10;i++)
*a1[i]=a[i]; //a1[]为地址数组,内容为a[]的整数

for(i=0;i<10;i++)
{
while(a[i]!=0)
{
b[j]=a[i]%10;
a[i]=a[i]/10;
j++;
}
for(;j>=0;--j)
s[i]+=b[j]; //s[]求出每项a[]整数的合的值
*a1[i]=s[i];
}
qsort(*a1[],10);

for(i=0;i<10;i++)
cout<<setw(5)<<a1[i];
cout<<endl;

}

int qsort(int *array[],int len)
{
int temp,*temp1;
for(int i=0;i<len-1;i++)
if(*array[i]>*array[i+1])
{
temp=*array[i];*array[i]=*array[i+1];*array[i+1]=temp; //交换值
temp1=array[i];array[i]=array[i+1];array[i+1]=temp1; //交换地址
}
return 0;
}

我的想法是:数组a[]里的每项整数作为数组s[]的地址,s[]就是数组a每项整数的各位数之合的数组.即*a1[]中,a1[]里内容与a[]一样,*a1[]内容与s[]一样,写出上面程序,调试红色处出错,搞不定了,帮忙~

另外,有朋友有其他想法的也说下,不必写代码.

4 回复
#2
HJin2007-10-16 20:28
回复:(六道)[求助]按照数组内整数的各项合排序~

/*---------------------------------------------------------------------------
File name: bccn-按照数组内整数的各项合排序.cpp
Author: HJin (email: fish_sea_bird [at] yahoo [dot] com )
Created on: 10/16/2007 05:27:32
Environment: WinXPSP2 En Pro + VS2005 v8.0.50727.762


Modification history:
===========================================================================


Problem statement:
---------------------------------------------------------------------------
https://bbs.bc-cn.net/viewthread.php?tid=178163

[求助]按照数组内整数的各项合排序~


对给出的整数组,按照数组中整数的各位和的大小来排序.

#include<iostream.h>
#include<iomanip.h>

int qsort(int *array[],int len);

void main()
{
int a[]={12,32,42,51,8,16,51,21,19,9};
int b[10],s[10],*a1[10]; //*a1[]是存放a[]中每项整数的合的数组
int i,j;

for(i=0;i<10;i++)
*a1[i]=a[i]; //a1[]为地址数组,内容为a[]的整数

for(i=0;i<10;i++)
{
while(a[i]!=0)
{
b[j]=a[i]%10;
a[i]=a[i]/10;
j++;
}
for(;j>=0;--j)
s[i]+=b[j]; //s[]求出每项a[]整数的合的值
*a1[i]=s[i];
}
qsort(*a1[],10);

for(i=0;i<10;i++)
cout<<setw(5)<<a1[i];
cout<<endl;

}

int qsort(int *array[],int len)
{
int temp,*temp1;
for(int i=0;i<len-1;i++)
if(*array[i]>*array[i+1])
{
temp=*array[i];*array[i]=*array[i+1];*array[i+1]=temp; //交换值
temp1=array[i];array[i]=array[i+1];array[i+1]=temp1; //交换地址
}
return 0;
}

我的想法是:数组a[]里的每项整数作为数组s[]的地址,s[]就是数组a每项整数的各位数之合的数组.即*a1[]中,a1[]里内容与a[]一样,*a1[]内容与s[]一样,写出上面程序,调试红色处出错,搞不定了,帮忙~

另外,有朋友有其他想法的也说下,不必写代码.

Analysis:
---------------------------------------------------------------------------

Sample output:
---------------------------------------------------------------------------
12 21 32 42 51 51 16 8 9 19
Press any key to continue . . .


Reference:
---------------------------------------------------------------------------


*/

#include <iostream>
#include <algorithm>
using namespace std;

inline int calculate(int n)
{
int r=0;

while(n)
{
r+=n%10;
n/=10;
}

return r;
}

bool predicator(const int& a, const int &b)
{
return calculate(a)<calculate(b);
}

#define DIM(x) sizeof((x)) / sizeof((x)[0])

int main()
{

int a[]={12,32,42,51,8,16,51,21,19,9};

sort(a, a+DIM(a), predicator);

copy(a, a+DIM(a), ostream_iterator<int>(cout, " "));
cout<<endl;

return 0;
}

#3
六道2007-10-16 21:54

thank you your answer.i can carefully study it.

#4
六道2007-10-16 22:39
有没有朋友能给个用指针知识解答的算法??

BZ给的方法有些地方搞不懂,初学者
#5
六道2007-10-21 15:08

楼下的朋友给个简单点的算法啊~

1