将一整数数列按奇数在前,偶数在后的顺序重新排列,并要求奇偶两部分分别有序的c语言程序。
2个回答
展开全部
1 #include <stdio.h>
2
3 int main(int argc, char* argv[])
4 {
5 int a[10], b[10];
6 int i, j = 0, tmp;
7
8 for (i = 0; i < 10; i++)
9 {
10 scanf("%d", &a[i]);
11 if (0 == a[i] % 2) continue;
12 tmp = a[j];
13 a[j] = a[i];
14 a[i] = tmp;
15 j++;
16 }
17
18 for (i = 0; i < 10; i++)
19 {
20 printf("%d ", a[i]);
21 }
22 printf("\n");
23 return 0;
24 }
2
3 int main(int argc, char* argv[])
4 {
5 int a[10], b[10];
6 int i, j = 0, tmp;
7
8 for (i = 0; i < 10; i++)
9 {
10 scanf("%d", &a[i]);
11 if (0 == a[i] % 2) continue;
12 tmp = a[j];
13 a[j] = a[i];
14 a[i] = tmp;
15 j++;
16 }
17
18 for (i = 0; i < 10; i++)
19 {
20 printf("%d ", a[i]);
21 }
22 printf("\n");
23 return 0;
24 }
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <iostream>
using namespace std;
//=================================================================================
double func (int a[])
{
int count = 0;
int sum = 0, i;
for (i = 0; i < sizeof(a) - 1; i++)
{
if (a[i] % 2 == 0)
{
sum += a[i];
count++;
}
}
return sum / static_cast<double>(count);
}
//=================================================================================
void main ()
{
int *a = new int[100];
int i = 0;
while (cin >> (a[i]))
{
i++;
if (cin.get() == '\n')
break;
}
cout << "The result is " << func (a) << endl;
delete[] a;
}
using namespace std;
//=================================================================================
double func (int a[])
{
int count = 0;
int sum = 0, i;
for (i = 0; i < sizeof(a) - 1; i++)
{
if (a[i] % 2 == 0)
{
sum += a[i];
count++;
}
}
return sum / static_cast<double>(count);
}
//=================================================================================
void main ()
{
int *a = new int[100];
int i = 0;
while (cin >> (a[i]))
{
i++;
if (cin.get() == '\n')
break;
}
cout << "The result is " << func (a) << endl;
delete[] a;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询