gudong1996 于 2016.04.21 22:57 提问

#include
#include
#define N 40
int Linsearch(long id[],long m,int n);
void Linsort(int score[],long id[],int n);
void Selectsort(int score[],long id[],int n);
int Binsearch(long id[],long m,int n);
void Printf(int score[],long id[],int n);

int main()
{
int score[N];//记录学生成绩
long id[N];//记录学生学号
long x,x1;
int a,a1;//接收返回学号的值

``````//记录学生成绩

//输出成绩和学号
Printf(score,id,num);

// 顺序查找法
printf("输入你要查找的学生的学号：");
scanf("%ld",&x);
a=Linsearch(id,x,num);
printf("%d %ld\n",score[a],id[a]);

//线性排序法
Linsort(score,id,num);

//输出成绩和学号
Printf(score,id,num);

//选择排序法
Selectsort(score,id,num);

//输出成绩和学号
Printf(score,id,num);

//折半查找法
printf("输入你要查找的学生的学号：");
scanf("%ld\n",&x1);
a1=Binsearch(id,x1,num);
if(a1 != -1)
{
printf("%d %ld\n",score[a1],id[a1]);
}
else
{
printf("大兄弟，没这个人啊!");
}

//输出成绩和学号
Printf(score,id,num);

return 0;
``````

}

//录入学生成绩 和学号
{
int i=-1;
do
{
i++;
printf("input score and id:");
scanf("%d %ld",&score[i],&id[i]);
}while((score[i] >= 0)&&(id[i] >=0));
return i;
}
//顺序查找
int Linsearch(long id[],long m,int n)
{
int i;

``````for(i=0;i<n;i++)
{
if(m == id[i])
{
return i;
}
}
return -1;
``````

}
//线性排序,升序排
void Linsort(int score[],long id[],int n)
{
int i,j;
int temp1,temp2;

``````for(i=0;i<n-1;i++)
{

for(j=i+1;j<n;j++)
{
if(score[i]>score[j])
{
//交换成绩
temp1=score[j];
score[j]=score[i];
score[i]=temp1;
//交换学号
temp2=id[j];
id[j]=id[i];
id[i]=temp2;
}
}
}
``````

}
//选择排序法,降序排
void Selectsort(int score[],long id[],int n)
{
int i,j,k;
int temp1;
long temp2;

``````for(i=0;i<n-1;i++)
{
k=i;
for(j=n+1;j<n;j++)
{
if(score[j] > score[k])
{
k=j;
}
}
if(k != i)
{
//交换成绩
temp1 = score[k];
score[k] = score[i];
score[i] = temp1;

//交换学号
temp2 = id[k];
id [k] =id[i];
id[i] = temp2;
}
}
``````

}

//折半查找法
int Binsearch(long id[],long m,int n)
{
int high = n-1;
int low = 0;
int mid;

``````    while(low <= high)
{
mid = (high+low)/2;
if(m < id[mid])
{
high = mid-1;
}
else if(m > id[mid])
{
low = mid+1;
}
else
{
return mid;
}
}
return -1;
}
``````

//输出函数
void Printf(int score[],long id[],int n)
{
int i;
printf("=============================\n");
for(i=0;i < n;i++)
{

``````    printf("%d %ld\n",score[i],id[i]);

}
``````

}

3个回答

qq423399099      2016.04.22 13:38

for(j=n+1;j<n;j++)应该改为for(j=i+1;j<n;j++)

scanf("%ld\n",&x1);应该是scanf("%ld",&x1);

`````` #include <stdio.h>
#include <stdlib.h>
#define N 20
int Linsearch(long id[],long m,int n);
void Linsort(int score[],long id[],int n);
void Selectsort(int score[],long id[],int n);
int Binsearch(long id[],long m,int n);
void Printf(int score[],long id[],int n);

int main()
{
int score[N];//记录学生成绩
long id[N];//记录学生学号
long x,x1;
int a,a1;//接收返回学号的值

//记录学生成绩

//输出成绩和学号
Printf(score,id,num);

// 顺序查找法
printf("输入你要查找的学生的学号：");
scanf("%ld",&x);
a=Linsearch(id,x,num);
printf("%d %ld\n",score[a],id[a]);

//线性排序法
Linsort(score,id,num);

//输出成绩和学号
Printf(score,id,num);

//选择排序法
Selectsort(score,id,num);

//输出成绩和学号
Printf(score,id,num);

//折半查找法
printf("输入你要查找的学生的学号：");
scanf("%ld",&x1);
a1=Binsearch(id,x1,num);
if(a1 != -1)
{
printf("%d %ld\n",score[a1],id[a1]);
}
else
{
printf("大兄弟，没这个人啊!");
}

//输出成绩和学号
Printf(score,id,num);

return 0;
}

//录入学生成绩 和学号
{
int i=-1;
do
{
i++;
printf("input score and id:");
scanf("%d %ld",&score[i],&id[i]);
}while((score[i] >= 0)&&(id[i] >=0));
return i;
}
//顺序查找
int Linsearch(long id[],long m,int n)
{
int i;

for(i=0;i<n;i++)
{
if(m == id[i])
{
return i;
}
}
return -1;
}
//线性排序,升序排
void Linsort(int score[],long id[],int n)
{
int i,j;
int temp1,temp2;

for(i=0;i<n-1;i++)
{

for(j=i+1;j<n;j++)
{
if(score[i]>score[j])
{
//交换成绩
temp1=score[j];
score[j]=score[i];
score[i]=temp1;
//交换学号
temp2=id[j];
id[j]=id[i];
id[i]=temp2;
}
}
}
}
//选择排序法,降序排
void Selectsort(int score[],long id[],int n)
{
int i,j,k;
int temp1;
long temp2;

for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
{
if(score[j] > score[k])
{
k=j;
}
}
if(k != i)
{
//交换成绩
temp1 = score[k];
score[k] = score[i];
score[i] = temp1;

//交换学号
temp2 = id[k];
id [k] =id[i];
id[i] = temp2;
}
}
}

//折半查找法
int Binsearch(long id[],long m,int n)
{
int high = n-1;
int low = 0;
int mid;

while(low <= high)
{
mid = (high+low)/2;
if(m < id[mid])
{
high = mid-1;
}
else if(m > id[mid])
{
low = mid+1;
}
else
{
return mid;
}
}
return -1;
}
//输出函数
void Printf(int score[],long id[],int n)
{
int i;
printf("=============================\n");
for(i=0;i < n;i++)
{

printf("%d %ld\n",score[i],id[i]);

}
}
``````

caozhy      2016.04.21 23:08

4 65
1 80
2 75
3 70

1 65
2 70
3 75
4 80

u012155923   2016.04.22 08:31