PATA1086
来源:互联网 发布:加拿大 软件 研究生 编辑:程序博客网 时间:2024/06/07 07:09
OS: OS X 10.10.5
IDE:Xcode 7.2.1
#include <stdio.h>#include <stdlib.h>#include<time.h>int cmp ( const void *a , const void *b){//a function used in the qsort return *(int *)b - *(int *)a; //decreasing order.}int main(int argc, const char * argv[]) { int N,M; scanf("%d%d",&N,&M); int MaxSum[N+1][M+1];//MaxSum[i][j] represents the maximal sum of the preceding i numbers, and the sum is <=j int flag[N+1][M+1];// flag[i][j]==1 represents Number[i] is number to be added to get MaxSum[i][j], while flag[i][j]==0 not for (int i=0; i<=N; i++) { //initilaize MaxSum[][] and flag[][] for (int j=0; j<=M; j++) { MaxSum[i][j]=0; flag[i][j]=0; } } int Number[N]; for (int i=1; i<=N; i++) { //read N numbers scanf("%d",&Number[i]); } /*for(int i=1;i<n;i++){ //to test large nunbers of data, assign the Number[i] from 1 to n, Number[i]=1;//because the Number[i] will be sorted ,so the sequence is not important. }*/ qsort(&Number[1],N ,sizeof(int),cmp); //sort in the sequence of decreasing for (int i=1; i<=N; i++) {//loop for (int j=1; j<=M; j++) { if (j-Number[i]<0||MaxSum[i-1][j-Number[i]]+Number[i]<MaxSum[i-1][j]) {// Number[i] is not number to be added to get MaxSum[i][j] MaxSum[i][j]=MaxSum[i-1][j]; } else{// Number[i] is number to be added to get MaxSum[i][j] MaxSum[i][j]=MaxSum[i-1][j-Number[i]]+Number[i]; //calculate MaxSum[i][j] flag[i][j]=1; //label it to track trail } } } int Sum=0;//test whether the result is true or not if (MaxSum[N][M]!=M) { //MaxSum[N][M] < M, that is ,no solution printf("No Solution");//print } else{ int flag2=0;//for the format of outputing for (int i=N,j=M; j>0; i--) { //because all the number is in decreasing order,so we print the numbers in increasing order, if (!flag[i][j]) { //flag[i][j] check whether Number[i] is number to be added to get MaxSum[i][j] or not flag[i][j]=1; //label it. continue;//next loop } if (!flag2++) { printf("%d",Number[i]);//priint the first number Sum+=Number[i]; } else{ printf(" %d",Number[i]);//print the next number Sum+=Number[i]; } j-=Number[i];//subtract from sum. } } /*printf("\n"); if (Sum==M) { printf("The result is true.\n"); } else{ printf("The result is false.\n"); }*/ return 0;}
1 0
- PATA1086
- pata1086
- SlickEdit基本设置
- html网页使用单引号双引号 转义符
- java多线程入门学习(三)
- 数据结构(java)——栈及其应用
- Retrofit使用教程(三):Retrofit与RxJava初相逢
- PATA1086
- leetcode-13-roman to integer
- 【OpenGL】Shader实例分析(五)- 边缘检测
- 欢迎使用CSDN-markdown编辑器
- bzoj 2299: [HAOI2011]向量 数学
- 自定义View基础
- MySQL数据库引擎介绍、区别、创建和性能测试的深入分析
- loadrunner12.50安装说明
- Android-Cloud-TagView-Plus集合到自定义dialog遇到的问题