69,382
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#define LEN 7
#define k 7
int a[LEN] = {4, 2, 5, 8, 3, 9, 6};
void swap(int c, int b)
{
int tmp;
if (c > b)
{
tmp = c;
c = b;
b = tmp;
}
return;
}
int main()
{
int i, j;
for (i = 0; i < k; i ++)
{
for (j = i + 1; j < LEN; j++)
{
swap(a[i], a[j]);
}
printf ("%d\n",a[k - 1]);
}
printf ("%d\n",a[k - 1]);
return 0;
}
void swap(int c, int b)
{
int tmp;
if (a[c] > a[b])
{
tmp = a[c];
a[c] = a[b];
a[b] = tmp;
}void swap(int* a,int* b){
int* temp=a;
a=b;
b=temp;
}
调用的时候:
swap(a,b);
void swap(int c, int b)
{
int tmp;
if (c > b)
{
tmp = c;
c = b;
b = tmp;
}
return;
}
这个函数在其内部做了变换,但在main中没有作用,应该为:
void swap(int &a, int &b)
{
int temp = 0;
if(a > b) {
temp = a;
a = b;
b = a;
}
}