小程序:备考问题&递归实现每天备考时间的分配(含代码)

23 篇文章 0 订阅
15 篇文章 1 订阅

此题是某校园招聘的笔试题,由于种种原因,没有做完,现在将此题补上。

就我做这个问题,感觉主要是考察三点:
1、不定向数据的输入(windows下 结束符为ctrl+z,linux下结束符为ctrl+d)
2、递归问题:三要素
(1)可以把这个问题转化为一个新的问题,而这个新问题的解决方法与原问题的方法相同,只是处理的对象不同,但它们也只是有规律的递增或递减。
(2)可以通过转化过程使问题得以解决。
(3)必须有个终止递归的条件。
在这里要注意的是,写递归程序,首先考虑的就是递归终止的条件。。。把出口写好了,就容易了。
3、malloc 函数的释放问题
本次的小程序实验数据较小,但是当你处理过20-30G的数据的时候,你就会发现,不free掉开辟的空间,会使整个内存占的满满的。 而且linux含有保护机制,当内存大到一定程度的时候,系统会将消耗内存的进程给kill掉。。。So,一定要注意内存的问题。

注意:该程序写于64 bit centos6.5,G++ 编译运行木有问题。在32 bit windows 7,VC++6.0 也木有问题。。。

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Problem Description:
临近期末,让小东头疼的考试又即将到来了,而且是小东最不喜欢的科目。遗憾的是,小东得知d天后她必须参加此次考试。小东的父亲对她要求非常严格,要求她立即开始复习功课。为照顾她的情绪,父亲要求她每天该科目的学习时间在iminTime到imaxTime之间,并计划在考前检查小东是否按要求做了。若未能完成,小东将会受到惩罚。
现在小东的父亲要求检查小东的备考情况。遗憾的是,由于专注于备考,小东只是记录了自己备考的总时间sumTime,并没有记录每天复习所用的时间,也不知道准备情况是否符合父亲的要求。她想知道是否能够制作一个满足要求的时间表以应付父亲的检查。
小东希望你能够帮到她,你是否愿意?

输入中有多组测试数据。每组测试数据的第一行包含两个整数d和sumTime,1<=d<=30, 0<=sumTime<=240,分别表示小东复习的天数以及每天用于复习的时间之和。紧随其后的d行中,每行包含两个空格分隔的整数,为小东父亲要求小东在这一天用于复习时间的范围iminTime和imaxTime,0<=iminTime<=imaxTime<=8。
对每组测试数据,若能够做出一个满足小东父亲要求的时间表,则在单独的一行中输出Yes,并在随后的一行中给出每天复习花费的时间。否则输出No。
若满足要求的时间表不唯一,小东希望给父亲留下比较用功的映像,开始时每天复习的时间比较长。
input:
1 48
5 7
2 5
0 1
3 5

output:
No
Yes
1 4

/**
 * @filename      remarks.cc
 * @Synopsis      input : the remarks day and the sumTime
 *                output: whether it can make a good schedule
 * @author        XIU
 * @version       GCC 1
 * @date         2016-04-18
 */

#include<iostream>
#include<stdlib.h>
using namespace std;


/* ============================================================================*/
/**
 * @Synopsis      Judge whether the input data can make a good schedule
 *
 * @Param         d             all the review days
 * @Param         cur           from d-1 to 0
 * @Param         sumTime       from input sumTime to 0
 * @Param         iminTime      the pointer of iminTime
 * @Param         imaxTime      the pointer of imaxTime
 * @Param         result        restore the schedule
 *
 * @Returns       1     means it can make a good schedule
 *                0     means if can't make a good schedule
 */
/* ============================================================================*/
int compute( int d, int cur, int sumTime, int *iminTime, int *imaxTime ,int *result )
{

    if( cur == 0 && sumTime >= iminTime[cur] && sumTime <= imaxTime[cur] )
    {
        result[ cur ] = sumTime;
        if( result[0] == 0 )
            return -1;

        return 1;           
    }
    if( cur==0 && sumTime !=0 || cur!=0 && sumTime <= 0 )
    {
        return -1;
    }


    for( int i=imaxTime[cur]; i>=iminTime[cur]; i-- )
    {
        if( cur == d-1 )
        {
            result[cur] = i;
            int temp = compute( d, cur-1, sumTime-i, iminTime,imaxTime,result );
            if( temp == 1)
                return 1;

        }

        if( cur < d-1 && i <= result[cur+1] )
        {
            result[cur] = i;
            int temp = compute( d, cur-1, sumTime-i, iminTime,imaxTime,result );
            if( temp == 1)
                return 1;

        }           
    }
}


int main()
{

    /* ============================================================================*/
    /**
     * @Synopsis      
     * @Param   sample_num:     the array number of the sample_result and sample_tag, 
     *                          for example the title's sample_num is 2;
     * @Param   sample_result:  stores the list of the sample result 
     * @Param   sample_tag:     one column store the 'd' of a sample ,and the other
     *                          column stores the 'Yes' or 'No'
     * @Param   sample_num_temp:count the number of the samples
     */
    /* ============================================================================*/
    int sample_num = 30;
    int *sample_result = (int *)malloc( sizeof(int)*sample_num*30 );
    int *sample_tag = (int *)malloc( sizeof(int)*sample_num * 2 );
    int sample_num_temp = 0;

    int d;   // 1 <= d <= 30 
    int sumTime; // 0 <= sumTime <= 240

    while( cin >> d )
    {
        /* ============================================================================*/
        /**
         * @Synopsis     input one sample data 
         */
        /* ============================================================================*/

        cin >> sumTime ;

        int *iminTime = (int *)malloc( sizeof(int) * d );
        int *imaxTime = (int *)malloc( sizeof(int) * d );

        for( int i=0; i<d; i++ )
        {
            *( sample_result + sample_num_temp * 30 + i) = -1;
            cin >> iminTime[i];
            cin >> imaxTime[i];
        }

        if( sample_num_temp >= sample_num )
        {

            sample_result = (int *) realloc( sample_result, (sample_num+1)*30 * sizeof(int) );

            sample_tag = (int *)realloc(sample_tag, sizeof(int)*( (sample_num+1)*2 ) );

            sample_num++;
        }

        /* ============================================================================*/
        /**
         * @Synopsis  calculate and determine whether the d & sumTime can make a good schedule
         */
        /* ============================================================================*/
        *(sample_tag + sample_num_temp * 2 + 0 ) = d;
        *(sample_tag + sample_num_temp * 2 + 1 ) = compute( d, d-1, sumTime, iminTime, imaxTime, sample_result + sample_num_temp * 30 );
        sample_num_temp++;

        free( iminTime );
        free( imaxTime );

    }



    /* ============================================================================*/
    /**
     * @Synopsis  output all the sample result 
     */
    /* ============================================================================*/
    for(int i=0; i<sample_num_temp; i++ )
    {
        if( *( sample_tag+i*2+1 )==-1 )
        {           
            cout << "No " << endl;
        }
        else
        {
            cout << "Yes" << endl;

            d = *( sample_tag + i*2 + 0 );

            for( int j=0; j<d; j++ )
            {
                cout << *( sample_result + i*30 + j ) <<"  ";
            }
            cout<< endl;
        }
    }

    free( sample_result );
    free( sample_tag );

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值