首页 > 试题广场 >

出专辑

[编程题]出专辑
  • 热度指数:10809 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

你作为一名出道的歌手终于要出自己的第一份专辑了,你计划收录 n 首歌而且每首歌的长度都是 s 秒,每首歌必须完整地收录于一张 CD 当中。每张 CD 的容量长度都是 L 秒,而且你至少得保证同一张 CD 内相邻两首歌中间至少要隔 1 秒。为了辟邪,你决定任意一张 CD 内的歌数不能被 13 这个数字整除,那么请问你出这张专辑至少需要多少张 CD ?


输入描述:

每组测试用例仅包含一组数据,每组数据第一行为三个正整数 n, s, L。 保证 n ≤ 100 , s ≤ L ≤ 10000



输出描述:

输出一个整数代表你至少需要的 CD 数量。

示例1

输入

7 2 6

输出

4
import sys
import math
 
class Solution:
    def get_cd_num(self, n, s, l):
        x = int((l+1) / (s+1))
        if n >= x and x % 13 == 0:
            x -= 1
        elif n < x and n % 13 == 0:
            print(2)
            return
        num = math.ceil(n/x)
        real = int(n/x)
        left = n - real*x
        if left and x == left+1 and left % 13 == 0:
            print(num+1)
        else:
            print(num)
            
if __name__ == '__main__':
    args = list(map(int, sys.stdin.readline().split()))
    solution = Solution()
    solution.get_cd_num(*args)

发表于 2018-07-12 20:49:11 回复(0)
更多回答
题目:你作为一名出道的歌手终于要出自己的第一份专辑了,你计划收录 n 首歌而且每首歌的长度都是 s 秒,每首歌必须完整地收录于一张 CD 当中。每张 CD 的容量长度都是 L 秒,而且你至少得保证同一张 CD 内相邻两首歌中间至少要隔 1 秒。为了辟邪,你决定任意一张 CD 内的歌数不能被 13 这个数字整除,那么请问你出这张专辑至少需要多少张 CD ?

<code lang="js">
while(input = readline()){
  var lines = input.split(" ");
  var n = parseInt(lines[0]);
  var s = parseInt(lines[1]);
  var L = parseInt(lines[2]);
  var x;  //设每张CD收录x首歌
  x=(L+1)/(s+1);
  x = Math.floor(x);  //求出x
  var ans = Math.ceil(n/x);  //求出非特殊条件下的结果
  if( x%13 == 0 ){
    x = x-1;  //如果x是13倍数,那么每张CD都要少收录1首
    ans = Math.ceil(n/x);
  }
  if( n < x && n%13 == 0 ){
    ans = 2;//如果仅需要一张CD但是收录了13倍数的歌曲需要加一张
  }
  if( (n-(ans-1)*x)%13 == 0 && x-(n-(ans-1)*x) == 1 ){
    //如果最后一张CD是13的倍数【(n-(ans-1)*x)%13 == 0】,可以把前一张拿过来一首,但是拿过来后前一张又可能会是13的倍数
    //这时候也需要再加一张CD,比如这样的情况“14 14 13” “27 27 27 26”
    //这样情况的条件是最后一个必须比前一个少1【x-(n-(ans-1)*x) == 1】
    //所以满足上述两项条件的时候总CD需要加1
    ans += 1;
  }
  print(ans);
}
</code>

编辑于 2018-05-26 10:36:51 回复(0)
var lines = readline()
lines = lines[0].split(' ')
//获得数据
var n = parseInt(lines[0])
var s = parseInt(lines[1])
var l = parseInt(lines[2])
//1.判断一张光盘能放几首歌,因为间隔为1s,所以给每首歌***
var sLength = s + 1
//向下取整
var num = Math.floor(l / sLength)
//最后一首歌不用间隔,需要判断
if((l+1)%sLength === 0){
    num++
}
// 不能出现13
// (1).刚好为13
if(num == 13){
    num--
}
 
//2.计算总共放几张光盘
var CDNum = Math.floor(n/num)
if(n%num != 0){
    CDNum++
}
// (2).大于13,那么就只有可能发生在最后一张光盘身上
if(num > 13){
    if(n % num == 13){
        CDNum++
    }
}
console.log(CDNum)

