在C中对整数列表进行排序 [英] Sorting a list of integers in C

查看:108
本文介绍了在C中对整数列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一组整数,如何在C

中编写程序,使用C对这些整数进行排序,给出以下条件

a。不要使用数组

b。不要使用任何比较功能,如if / then

或switch-case

c。你只能使用指针

d。你也不能使用任何循环。

Given a set of integers, how to write a program in C
to sort these set of integers using C, given the following conditions
a. Do not use arrays
b. Do not use any comparison function like if/then
or switch-case
c. you can use pointers only
d. you cannot use any of the loops either.

推荐答案

使用qsort()


aruna写道:
Use qsort()

aruna wrote:
给定一组整数,如何在C中编写程序,使用C对这些整数进行排序,给出以下条件
a。不要使用数组
b。不要使用任何比较功能,如if / then
或switch-case
c。你只能使用指针
d。你也不能使用任何一个循环。
Given a set of integers, how to write a program in C
to sort these set of integers using C, given the following conditions
a. Do not use arrays
b. Do not use any comparison function like if/then
or switch-case
c. you can use pointers only
d. you cannot use any of the loops either.




-

从地址删除.spam以通过电子邮件回复。



--
remove .spam from address to reply by e-mail.


ar********@yahoo.co。在(aruna)写道:
ar********@yahoo.co.in (aruna) writes:
给定一组整数,如何用C语言编写一个程序,用C对这些整数进行排序,给定以下条件
不要使用数组
b。不要使用任何比较功能,如if / then
或switch-case
c。你只能使用指针
d。你不能使用任何一个循环。
Given a set of integers, how to write a program in C
to sort these set of integers using C, given the following conditions
a. Do not use arrays
b. Do not use any comparison function like if/then
or switch-case
c. you can use pointers only
d. you cannot use any of the loops either.



为什么一些大学教授继续设置这样不切实际的作业

问题!?为什么不让学生质疑这些问题的不切实际性?


__________________________________________________ _____________________________

克里斯麦克唐纳博士EMAIL:< a href =mailto:ch *** @ csse.uwa.edu.au> ch *** @ csse.uwa.edu.au

计算机科学与技术学院软件工程

西澳大利亚大学WWW: http://www.csse.uwa.edu.au/~chris

克劳利,西澳大利亚州,6009 PH:+61 8 6488 2533,传真:+61 8 6488 1089


Why do some College professors continue to set such unrealistic homework
questions!? Why don''t students question the unrealistic nature of these
questions?

__________________________________________________ _____________________________
Dr Chris McDonald EMAIL: ch***@csse.uwa.edu.au
School of Computer Science & Software Engineering
The University of Western Australia WWW: http://www.csse.uwa.edu.au/~chris
Crawley, Western Australia, 6009 PH: +61 8 6488 2533, FAX: +61 8 6488 1089


2004-04-24,James McIninch< ja ************ @ comcast.net.spam>写道:

[由AutoTopPostIncinerator X ++ Professional Edition修复的热门帖子]
On 2004-04-24, James McIninch <ja************@comcast.net.spam> wrote:
[Top posting fixed by "AutoTopPostIncinerator X++ Professional Edition" ]
aruna写道:
aruna wrote:
给定一组整数,如何用C语言编写程序来使用C对这些整数进行排序,鉴于以下条件
a。不要使用数组
b。不要使用任何比较功能,如if / then
或switch-case
c。你只能使用指针
d。你也不能使用任何一个循环。
Given a set of integers, how to write a program in C
to sort these set of integers using C, given the following conditions
a. Do not use arrays
b. Do not use any comparison function like if/then
or switch-case
c. you can use pointers only
d. you cannot use any of the loops either.



使用qsort()



Use qsort()




对,但另一块拼图如何在不使用循环的情况下吸收输入




我想我可以用这样的递归作弊:


#include< stdio.h>


int read_one_int(FILE * fp,int * i)

{

int v = fscanf(fp,"%d",i);

return(v!= 0&& v!= EOF);

}


int read_all_ints(FILE * fp,int * v,unsigned num)

{

返回num&& read_one_int(fp,v)&& read_all_ints(fp,v + 1,num-1);

}


- James



Right, but the other piece of the puzzle is how to absorb the input
without using a loop?

I suppose I could cheat with recursion like this:

#include <stdio.h>

int read_one_int(FILE *fp, int *i)
{
int v = fscanf(fp, "%d", i);
return (v != 0 && v != EOF);
}

int read_all_ints(FILE *fp, int *v, unsigned num)
{
return num && read_one_int(fp, v) && read_all_ints(fp, v+1, num-1);
}

-- James


这篇关于在C中对整数列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