冒泡结构与选择结构

今天完成的两个小作业:

冒泡结构:

#include 


int main()
{ 
int a[5];
int n,m,i,j,k;
printf ("wirte 5 numbers:\n");
for (n = 0; n < 5; n++)
scanf ("%d",&a[n]);

for (k = 1; k < 5; k++)
{
for(i = 0; i < 4; i++)
{
if (a[i] < a[i+1])
{
a[i] = a[i] + a[i+1];
a[i+1] = a[i] - a[i+1];
a[i] = a[i] - a[i+1];
}
}
while (k == 4)
break;
}



for (m = 0; m < 5; m++)
printf ("%d;",a[m]);
printf("\n");
return 0;


}



选择结构:

#include 


int main()
{ 
int a[8];
int n,i,j,k,m;


printf ("input 8 numbers:\n");
for (n = 0; n < 8; n++)
scanf ("%d",&a[n]);

for (i = 0; i < 8; i++)
{
k = i;
for (j = i + 1; j < 8; j++)
{
if(a[k] > a[j])
k = j;
}
if (i != k)
{
a[i] = a[i] + a[k];
a[k] = a[i] - a[k];
a[i] = a[i] - a[k];
}
}


for (m = 0; m < 8; m++)
printf("%d;",a[m]);
printf("\n");


return 0;
}




你可能感兴趣的:(冒泡结构与选择结构)