写一个C程序
动态规划 [此贴子已经被作者于2018-11-21 05:15编辑过]
/** *\file test.cc */ #include <stdio.h> #include <assert.h> #define MAX_ARY_SIZE (100) ///< unsigned int id_ary[MAX_ARY_SIZE]; unsigned int val_ary[MAX_ARY_SIZE]; struct{ unsigned int idx; ///< 真实下标 unsigned int val; }swap[MAX_ARY_SIZE]; int main(void) { unsigned int n = 0, sn = 0; unsigned int k = 0; unsigned int i = 0; scanf("%u %u", &n, &k); assert(n >= k); sn = n; /* 循环输入 */ for (i = 0; i < n; ++i){ scanf("%u", &val_ary[i]); swap[i].val = val_ary[i]; swap[i].idx = i; /* 初始化id */ id_ary[i] = i; } while (k < sn){ unsigned int sum = 0, tmp = 0; unsigned int min_idx = 0; sum = swap[0].val + swap[1].val; for (i = 1; i < sn - 1; ++i){ tmp = swap[i].val + swap[i + 1].val; if (sum > tmp){ min_idx = i; sum = tmp; } } id_ary[swap[min_idx].idx] = id_ary[swap[min_idx + 1].idx]; swap[min_idx].val += swap[min_idx + 1].val; /* 调整位置 */ for (i = min_idx + 1; i < sn - 1; ++i){ swap[i].val = swap[i+1].val; swap[i].idx = swap[i+1].idx; } sn--; } /* 输出结果 */ for (i = 0; i < n; ++i){ if ((0 < i) && (i < n) && (id_ary[i-1] != id_ary[i])){ printf("|"); } printf(" %u ", val_ary[i]); } printf("\n"); return 0; } #if 0 ~/test # ./tests 9 5 7 11 12 5 3 20 6 5 2 7 11 | 12 | 5 3 | 20 | 6 5 2 ~/test # ./tests 9 7 7 11 12 5 3 20 6 5 2 7 | 11 | 12 | 5 3 | 20 | 6 | 5 2 ~/test # ./tests 9 3 7 11 12 5 3 20 6 5 2 7 11 | 12 5 3 | 20 6 5 2 #endif
[此贴子已经被作者于2017-4-16 01:58编辑过]
#include<stdio.h> #include<stdlib.h> #include<conio.h>//划分整数问题~程序有误~ void Init(int** s,int** p,int* N,int* K); //初始化数据 void Print(int s[],int N); //输出数据 void Print_Result(int s[],int p[],int N); //输出执行结果 int Count(int s[],int i,int j); //求和 void fun(int s[],int p[],int N,int K); //函数执行主体 int main() { int N=0; int K=0; int* s=NULL; int* p=NULL; Init(&s,&p,&N,&K); puts("测试输入数据如下:"); Print(s,N); //开始检查初始化数据 fun(s,p,N,K); puts("划分结果如下:"); Print_Result(s,p,N); return 0; } void Init(int** s,int** p,int* N,int* K) //初始化数据 { int i=0; puts("请输入N K的值(中间用空格隔开):"); scanf("%d%d",N,K); *s=(int*)malloc(*N*sizeof(int)); *p=(int*)malloc(*K+1*sizeof(int)); if (*s==NULL||*p==NULL) { puts("分配空间失败!"); exit(0); } if (*N<=0||*K<=0||*N<*K) { puts("输入数据出错!"); exit(0); } printf("请输入%d个数:\n",*N); for (i=0;i<*N;++i) scanf("%d",&((*s)[i])); for (i=0;i<*K;++i) (*p)[i]=i; (*p)[i]=*N; } void Print(int s[],int N) { int i=0; for (i=0;i<N;++i) printf("%-4d",s[i]); puts(""); } int Count(int s[],int i,int j) //求两个划分区间的和 { int sum=0; while (i<j) sum+=s[i++]; return sum; } void fun(int s[],int p[],int N,int K) //函数执行主体 { int i=0; //循环变量 int point=K-1; //指针数据指向尾部 while (1) { point=K-1; while (point>0&&(Count(s,p[point-1],p[point])>=Count(s,p[point],p[point+1])||p[point+1]-p[point]==1)) --point; //这里判断条件少考虑了跨划分标记遍历的情况,要完善一下,感觉要新增一个判断函数还要改变遍历标记的值 if (point==0) break; while (p[point+1]-p[point]>1&&Count(s,p[point-1],p[point])<Count(s,p[point],p[point+1])) ++p[point]; } } void Print_Result(int s[],int p[],int N) { int i=0; int j=1; for (i=0;i<N;++i) { if (i==p[j]) { printf("| "); j++; } printf("%-4d",s[i]); } puts(""); }
[此贴子已经被作者于2017-4-16 23:02编辑过]
[此贴子已经被作者于2017-4-17 20:30编辑过]