发表于 2019-06-07 13:08:18 回复(2)
import java.util.*;
public class Main {

        public static void main(String[] args) {
            // 题目的输入时单行三大int数
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            int s = sc.nextInt();
            int L = sc.nextInt();
            System.out.println(countCD(n, s, L));
        }

        public static int countCD(int n, int s, int L) {
            int res = 0;
            // count表示每张CD的最多存歌数
            int count = (int) (L+1) / (s+1);
            count = Math.min(count, n);
            // 如果CD存的歌被13整除了,那就少放一首
            if (count % 13 == 0) {
                count--;
            }
            // n首歌,每个CD有count首曲子,则共有res个CD
            res = n / count;
            // 余数是多出来没放下的歌
            int yu = n % count;
            if (yu != 0) {
                // 有余数证明有歌没放下,就加一个CD呗
                res++;
                // 不管怎么找都要避免因为有余数所有单独给的一个CD,
                // 保证这个CD不能放13的倍数首歌曲
                // 且此时每个CD放的曲子数count和剩下的yu首曲子只差1首
                // 如果不是只差一首,而是还有更多的空余,
                // 那就完全可以从其他的一张CD拿过来一首,让它不是13的倍数即可
                if (yu % 13 == 0 && (count - yu) == 1) {
                    res++;
                }
            }
            return res;
        }
}

发表于 2021-07-18 21:53:44 回复(0)
const [n, s, L] = readline().split(' ').map(item => parseInt(item, 10));
// s > L
if (s > L || n === 0) {
    print(0);
}
// 一张cd只能容纳一首歌
if (L >= s && L < 2*s + 1) {
    print(n);
}
if (L >= 2* s + 1) {
    // 每张CD容纳的歌数
    let per = Math.floor((L+1)/(s+1));
    if (per % 13 === 0) {
        per = per - 1;
    }
    // 结果
    let result = Math.ceil(n / per);
    // 特例:一张CD可以容纳所有的歌时,歌数能被13整除
    if (n <= per && n % 13 === 0) result = 2;
    print(result);
}
发表于 2020-12-12 22:47:42 回复(0)
js v8
while (line = readline()) {
    var arr = line.split(' ')
    var n = parseInt(arr[0]), s = parseInt(arr[1]), ll = parseInt(arr[2])
    var cd = 1, count = 0, l = ll, r = Math.floor((ll - s) / (s + 1) + 1)
    while (n > 0) {
        if (l >= s) {
            if (((count + 1) % 13 !== 0)) {
                if (count === 0) {
                    l -= s;
                } else {
                    l -= (s + 1);
                }
                count++
                n--
            } else if ((l > (2 * s + 2)) && (n >= 2) || ((n < r) && (l > s + 1)) && (cd !== 1)) {
                l -= (2 * (s + 1));
                count = count + 2
                n -= 2
            } else {
                cd = cd + 1
                count = 0
                l = ll
            }
        } else {
            cd = cd + 1
            count = 0
            l = ll
        }
    }
    console.log(cd)
}


发表于 2020-02-14 19:04:17 回复(0)
import java.util.Scanner;

public class JRTT2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		while (scanner.hasNext()) {
			int n = scanner.nextInt();
			int s = scanner.nextInt();
			int L = scanner.nextInt();
			System.out.println(getNum(n, s, L));
		}
	}

	public static int getNum(int n, int s, int L) {
		int maxPerCD = (L + 1) / (s + 1); // 每张CD最大的存歌量
		int num = 0;
		if (maxPerCD > n) { // 每张CD最大的存歌量大于歌数
			if (n % 13 == 0) {
				return 2;
			} else {
				return 1;
			}
		}
		if (maxPerCD % 13 == 0) { // CD最大的存歌量能被13整除就自减1
			maxPerCD--;
		}
		while (n / maxPerCD != 0) {
			n -= maxPerCD;
			num++;
		}
		if (n != 0) { // 此时的n表示剩余歌的数量
			if (n % 13 == 0) {
				if (maxPerCD - n == 1) {//剩余歌的数量被13整除,且与最大存歌量只相差1个的时候,此时不能通过交换来节省。只有再加一个CD
					num += 2;
				} else { //通过交换来节省所需CD的数量
					num++;
				}
			} else {
				num++;
			}
		}
		return num;
	}
}


