c语言 任意10个整数,从小到大排序,并求其正数平均值

十分感谢。。。。。。。。
是10个数中的正数

第1个回答  2010-06-23
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct TLink {
int data;
struct TLink * next;
};/*end struct TLink*/

struct TLink * new_item(int number)
{
struct TLink * r = 0;
r = (struct TLink *)malloc(sizeof(struct TLink));
r->data = number;
r->next = 0;
return r;
}/*end new_item*/

void append(struct TLink * * root, int number)
{
struct TLink * r = 0, * n = 0;
if (!root) return ;
r = *root;
if (!r) {
*root = new_item(number);
return ;
}/*end if*/
if (number < r->data ) {
n = new_item(number);
n->next = r;
*root = n;
return ;
}/*end if*/
while(r) {
n = r->next ;
if (!n) {
n = new_item(number);
r->next = n;
return ;
}/*end if*/
if (number < n->data ) {
r->next = new_item(number);
r->next->next = n;
return ;
}/*end if*/
r = n;
}/*end while*/
}/*end append*/

void print(struct TLink * root)
{
struct TLink * r = root;
printf("[");
while(r) {
printf("%d ", r->data );
r = r->next ;
}/*end while*/
printf("]\n");
}/*end print*/

int main(void)
{
int i = 0, sum = 0, x = 0; struct TLink * root = 0;
printf("请输入任意十个整数:");
for(i = 0; i < 10; i++) {
scanf("%d", &x);
append(&root, x);
sum += (x>0?x:-x);
}/*next i*/
printf("排序后这些整数顺序为:\n");
print(root);
printf("\n上述整数的绝对值平均值为%d\n", sum / 10);
return 0;
}/*end main*/

运行实例:

请输入任意十个整数:89 23 45 91 72 15 3 -88 0 2
排序后这些整数顺序为:
[-88 0 2 3 15 23 45 72 89 91 ]

上述整数的绝对值平均值为42

另外,我不知道你说的正数平均值是不是上面的绝对值平均值,如果你想挑出所有的正整数求平均值,那么请把sum +=的那句话改成如下形式:
sum += (x>0?x:0);
就可以了。
第2个回答  2010-06-23
其实可以很简单:
#include <conio.h> /* 此头函数请不要删除 */
#include <math.h>

main()
{
int i,j,p,q,s,a[10];
float jun;
printf("\n input 10 numbers:\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=0;i<10;i++){
p=i;q=a[i];
for(j=i+1;j<10;j++)
if(q<a[j]) { p=j;q=a[j]; }
if(i!=p)
{s=a[i];
a[i]=a[p];
a[p]=s; }
printf("%d ",a[i]);
}
jun=a[0];
for(i=1;i<10;i++)
jun=jun+a[i];
jun=jun/10;
printf("pingjunzhi:%f",jun);
getch(); /* 此语句请不要删除*/
}本回答被提问者采纳
第3个回答  2010-06-23
试一下c的qsort 函数
或c++的sort 函数
相似回答