Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

又是水题

Posted by KatrineYang at 2016-11-13 11:50:17 on Problem 1564
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include <algorithm>
using namespace std;

int gs, T, N;
int sz[15], sl[15];
int jc[15];
int bfh[15];

int ct;

void dfs(int bs, int sy){
	if(sy > bfh[bs]) return;
	if(sy == bfh[bs]){
		ct++;
		bool printed = 0;
		for(int i = 0; i < bs; i++){
			for(int j = 0; j < jc[i]; j++){
				if(printed) printf("+");
				printf("%d",sz[i]);
				printed=1;
			}
		}
		for(int i = bs; i < gs; i++){
			for(int j = 0; j < sl[i]; j++){
				if(printed) printf("+");
				printf("%d",sz[i]);
				printed=1;
			}
		}
		printf("\n");
		return;
	}
	if(sy == 0){
		ct++;
		bool printed = 0;
		for(int i = 0; i < bs; i++){
			for(int j = 0; j < jc[i]; j++){
				if(printed) printf("+");
				printf("%d",sz[i]);
				printed = 1;
			}
		}
		printf("\n");
		return;
	}
	if(bs >= gs) return;
	int zd = sl[bs];
	if(sy/sz[bs] < zd) zd = sy/sz[bs];
	for(int i = zd; i >= 0; i--){
		jc[bs] = i;
		dfs(bs+1, sy-i*sz[bs]);
	}
}

int main() {
	while(1){
		scanf("%d%d",&T,&N);
		if(!T && !N) break;
		int s = -1, cnt = 0;
		gs = 0;
		for(int i = 0; i < N; i++){
			int temp;
			scanf("%d",&temp);
			if(temp == s) cnt++;
			else{
				if(s != -1){
					sz[gs] = s;
					sl[gs] = cnt;
					gs++;
				}
				cnt = 1;
				s = temp;
			}
		}
		sz[gs] = s;
		sl[gs] = cnt;
		gs++;
		ct = 0;
		printf("Sums of %d:\n", T);
		bfh[gs] = 0;
		for(int i = gs-1; i >= 0; i--){
			bfh[i] = bfh[i+1] + sz[i]*sl[i];
		}
		dfs(0, T);
		if(!ct) printf("NONE\n");
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator