本文主要是介绍UVA 1073 ACdream,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include<stdlib.h>
using namespace std;
int main () {int a[4], b[1002], comp[1002] = {0}, comp1[1002] = {0};int n, flags,i;while (scanf("%d", &n) != EOF) {for (i = 0; i < 4; i++) {scanf("%d", &a[i]);}for ( i = 0; i < n; i++)scanf("%d", &b[i]);if (n < 4) {printf("NO\n");continue;}sort(b, b + n);int j;int term = 0;//tt = n;//int ff = 0;//ff用来for (i = 0; i < 4; i++) { // ff = 0;for (j = 0; j < n; j++) {if (a[i] <= b[j] && comp[j] == 0 && comp1[i] == 0) {term++;comp[j] = 1;comp1[i] = 1;//tt--;// ff = 1;break;}} if (j != n)continue;for (int p = n - 1; p >= 0; p--) {if(comp[p] == 0) {a[i] = a[i] - b[p];comp[p] = 1;i--;break;}}/* int p = n - 1;if (j == n && ff ==0) {while (p >= 0) {if (comp[p] == 0) {a[i] = a[i] - b[p];comp[p] = 1;// tt--;// ff1 = 1;i--;break;}} }*//*if (ff1 == 1) {i--;ff1 = 0;}*///if (tt == 0)// break;}int flags = 0;if (term == 4)flags = 1;int sum = 0;for (i = 0; i < n; i++) {if (comp[i] == 0) {sum = sum + b[i];}}if (flags == 1)printf("YES %d\n", sum);else printf("NO\n");/*for (i = 0; i < n; i++){comp[i] = 0;comp1[i] = 0;}*/memset(comp1,0,sizeof(comp1));memset(comp,0,sizeof(comp));}//system("pause");return 0;
}
二:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include<stdlib.h>
using namespace std;
int main () {int a[4], b[1002], comp[1002] = {0}, comp1[1002] = {0};int n, flags,i;while (scanf("%d", &n) != EOF) {for (i = 0; i < 4; i++) {scanf("%d", &a[i]);}for ( i = 0; i < n; i++)scanf("%d", &b[i]);if (n < 4) {printf("NO\n");continue;}sort(b, b + n);int j;int term = 0;for (i = 0; i < 4; i++) { for (j = 0; j < n; j++) {if (a[i] <= b[j] && comp[j] == 0 && comp1[i] == 0) {term++;comp[j] = 1;comp1[i] = 1;break;}} if (j != n)continue;for (int p = n - 1; p >= 0; p--) {if(comp[p] == 0) {a[i] = a[i] - b[p];comp[p] = 1;i--;break;}} }int flags = 0;if (term == 4)flags = 1;int sum = 0;for (i = 0; i < n; i++) {if (comp[i] == 0) {sum = sum + b[i];}}if (flags == 1)printf("YES %d\n", sum);else printf("NO\n");memset(comp1,0,sizeof(comp1));memset(comp,0,sizeof(comp));}//system("pause");return 0;
}
这篇关于UVA 1073 ACdream的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!