程序的问题找到了
#include <stdio.h>
#include <string.h>
int main()
{
int a[10];
char b[10][10], c[10];
int i, j, k = 1, m, n, f, z, l;//后面循环部分k,这里赋值了,但是接下来k重新赋值了,不是1了
for (i = 0;i < 10;i++)
{
printf("shu ru hao he xing ming %d:",i+1);
scanf("%d", &a[i]);
printf("shu ru ming zhi:");
scanf("%s", &b[i]);
}
for (i = 0;i < 10;i++)
printf("%d %s\n", a[i], b[i]);
for (i = 0;i < 9;i++)
{
for(j = 0;j <= 9-i;j++)
if (a[j] > a[j+1])
{
k = a[j];//这里k被重新赋值了,错在这里啊
a[j] = a[j+1];
a[j+1] = k;
strcpy( c, b[j]);
strcpy( b[j], b[j+1]);
strcpy(b[j+1], c);
}
}
for (i = 0;i < 10;i++)
printf("%d %s\n", a[i], b[i]);
printf("shu ru yao zhao de xu hao:");
scanf("%d", &m);
printf("%d\n", m);
if (m < a[0] || m > a[9])
l = -1;
f = 9;
z = 0;
//这里填上k=1;就可以了
while ((k == 1) && (z <= f))
{
n = (f+z) / 2;
if (m == a[n])
{
printf("zhe ge shi %d,ming zhi shi:%s", a[n], b[n]);
k = 0;
}
else if(m > a[n])
z = n + 1;
else
f = n - 1;
}
if (k == 1 || l == -1)
printf("mei you zhao dao \n");
return 0;
}
谢谢大家的参与,谢谢
#include <stdio.h>
#include <string.h>
int main()
{
int a[10];
char b[10][10], c[10];
int i, j, k = 1, m, n, f, z, l;//后面循环部分k,这里赋值了,但是接下来k重新赋值了,不是1了
for (i = 0;i < 10;i++)
{
printf("shu ru hao he xing ming %d:",i+1);
scanf("%d", &a[i]);
printf("shu ru ming zhi:");
scanf("%s", &b[i]);
}
for (i = 0;i < 10;i++)
printf("%d %s\n", a[i], b[i]);
for (i = 0;i < 9;i++)
{
for(j = 0;j <= 9-i;j++)
if (a[j] > a[j+1])
{
k = a[j];//这里k被重新赋值了,错在这里啊
a[j] = a[j+1];
a[j+1] = k;
strcpy( c, b[j]);
strcpy( b[j], b[j+1]);
strcpy(b[j+1], c);
}
}
for (i = 0;i < 10;i++)
printf("%d %s\n", a[i], b[i]);
printf("shu ru yao zhao de xu hao:");
scanf("%d", &m);
printf("%d\n", m);
if (m < a[0] || m > a[9])
l = -1;
f = 9;
z = 0;
//这里填上k=1;就可以了
while ((k == 1) && (z <= f))
{
n = (f+z) / 2;
if (m == a[n])
{
printf("zhe ge shi %d,ming zhi shi:%s", a[n], b[n]);
k = 0;
}
else if(m > a[n])
z = n + 1;
else
f = n - 1;
}
if (k == 1 || l == -1)
printf("mei you zhao dao \n");
return 0;
}
谢谢大家的参与,谢谢