- UID
- 70756
注册时间2010-11-1
阅读权限85
最后登录1970-1-1
见习版主
TA的每日心情 | 擦汗 2016-4-19 21:35 |
---|
签到天数: 3 天 [LV.2]偶尔看看I
|
本帖最后由 wai1216 于 2016-6-19 11:27 编辑
个人评价写得略丑。。。
--------------
1.
[C] 纯文本查看 复制代码
void stol(int *a, int b)
{
int temp;
if (b - 1 <= 0)
return;
if (*(a + b - 2) < *(a + b - 1))
{
temp = *(a + b - 1);
*(a + b - 1) = *(a + b - 2);
*(a + b - 2) = temp;
stol(a, b - 1);
}
stol(a, b - 1);
}
void main()
{
int i = 0, n = 10;
int a[] = { 12,45,17,95,24,67,32,11,5,100 };
for (i = n; i > 0; i--)
{
stol(a, i);
}
for (i = 0; i < n; i++)
{
printf("%d ", *(a + i));
}
}
看视频之后的2,3
--------------
2.[C] 纯文本查看 复制代码 void swap(int * a,int * b)
{
int temp = *b;
*b = *a;
*a = temp;
}
void stol(int *a, int n)
{
int i = 0;
for (i = n; i > 0; i--) /*将循环放在了函数里面*/
{
if (n - 1 <= 0)
return;
if (*(a + n - 2) < *(a + n - 1))
{
swap(a + n - 2,a + n - 1);
stol(a, n - 1);
}
stol(a, n - 1);
}
}
void main()
{
int i = 0, n = 10;
int a[] = { 12,45,17,95,24,67,32,11,5,100 };
stol(a,n);
for (i = 0; i < n; i++)
{
printf("%d ", *(a + i));
}
}
-------------------
3.[C] 纯文本查看 复制代码 void swap(int * a,int * b)
{
int temp = *b;
*b = *a;
*a = temp;
}
int Retn(int a,int b)
{
return a < b ? 1: 0;
}
void stol(int *a, int n)
{
int i = 0;
if (n - 1 <= 0)
return;
for (i = n; i > 0; i--)
{
if (Retn(*(a + n - 2),*(a + n - 1))) /*修改if判断,以及逻辑化简写stol*/
swap(a + n - 2,a + n - 1);
stol(a, n - 1);
}
}
void main()
{
int i = 0, n = 10;
int a[] = { 12,45,17,95,24,67,32,11,5,100 };
stol(a,n);
for (i = 0; i < n; i++)
{
printf("%d ", *(a + i));
}
}
-------------
4.[C] 纯文本查看 复制代码 void swap(int * a,int * b)
{
int temp = *b;
*b = *a;
*a = temp;
}
int Retn(int a,int b)
{
return a < b ? 1: 0;
}
void stol(int *a, int n)
{
int i = 0;
if (n - 1 <= 0)
return;
for (i = n; i > 0; i--)
{
if (Retn(*(a + n - 2),*(a + n - 1)))
swap(a + n - 2,a + n - 1);
stol(a, n - 1);
}
}
void Showarray(int *a,int n)
{
int i = 0;
for (i = 0; i < n; i++)
{
printf("%d ", *(a + i));
}
}
void main()
{
int n = 10;
int a[] = { 12,45,17,95,24,67,32,11,5,100 };
stol(a,n);
Showarray(a,n); /*调用函数来输出*/
} |
|