1614Shredding Company

我的错误代码

#include "iostream"
#include "math.h"
#include "algorithm"
#define N 100000;
using namespace std;
int total,list[1000],n,m,MIN,top,step,data[1000];

void dfs(int a){
  int i,j,num;
  if(total>m)return;
  if(m-total<MIN&&a==top){MIN=m-total;}
  for(i=a;i<top;i++){
    num=0;
    for(j=a;j<=i;j++){
      num=num+list[j]*pow(10,j-a);
    }
    total+=num;
    //cout<<a<<' '<<num<<' '<<total<<endl;system("pause");
    dfs(i+1);
    total-=num;
  }
}

int main(){
  int i;
  while(cin>>m>>n&&m&&n){
    top=1;step=1;
    while(n){
      list[top++]=n%10;
      n=n/10;
    }
    MIN=N;
    dfs(1);
    cout<<m-MIN<<endl;
  }
}

人家的代码

// POJ1416.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <iterator>
#include <string>
using namespace std;

static int clonum;
static bool repeat;
static int sh[20];
static int stps;
void DFS(const string& num, int beg, int end, int cnt, int t, int stp, int shredded[20])
{
    int cutnum = 0;
    if (end == num.size())
    {
        for (int i = beg; i < end; ++i) cutnum = cutnum * 10 + (num[i]-'0');
        cnt += cutnum;
        if (cnt == clonum)repeat = true;
        else if(cnt <= t && cnt > clonum)
        {
            shredded[stp] = cutnum;
            copy(&shredded[0],&shredded[stp + 1], &sh[0]);
            clonum = cnt;
            stps = stp;
            repeat = false;
        }
    }
    else
    {
        DFS(num, beg, end + 1, cnt, t, stp, shredded);
        for (int i = beg; i < end; ++i) cutnum = cutnum * 10 + (num[i]-'0');
        if (cnt + cutnum <= t)
        {
            shredded[stp] = cutnum;            
            DFS(num, end, end + 1, cnt + cutnum, t, stp + 1, shredded);
        }
    }
}
int main(int argc, char* argv[])
{
    int t;
    string num;
    while (cin >> t >> num && t != 0 && num != "0")
    {
        int n;
        sscanf(num.c_str(),"%d",&n);
        if (n == t)
        {
            cout << t <<" " << n << endl;
            continue;
        }
        n = 0;
        for (int i = 0; i < num.size(); ++i) n += num[i] - '0';
        if (n > t )
        {
            cout << "error\n";
            continue;
        }
        clonum = 0;
        repeat = false;
        int shredded[20];
        memset(shredded, 0, sizeof(shredded));
        DFS(num, 0, 1, 0, t, 0,shredded);
        if (repeat == true) cout << "rejected\n";
        else
        {
            cout << clonum << " ";
            copy(&sh[0], &sh[stps + 1], ostream_iterator<int>(cout," "));
            cout<<endl;
        }
    }
    return 0;
}

 

 

posted @ 2013-10-02 20:12  龙城星  阅读(154)  评论(0编辑  收藏  举报