链接:
http://poj.org/problem?id=1416
题意:
输入两个数t和num,问如何分割后的num的和小于t并且与t最接近
题解:
按着题目思路敲就好了
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<stdlib.h>
#include <string.h>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<time.h>
using namespace std;
#define MAX_N 100005
#define inf 0x3f3f3f3f
#define LL long long
#define ull unsigned long long
const LL INF = 9e18;
const int mod = 4e4+7;
typedef pair<int, int>P;
string tag, n;
int nLen;
int Tag;
bool flag;
int ans;
int d;
string S;
int cal(int l, int r, string s)
{
int sum = 0;
for(int i=l; i<r; i++) {
sum = sum*10 + s[i]-'0';
}
return sum;
}
void dfs(int k, int sum, string s)
{
if(sum>Tag || k>nLen)
return;
if(k == nLen) {
if(Tag-sum==d) {
flag = true;
}
else if(Tag - sum < d) {
flag = false;
d = Tag - sum;
S = s;
ans = sum;
}
}
for(int i=1; i<=nLen; i++) {
string tmp = s + ' ';
for(int j=k; j<k+i; j++)
tmp += n[j];
dfs(k+i, sum+cal(k, k+i, n), tmp);
}
}
int main()
{
while(cin >> tag >> n) {
if(tag=="0" && n=="0")
break;
flag = false;
d = inf;
nLen = n.length();
string s = "";
Tag = cal(0, tag.length(), tag);
dfs(0, 0, s);
if(flag) {
printf("rejected\n");
}
else {
if(d == inf)
printf("error\n");
else {
printf("%d",ans);
cout << S << endl;
}
}
}
}