汉子编码比字母编码长

Problem statement:

问题陈述:

Shivang is a blog writer and he is working on two websites simultaneously. He has to write two types of blogs which are:

Shivang是一位博客作家,他同时在两个网站上工作。 他必须写两种类型的博客:

  • Technical blogs for website_1: X of this type can be written in an hour. (X will be the input)

    website_1的技术博客 :此类X可以在一小时内编写。 ( X将作为输入)

  • Fun blogs for website_2: Y of this type can be written in an hour. (Y will be input)

    网站_2的趣味博客 :可以在一个小时内编写此类Y。 (将输入Y )

You are to help him to save time. Given N number of total blogs, print the minimum number of hours he needs to put for combination of both the blogs, so that no time is wasted.

您是要帮助他节省时间。 给定总数为N的博客,请打印他将两个博客组合在一起所需最少小时数 ,以免浪费时间。

    Input:
N: number of total blogs
X: number of Technical blogs for website_1 per hour
Y: number of Fun blogs for website_2 per hour
Output:
Print the minimum number of hours Shivang needs to write the
combination of both the blogs (total N).
If it is NOT possible, print "−1".

Example:

例:

    Input:
N: 33
X: 12
Y: 10
Output:
-1
Input:
N: 36
X: 10
Y: 2
Output:
6 (10*3+2*3)

Explanation:

说明:

    For the first case,
No combination is possible that's why output is -1.
For the second test case,
Possible combinations are 30 technical blogs (3 hours) + 6 fun blogs (3 hours)
20 technical blogs (2 hours) + 16 fun blogs (8 hours)
10 technical blogs (1 hours) + 26 fun blogs (13 hours)
0 technical blogs (0 hours) + 36 fun blogs (18 hours)
So, best combination is the first one which takes total 6 hours

The problem is basically solving equation,

问题基本上是解决方程式,

aX + bY = N where we need to find the valid integer coefficients of X and Y. Return a+b if there exists such else return -1.

aX + bY = N ,我们需要找到XY的有效整数系数。 如果存在则返回a + b ,否则返回-1

We can find a recursive function for the same too,

我们也可以找到相同的递归函数,

Say,

说,

    f(n) = minimum hours for n problems
f(n) = min(f(n-x) + f(n-y)) if f(n-x), f(n-y) is solved already

We can convert the above recursion to DP.

我们可以将上述递归转换为DP。

Solution Approach:

解决方法:

Converting the recursion into DP:

将递归转换为DP:

    1)  Create DP[n] to store sub problem results
2)  Initiate the DP with -1 except DP[0], DP[0]=0
3)  Now in this step we would compute values for DP[i]
4)  for i=1 to n
if i-x>=0 && we already have solution for i-x,i.e.,DP[i-x]!=-1
DP[i]=DP[i-x]+1;
end if
if i-y>=0 && we already have solution for i-y,i.e.,DP[i-y]!=-1)
if DP[i]!=-1
DP[i]=min(DP[i],DP[i-y]+1);
else
DP[i]=DP[i-y]+1;
End if
End if
End for
5)  Return DP[n]

C++ Implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;
int minimumHour(int n, int x, int y)
{int a[n + 1];
a[0] = 0;
for (int i = 1; i <= n; i++)
a[i] = -1;
for (int i = 1; i <= n; i++) {if (i - x >= 0 && a[i - x] != -1) {a[i] = a[i - x] + 1;
}
if (i - y >= 0 && a[i - y] != -1) {if (a[i] != -1)
a[i] = min(a[i], a[i - y] + 1);
else
a[i] = a[i - y] + 1;
}
}
return a[n];
}
int main()
{int n, x, y;
cout << "Enter total number of blogs, N:\n";
cin >> n;
cout << "Enter number of techical blogs, X:\n";
cin >> x;
cout << "Enter number of fun blogs, Y:\n";
cin >> y;
cout << "Minimum hour to be dedicated: " << minimumHour(n, x, y) << endl;
return 0;
}

Output

输出量

RUN 1:
Enter total number of blogs, N:
36
Enter number of techical blogs, X:
10
Enter number of fun blogs, Y:
2
Minimum hour to be dedicated: 6
RUN 2:
Enter total number of blogs, N:
33
Enter number of techical blogs, X:
12
Enter number of fun blogs, Y:
10
Minimum hour to be dedicated: -1

翻译自: https://www.includehelp.com/icp/letter-blog-writer-coding-problem-using-dynamic-programming.aspx

汉子编码比字母编码长

汉子编码比字母编码长_字母/博客作者编码问题(使用动态编程)相关推荐

  1. 汉子编码比字母编码长_编码比您想象的更具创意

    汉子编码比字母编码长 来自创作背景,当我告诉他们我是全栈开发人员时,人们常常会感到困惑. 似乎数学痴迷的程序员的污名在大多数社交圈中仍然相当突出. 但是,当我解释编码实际上是-非常有创造力时,困惑的表 ...

  2. 如何控制Yahoo! Slurp蜘蛛的抓取频度_国外博客资源站_百度空间

    如何控制Yahoo! Slurp蜘蛛的抓取频度_国外博客资源站_百度空间 如何控制Yahoo! Slurp蜘蛛的抓取频度 2009年08月13日 星期四 5:56 上周末豆瓣的阿北给我电话:最近你们雅 ...

  3. 图像分割 更好的细分_为博客文章创建更好图像的16种工具

    图像分割 更好的细分 Do you want to create better images for your blog posts? Images create a huge impact on h ...

  4. mysql 查询最顶级_2018顶级MySQL博客作者

    mysql 查询最顶级 We made a collection of the most popular MySQL bloggers, including a link to each indivi ...

  5. 一篇博客读懂设计模式之---动态代理与反射

    一篇博客读懂设计模式之---动态代理与反射 先来讲一下反射: 1 关于反射 反射最大的作用之一就在于我们可以不用在编译时就知道某个对象的类型,而在运行时通过提供完整的"包名+类名.class ...

  6. 基于python的个人博客系统的设计开题报告_个人博客设计实现开题报告

    现在的毕业设计的开题报告是很难写吗,以个人博客设计为课题的开题报告怎么写?看下文是yjbys小编搜集整理的关于开题报告的相关资料,希望对大家有所帮助! 1本课题所涉及的问题在国内(外)的研究现状综述: ...

  7. 【2017下集美大学软工1412班_助教博客】结对编程1——模块化

    作业要求 结对编程1-模块化 总成绩 计算方法: 百分数=总分/40*100 其中总分=个人作业+结对编程作业分数 个人项目成绩 (占原始总分的 20%) = ​ 每次作业成绩的累加,再把全班同学的最 ...

  8. 软工_个人博客作业3

    PART1 博文阅读感想 十几篇博客一气读下来,有一个词一直萦绕在我的脑海里--紧张!紧张!还是紧张! 首先这紧张来自于自己的学习方面.作为计算机系的科班出身,当然与生俱来就有一种优越感--我们是专业 ...

  9. mysql自带订阅功能_为博客提供订阅功能

    原标题:为博客提供订阅功能 以"为博客增加订阅功能"为例,来介绍用ajax提交表单的方法. 前端html5+js 欢迎使用ExASIC订阅服务 仅用于ExASIC最新文章通知,方便 ...

最新文章

  1. 曾大战LeCun的谷歌女性科学家,刚刚被Jeff Dean开除了!
  2. php循环遍历数组保存数据库,php数组循环遍历 - 与狼共舞红队的个人空间 - OSCHINA - 中文开源技术交流社区...
  3. 系列文章--oracle简单入门教程
  4. python核心编程电子版_python核心编程答案.pdf
  5. Mininet 系列实验(一)
  6. C#多线程编程系列(五)- 浅析C# Dictionary实现原理
  7. 不插电的计算机科学百度云,【精品】不插电的计算机科学.pdf
  8. SQL之COLLATE 子句 排序规则
  9. dataframe数组做元素_数组 array 矩阵 list 数据框 dataframe
  10. 基于tutk方案的p2p源码_基于JAVA的局域网文件共享平台P2P实训项目源码(毕业设计 课程设计)...
  11. Spring Boot @ SpringBootApplication,SpringApplication类
  12. 3D 数学(三角函数、坐标系、向量、摄像机跟随、点乘)
  13. Android原生PDF功能实现,掌握了这些Android高级工程师必备知识,
  14. java.sql.BatchUpdateException
  15. 电脑连接wifi总是断 手机正常 解决方案
  16. python上方菜单栏不见了_python tkinter-菜单栏
  17. Keil中部分Error Warning解决方法记录
  18. 基于 Python 使用 CNN 实现身份证汉字和数字识别
  19. 神经网络编程的34个案例,java调用神经网络模型
  20. 微信支付的统一下单小demo

热门文章

  1. mysql test数据库_mysql数据库test
  2. java异常标记_java.lang.RuntimeException:错误:0D0680A8:asn1编码例程:ASN1_CHECK_TLEN:错误的标记...
  3. java移动端接口测试_使用java如何进行接口测试
  4. c语言贪吃蛇_C语言贪吃蛇完整代码
  5. 计算机的屏幕约是16平方分米吗,小明的卧室有16平方分米对不对
  6. java.lang.class_关于Java.lang.Class的一些疑问
  7. python3.0下载用什么浏览器_无法让Python下载网页源代码:“不支持浏览器版本”...
  8. 【SSM面向CRUD编程专栏 1】Spring简介 xml配置文件 依赖注入 数据注入
  9. nginx日志分析脚本
  10. Problem D: 分数减法——结构体