发表于 2016-09-30 22:30:30 回复(0)
DHJ头像 DHJ
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
using namespace std;
  
intmain() {
    doublen, s, l;
    while(cin >> n >> s >> l) {
        if(l != s) s++;
        intcnt = floor(l/s);
        if((int)cnt % 13== 0) cnt--;
        intans = ceil(n / cnt);
        cout << (min((int)n,cnt) % 13== 0? ans + 1: ans) << endl;
    }
    return0;
}
发表于 2016-09-04 14:38:38 回复(0)
#include<iostream>
using namespace std;
int main(){
    int n, s, l;
    while (cin >> n >> s >> l){
        int max_num_pre_CD = 0;
        for (; max_num_pre_CD*s + max_num_pre_CD - 1 <= l; ++max_num_pre_CD);//求得CD最大容纳歌曲数量
        --max_num_pre_CD;
        if (max_num_pre_CD % 13 == 0)
            --max_num_pre_CD;
        int numOfCD = n / max_num_pre_CD;
        int left = n%max_num_pre_CD;
        if (left != 0){//如果left没有剩下,也就直接输出numOfCD即可,left不为零就需要考虑left%13是否为0了
            ++numOfCD;//先算上left的歌要占的不足一张的CD数。
            if(left%13 == 0){ //有两种情况left必须要另拆成两张CD存
             	if(numOfCD == 1)//加上left就一张,没有前一张满的可以用来得补,left得拆成两张CD
                	++numOfCD;
                if(numOfCD>1 && (max_num_pre_CD-1)%13 == 0)//有前一张满的,但是不能补,left也得拆成两张CD
                    ++numOfCD;
            }      
        }
        cout << numOfCD << endl;
    }
 
 
    return 0;
}

编辑于 2016-08-09 23:07:48 回复(1)
#include<iostream>
using namespace std;
 
intCDNUM(intn, ints, intl) {
 
    intrst = 0;
    intx = (l + 1) / (s + 1);
    if(n <= x) {
        if(n % 13== 0) return2;
        elsereturn1;
    }
 
    intpre, suff,remain;
    if(x % 13== 0) {
        pre = n / (x - 1);
        remain = n%(x-1);      
    }
    else{
        pre = n / x;
        remain = n%x;
    }
 
    if(remain>0) {
        if(remain % 13== 0) {
            if(x - remain == 1) // 这里是问题的关键,因为当余数为13所整除时,看是否可以并接之前完整的CD,仅在CD的容量和余数差距为1时候,需要再加一个CD
                suff = 2;
            else
                suff = 1;
        }
        else
            suff = 1;
    }
    else
        suff = 0;
 
    return pre + suff;
 
}
 
 
intmain() {
 
    intn, s, l;
    cin >> n >> s >> l;
    cout<<CDNUM(n, s, l)<<endl;
    return0;
}

发表于 2016-08-08 15:18:51 回复(0)
#include<iostream>
#include<cmath>

using namespace std;

int main()
    {
    int n,s,l;
    while(cin>>n>>s>>l)
        {
        int temp = 0;
        temp = l/s;
        while(temp>0)
            {
            if(temp*s+(temp-1)>l)
                temp--;
            else
                break;
                        }
        temp = temp<n?temp:n;
        while(temp%13==0)
            temp--;
        double res= (double)n/temp;
        int ress = ceil(res);
        int downres = floor(res);
        if(ress>1&&(ress*temp-n>2))//此时属于可以在最后两张CD互相调整一下数量
            ;
        else if((n-downres*temp)!=0&&(n-downres*temp)%13==0)
            ress++;
        cout<<ress<<endl;
    }
}

编辑于 2016-08-04 16:52:25 回复(0)
import sys
for line in sys.stdin:
    data = line.split()
    n = int(data[0]) 
    s = int(data[1]) 
    L = int(data[2]) 
    
    num_in_one = L//(s+1)
    if num_in_one == 0:
        num_in_one =1
    
    if num_in_one > n:
        if n%13==0:
            print 2
            continue
        else:
            print 1
            continue
    else :
        if num_in_one%13==0:
            num_in_one = num_in_one-1
        cd_need = n//num_in_one
        if n%num_in_one !=0:
            cd_need = cd_need + 1
        print cd_need





