题目内容:
接受若干非负整数(数据不重复),当个数超过10个或者遇到负数时停止接受,将这几个正整数按升序排列输出,并且奇数在前,偶数在后。
输出要求,每个数字后输出空格与其他数字隔开,最后一个数字后也有空格
输入样例1:
10 9 8 7 6 5 4 3 2 1
输出样例1:
1 3 5 7 9 2 4 6 8 10 回车
输入样例2:
2 3 4 5 -1
输出样例2:
3 5 2 4 回车
时间限制:500ms内存限制:32000kb
我的代码
#include
#include
int main()
{
int s[10], a[10] = { 0 }, b[10] = {0};
int m = 0, x = 0, y = 0,t=0;
for (int i = 0;i < 10;++i)
{
scanf_s("%d", &s[i]);
if (s[i] > 0)
++m;
else break;
}
for (int i = 0;i < m;++i)
{
if (s[i] % 2 == 0 && s[i > 0])
{
b[x] = s[i];
++x;
}
else if (s[i] % 2 != 0 && s[i > 0])
{
a[y] = s[i];
++y;
}
}
for (int i = 1; i < 10;++i)
{
for (int j = 0;j
{
if (a[j] > a[j + 1])
{
t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
for (int j = 0;j < x-i;++j)
if (b[j] > b[j + 1])
{
t = b[j];
b[j] = b[j + 1];
b[j + 1] = t;
}
}
}
for (int i = 0;i < y;++i)
{
printf("%d ", a[i]);
}
for (int i = 0;i < x;++i)
{
printf("%d ", b[i]);
}
system("pause");
return 0;
}
报错内容
编译错误
a.c: In function 'main':
a.c:9:3: warning: implicit declaration of function 'scanf_s' [-Wimplicit-function-declaration]
/tmp/cc22hHTe.o: In function main': a.c:(.text.startup+0x7d): undefined reference to
scanf_s'
collect2: error: ld returned 1 exit status
两个例子都能运行出来,哪里还有问题啊
不认识scanf_s这个函数
你的写法太复杂了,修改如下,供参考:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int s[10], a[10] = { 0 }, b[10] = { 0 };
int m = 0, x = 0, y = 0, t = 0;
for (int i = 0; i < 10; ++i)
{
scanf("%d", &a[i]);
if (a[i] > 0)
++m;
else break;
}
for (int i = m - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (a[j] % 2 == 0 && a[j + 1] % 2 != 0 || // 奇数在前,偶数在后
a[j] % 2 != 0 && a[j + 1] % 2 != 0 && a[j] > a[j + 1] || //都是奇数,升序
a[j] % 2 == 0 && a[j + 1] % 2 == 0 && a[j] > a[j + 1]) //都是偶数,升序
t = a[j], a[j] = a[j + 1], a[j + 1] = t;
}
}
for (int i = 0; i < m; i++)
printf("%d ", a[i]);
#if 0
for (int i = 0; i < m; ++i)
{
if (s[i] % 2 == 0 && s[i > 0])
{
b[x] = s[i];
++x;
}
else if (s[i] % 2 != 0 && s[i > 0])
{
a[y] = s[i];
++y;
}
}
for (int i = 1; i < 10; ++i)
{
for (int j = 0; j < y - i; ++j)
{
if (a[j] > a[j + 1])
{
t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
for (int j = 0; j < x - i; ++j)
if (b[j] > b[j + 1])
{
t = b[j];
b[j] = b[j + 1];
b[j + 1] = t;
}
}
}
for (int i = 0; i < y; ++i)
{
printf("%d ", a[i]);
}
for (int i = 0; i < x; ++i)
{
printf("%d ", b[i]);
}
system("pause");
#endif
return 0;
}