C语言的程序代码
发布网友
发布时间:2022-04-20 10:27
我来回答
共3个回答
热心网友
时间:2023-07-19 15:27
#include <stdio.h>
#include <stdlib.h>
void DicPrint(char *a, char n)
{
char i;
for (i = 0; i < n; i++) {
printf("%u ", a[i]);
}
printf("\n");
}
void DicInit(char *a, char n)
{
char i;
for (i = 0; i < n; i++) {
a[i] = i + 1;
}
DicPrint(a, n);
}
char DicFindLastSeq(char *a, char n)
{
char i, m = -1;
for (i = n - 1; i > 0; i--) {
if (a[i - 1] < a[i]) {
m = i - 1;
break;
}
}
return m;
}
char DicFindLastBiggerValue(char *a, char n, char m)
{
char i;
for (i = n - 1; i > m; i--) {
if (a[m] < a[i]) {
break;
}
}
return i;
}
void DicSwap(char *a, char m, char k)
{
char tmp;
tmp = a[m];
a[m] = a[k];
a[k] = tmp;
}
void DicAntitone(char *a, char n, char m)
{
char i, j, tmp;
for (i = m + 1, j = n - 1; i <= j; i++, j--) {
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
DicPrint(a, n);
}
int main(void)
{
char *a = NULL, buf[2], len;
char m, k;
fgets(buf, sizeof(buf), stdin);
len = atoi(buf);
a = (char *)malloc(len);
if (NULL == a) {
printf("Malloc fail!\n");
return 0;
}
DicInit(a, len);
while ((m = DicFindLastSeq(a, len)) >= 0) {
k = DicFindLastBiggerValue(a, len, m);
DicSwap(a, m, k);
DicAntitone(a, len, m);
}
free(a);
return 0;
}
--------------------------------------------------------------------------
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
void DicPrint(int *a, int len)
{
int i;
for (i = 0; i < len; i++) {
printf("%d ", a[i]);
}
printf("\n");
}
void DicSort(int *a, int len)
{
int i, j, m, k, tmp;
DicPrint(a, len);
m = -1;
for (i = len -2; i >= 0; i--) {
if (a[i] < a[i + 1]) {
m = i;
break;
}
}
if (m < 0) {
return;
} else {
for (i = len - 1; i > m; i--) {
if (a[i] > a[m]) {
k = i;
break;
}
}
tmp = a[m];
a[m] = a[k];
a[k] = tmp;
for (i = m + 1, j = len - 1; i <= j; i++, j--) {
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
DicSort(a, len);
}
}
int main(void)
{
int *a = NULL, len, i;
scanf("%d", &len);
if (len > 8) {
printf("Input Error!\n");
return 0;
}
a = malloc(len * sizeof(int));
if (a == NULL) {
printf("Malloc Failed!\n");
return 0;
}
for (i = 0; i < len; i++) {
a[i] = i + 1;
}
DicSort(a, len);
free(a);
return 0;
}
热心网友
时间:2023-07-19 15:27
#include <stdio.h>
void perm(int *data, int n, int curr)
{
int i,t;
if(curr==n-1)
{
for(i=0;i<n;++i)
printf("%d ",data[i]);
printf("\n");
}
else
{
for(i=curr;i<n;++i)
{
t=data[curr],data[curr]=data[i],data[i]=t;
perm(data,n,curr+1);
t=data[curr],data[curr]=data[i],data[i]=t;
}
}
}
int main()
{
int array[]={1,2,3};
perm(array,sizeof(array)/sizeof(array[0]),0);
return 0;
}
热心网友
时间:2023-07-19 15:28
可以百度文库搜索
算法之字典排序法