发表于 2016-08-02 16:20:48 回复(0)
import java.util.Scanner;

public class Main{

public Main() {
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int n = scanner.nextInt();
int s = scanner.nextInt();
int L = scanner.nextInt();
int size = getMaxSize(s, n, L);
int reslut = n % size == 0 ? n / size : n / size + 1;
if (n % size != 0 && (n % size) % 13 == 0 && n % size == size - 1)
reslut += 1;
System.out.println(reslut);
}
scanner.close();
}

public static int getMaxSize(int s, int n, int L) {
int temp = (L + 1) / (s + 1) > n ? n : (L + 1) / (s + 1);
return temp % 13 == 0 ? temp - 1 : temp;
}
}

发表于 2016-07-30 17:21:31 回复(0)
function do_something(n, s, l){
var count=0;
if(n<=100&&s<=l&&l<=10000){
var max=Math.ceil(l/s);
var t=0;
for(i=max;i>=1;i--){ //每张CD最多收录的歌曲数量
t=s*i+(i-1);
if(l>=t){
break;
}
}
if(n<i)i=n; //每张CD最多收录的歌曲数量大于歌曲总数量,赋值统一操作
if (i % 13 == 0) { //歌曲总数量可被13整除
i=i-1;
}
count = Math.ceil(n / i); //计算所需 CD的数量
// 歌曲总数量除每张CD收录的歌曲数量的余数可被13整除,并且, 每张CD收录的歌曲数量减一可被13整除
var s= n%i;
if( s %13==0&&i-13==1)count=count+1;
}
console.log(count);
return count;
}

function do_something(n, s, l) {
    var count = 0; //CD的数量
    if (n <= 100 && s <= l && l <= 10000) {
        var max = Math.ceil(l / s);
        var t = 0;
        for (i = max; i >= 1; i--) { //每张CD最多收录的歌曲数量
            t = s * i + (i - 1);
            if (l >= t) {
                break;
            }
        }
        if (n < i) { //每张CD最多收录的歌曲数量大于歌曲总数量
            i = n;
            if (i % 13 == 0) { //歌曲总数量可被13整除
                i = i - 1;
            }
            count = Math.ceil(n / i);
        } else { //每张CD最多收录的歌曲数量小于于歌曲总数量
            if (i % 13 == 0) { //每张CD最多收录的歌曲数量可被13整除
                i = i - 1;
                count = Math.ceil(n / i);
            } else {
                count = Math.ceil(n / i);
                var s = n % i;
                if (s % 13 == 0) { //歌曲总数量除每张CD最多收录的歌曲数量的余数可被13整除
                    if (i - s == 1) {
                        count = count + 1;
                    }
                }
            }
        }
    }
    console.log(count);
    return count;
}
编辑于 2016-08-10 19:09:59 回复(8)
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
	int n, s, l;
	while (cin >> n >> s >> l)
	{
		int sum = l / (s + 1);
		if (l - sum * (s + 1) == s)
			sum++;
		if (sum % 13 == 0)
			sum--;
		int res = n / sum;
		int x = n - res * sum;
		if (n % sum != 0)
		{
			if (x % 13 == 0)
			{
				if ((sum - 1) % 13 == 0 || res == 0)
					res += 2;
				else res += 1;
			}
			else
				res += 1;
		}
		cout << res << endl;
	}
	return 0;
}

发表于 2016-07-23 14:07:57 回复(0)
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int res = 0;
        	int n = in.nextInt();
            int s = in.nextInt();
            int l = in.nextInt();
            int c;
            c = (l+1)/(s+1);
            if((c%13) == 0) c--;
            if(n%c == 0){
                res =n/c;
            }
            else{
                res = n/c + 1;
            }
            if((n%13 == 0) && n <= c) res++;
            if(n%c == c -1 && n%c!=0 && n%c%13==0) res++;
            System.out.println(res);
        }
    }
}

发表于 2016-05-07 20:39:07 回复(0)
import java.util.*;

