先把1到n中的所有奇数从小到大输出,再把所有的偶数从小到大输出 怎么编程?c语言

如题所述

若是C++语言,就可直接使用标准库中的泛型算法STL

初始化两个list listj(奇数),listo(偶数) ,先进行判断,

for(2<=n<=10000 n++)

if(n%2!=0) listj.add(n);

if(n%2==0) listo.add(n);

最后分别遍历listj和listo。

例如:

#include <stdlib.h>

#include <stdio.h>

void main()

{

int a[100]={0},b[100]={0},c;

int i=0,j=0,t,n,m;

while (i>=0)

{

if(!scanf("%d",&c))

break;

else

{

if(c%2==0)

a[i++]=c;

else

b[j++]=c;

}

}

for(m=0;m<i;m++)

{

for (t=0;t<i-m-1;t++)

{

if(a[t]>a[t+1])

{

n=a[t];

a[t]=a[t+1];

a[t+1]=n;

}

}

}

for(m=0;m<i;m++)

printf("%d\t",a[m]);

for(m=0;m<j;m++)

{

for (t=0;t<j-m-1;t++)

if(b[t]<b[t+1])

{

n=b[t];

b[t]=b[t+1];

b[t+1]=n;

}

}

printf("\n");

for(m=0;m<j;m++)

printf("%d\t",b[m]);

system("pause");

}

扩展资料:

C 语言的运算范围的大小直接决定了其优劣性。C 语言中包含了34种运算符,因此运算范围要超出许多其它语言,此外其运算结果的表达形式也十分丰富。此外,C 语言包含了字符型、指针型等多种数据结构形式,因此,更为庞大的数据结构运算它也可以应付。 

9 类控制语句和32个KEYWORDS是C语言所具有的基础特性,使得其在计算机应用程序编写中具有广泛的适用性,不仅可以适用广大编程人员的操作,提高其工作效率,同 时还能够支持高级编程,避免了语言切换的繁。

参考资料来源:百度百科-c语言

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-11-19
#include<iostream>
using namespace std;
int main()
{
int i,m,n,q,w;
cin>>w;
for(q=0;q<w;i++)
{

cin>>m;

if(m%2==0)
{
for(i=0;i<=m;i++)

if(i%2==1)
cout<<i<<" ";
cout<<endl;
for(n=0;n<=m;n++)
if(n%2==0)
cout<<n<<" ";
cout<<endl;
}
if(m%2==1) cout<<"error"<<endl;
}
return 0;
}
第2个回答  2011-11-18
若是C++语言,就可直接使用标准库中的泛型算法STL本回答被网友采纳