有几点疑问,但是我刚学C,根本找不出来有什么问题。希望大家帮忙找找#include<stdio.h>
int TOTAL;//换成全局变量,照样是输入几本就几本,去重完全没有起作用
void total(int book[], int n)//统计书本数量并去重
{
int j,m;
for (j = 0; j < n-1; j++)
{
if (book[j] == book[j + 1])
{
for (m = j; m < n - j; m++)
{
if (m < n - 2)
{
book[m + 1] = book[m + 2];
}
else
{
book[m + 1] = NULL;
}
}
TOTAL--;
//i--;
}
}
printf("总共需要买%d本书\n", TOTAL);
}
void sort(int book[],int n)//对里面内容排序并输出
{
int i,j;
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - 1 - i; j++)
{
if (book[j] > book[j + 1])
{
book[j] ^= book[j+1];
book[j+1] ^= book[j];
book[j] ^= book[j+1];
}
}
}
for (i = 1; i <= (n / 10); i++)
{
for (j = (i - 1) * 10 + 1; j <= i * 10; j++)
{
printf("%d\t", book[j]);
}
printf("\n");
}
}
void main()
{
int i,num[100];
printf("请输入想买书的同学数量(不超过100人):\n");
scanf("%d\n",&TOTAL);
//fflush(stdin);加此代码则不执行sort函数
for (i = 0; i < TOTAL; i++)//总是先输入,再连续出现两行printf内容,然后才正常。始终未查出什么原因
{
printf("请输入第%d个同学想买的书的IBSN号:\n",i+1);
scanf("%d",&num[i]);
}
total(num, TOTAL);
sort(num, TOTAL);
//sort(num, total(num, n));
}
|