public class Main{
    
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        while(in.hasNext()){
            int n = in.nextInt();
            int s = in.nextInt();
            int l = in.nextInt();
            int count = (l+1)/(s+1);
            count = Math.min(n, count);
            if(count%13==0){
                count--;
            }
            int sum = n/count;
            int yu = n%count;
			if(yu!=0){
            	sum++;
            	if(yu%13==0&&(count-yu)==1){//查看最后最后一张专辑的情况
            		sum++;
            	}
            }
            System.out.println(sum);
        }
    }
}

    
编辑于 2016-06-03 23:22:00 回复(20)
#include <iostream>
 
using namespace std;
 
int main() {
    long long n, s, l;//n首歌,每首s秒,CD容量l秒
    long long i, z;//每张CD存i首歌,需要z张
    while(scanf("%lld%lld%lld", &n, &s, &l) != EOF) {
        for(i=1; i*s+i-1<=l; i++);//计算1张CD可以存i首歌
        i--;
        z=1;
 
        if(i % 13 == 0) {//每张CD不能存13的倍数首歌
            i--;
        }
 
        for(z=1; i*z<n; z++);//计算需要多少张CD
 
        if((n % 13 == 0) && (n <= i)) {//针对n是13的倍数且1张CD就可以存所有歌曲的情况
            z++;
        }
        if((i - 1) % 13 == 0){//针对最后i-1可能是13的倍数
            z++;
        }
        if((n == 1) && (s == 1) && (l == 1)){//单独考虑1,1,1
            z=1;
        }
        cout << z << endl;
    }
    return 0;
}
编辑于 2016-08-05 10:08:28 回复(8)
process.stdin.resume();
process.stdin.setEncoding('ascii');

var input = "";
var input_array = "";
var n;
var s;
var l;

process.stdin.on('data', function (data) {
    input += data;
});

function do_something(n, s, l){
	//你的代码
    var single = Math.floor(l / (s + 1));
    if (single % 13 === 0) single--;
    if (s === l) single = 1;
    
    var res = Math.ceil(n / single)
    
    if (res === 1 && n % 13 === 0) res++;
    
    console.log(res)
}

process.stdin.on('end', function () {
    input_array = input.split("\n");
    var nLine = 0;
    while(nLine < input_array.length){
        var line = input_array[nLine++].trim();
        if(line === ''){
            continue;
        }
        var input_arrays = line.split(' ');
        n = +input_arrays[0];
        s = +input_arrays[1];
        l = +input_arrays[2];
     
		do_something(n, s, l);
        
    }
});

发表于 2016-09-23 11:36:32 回复(1)
1.首先判断一张cd可以容纳几首歌(n)
2.判断给出的歌曲数n1与n的大小
3.如果n大,直接判断n1是否能被13整除,能的话,cd数++;如果n1大,就需要判断能否被13整除,能的话,n-1;然后cd数=n1/n,可以判断是否有余数,有的话cd数+1;
发表于 2016-04-29 12:38:12 回复(6)
一道简单直观的动态规划题目,仅仅需要一个下标。
首先是下标意义:
dp[i]表示有i首歌时需要的专辑数。
其次是递推关系式:
其中,数组p存放每张专辑可以存放的歌曲数量,满足条件:不是13的倍数,不大于n,时长不大于L
最后是初始化:
dp[p[t]]为1,其它为101
代码如下:
#include <iostream>
#include <vector>

using namespace std;

int main() {
	int n, s, L;
	int dp[101] = { 0 };
	vector<int>p(0);
	cin >> n >> s >> L;
	for (int i = 1; i*(s + 1) - 1 <= L; ++i) {
		if (i > n)break;
		if (i % 13) {
			p.push_back(i);
			dp[i] = 1;
		}
	}
	for (int i = 1; i <= n; ++i) {
		if (dp[i] == 1)continue;
		dp[i] = 101;
		for (int t = 0; t < p.size(); ++t) {
			if (p[t] > i)break;
			if (dp[i - p[t]] + 1 < dp[i])
				dp[i] = dp[i - p[t]] + 1;
		}
	}
	cout << dp[n];
	return 0;
}


发表于 2021-08-22 16:40:52 回复(0)