zoukankan      html  css  js  c++  java
  • POJ 1416 Shredding Company

      给出一个Max 和一串数字。将这一串数字分割成若干个数,其和为sum。求最接近但不超过Max的sum。。。

      将这个sum及这若干个数输出来。

      DFS + 打印路径

      

      1 #include <iostream>
      2 #include <algorithm>
      3 #include <cstdlib>
      4 #include <cstring>
      5 #include <cstdio>
      6 
      7 using namespace std;
      8 
      9 char SNum[10];
     10 
     11 struct N
     12 {
     13     int data,ans,pre;
     14     N *l,*r;
     15 }*root;
     16 
     17 N *creat()
     18 {
     19     N *p = (N *)malloc(sizeof(N));
     20     p->l = p->r = NULL;
     21     return p;
     22 }
     23 
     24 struct P
     25 {
     26     int site,pre;
     27 }path[100000];
     28 
     29 int top;
     30 
     31 void insert(N *&root,int data,int pre)
     32 {
     33     if(root == NULL)
     34     {
     35         root = creat();
     36         root->ans = 1;
     37         root->pre = pre;
     38         root->data = data;
     39         return ;
     40     }
     41     if(data < root->data)
     42         insert(root->l,data,pre);
     43     else if(data > root->data)
     44         insert(root->r,data,pre);
     45     else root->ans++;
     46 }
     47 
     48 void dfs(int sum,int Max,char *s,int site,int pre)
     49 {
     50     if(sum > Max)
     51         return ;
     52 
     53     if(strlen(s) == 0)
     54     {
     55         if(sum <= Max)
     56         {
     57             insert(root,sum,pre);
     58         }
     59         return ;
     60     }
     61 
     62     int i,dec,j,temp,len;
     63 
     64     for(i = 0,len = strlen(s);i < len; ++i)
     65     {
     66         for(j = i,temp = 0,dec = 1;j >= 0; --j,dec *= 10)
     67             temp += (s[j]-'0')*dec;
     68 
     69         path[top].pre = pre;
     70         path[top].site = site;
     71         top++;
     72 
     73         dfs(sum+temp,Max,s+i+1,site+i+1,top-1);
     74     }
     75 }
     76 
     77 void PrintPath(int pre)
     78 {
     79     int s[100],top = 0;
     80     while(pre != -1)
     81     {
     82         s[top++] = path[pre].site;
     83         pre = path[pre].pre;
     84     }
     85 
     86     for(int i = top-1;i >= 0; --i)
     87     {
     88         printf(" ");
     89         if(i != 0)
     90         {
     91             for(int j = s[i];j < s[i-1]; ++j)
     92                 printf("%c",SNum[j+1]);
     93         }
     94         else
     95         {
     96             for(int j = s[i]+1; SNum[j] != ''; ++j)
     97                 printf("%c",SNum[j]);
     98         }
     99     }
    100     puts("");
    101 }
    102 
    103 void SeekMax(N *root)
    104 {
    105     if(root->r != NULL)
    106         SeekMax(root->r);
    107     else
    108     {
    109         if(root->ans == 1)
    110         {
    111             printf("%d",root->data);
    112             PrintPath(root->pre);
    113         }
    114         else
    115             printf("rejected
    ");
    116     }
    117 }
    118 
    119 int main()
    120 {
    121     int Max,sum,j,i,len,dec;
    122 
    123     while(scanf("%d %s",&Max,SNum+1) && (Max != 0 || strcmp(SNum+1,"0") != 0))
    124     {
    125         root = NULL;
    126 
    127         top = 0;
    128 
    129         for(i = 1,len = strlen(SNum+1);i <= len; ++i)
    130         {
    131             for(j = i,dec = 1,sum = 0;j >= 1; --j,dec *= 10)
    132                 sum += (SNum[j]-'0')*dec;
    133             path[top].pre = -1;
    134             path[top].site = 0;
    135             top++;
    136             dfs(sum,Max,SNum+1+i,i,top-1);
    137         }
    138         if(root == NULL)
    139             cout<<"error"<<endl;
    140         else
    141             SeekMax(root);
    142     }
    143     return 0;
    144 }
    View Code

      今天拿到的第三个1A。。。。再用DFS写打印路径的题就剁手。。。

  • 相关阅读:
    Java 编程下的并发线程之间的同步代码块死锁
    Java 编程下的同步代码块
    Android 编程下两种方式注册广播的区别
    Java 编程下泛型的内部原理
    Android 编程下使用 Google 的 Gson 解析 Json
    c# 调用音库开发英语单词记忆本
    在.Net中实现RichClient+Restful+JPA架构探索实现
    Extjs 常用正则式
    企业内IT部门的一些问题总结
    WinServer2003部署VS2010的水晶报表
  • 原文地址:https://www.cnblogs.com/zmx354/p/3275816.html
Copyright © 2011-2022 走看看