当前位置:网站首页>JS - for cycle case: Horse grain
JS - for cycle case: Horse grain
2022-06-13 08:22:00 【tingkeaiii】
One 、 subject
One shipment to Malaysia 2 Stone grain , China Malaysia transportation 1 stone , Two little horses 1 stone , Use 100 A horse , shipment 100 Stone grain , How to allocate
Two 、 Source code and result diagram
for (var i = 0; i <= 50; i++) { /* Malaysia i*/
for (var j = 0; j <= 100; j++) { /* Zhongma j*/
for (var k = 66; k >= 0; k--) { /* The pony k*/
if ((parseInt(k / 2) + j + i * 2 == 100) && (i + j + k == 100)) {
console.log(' Malaysia needs ' + i + ' horse ' + '\n' + ' China and Malaysia need ' + j + ' horse ' + '\n' + ' The pony needs ' + k + ' horse ');
}
}
}
}
3、 ... and 、 Solution steps and ideas
( One ) Ideas :
Use three layers for loop , Each cycle represents a horse , Use if Statement to filter out the conditions that match the number of horses , And meet the conditions of grain quantity , If it meets the requirements, it will be output .
( Two ) The problem solving steps :
1. First of all, it is necessary to define the value range of each type of horse , For example, a big horse carries two bags of grain at a time , If the number of Damascus exceeds 50, The total transportation volume exceeds the standard ; Two ponies carry one bag , If the number of ponies is more than 67 Still small , No amount of Malaysia can carry all its grain , To determine i,j,k Value range of . Let's assume that Malaysia 0 only , Zhongma 0 only , The pony 66 As the first data , The pony k Decline .
for (var i = 0; i <= 50; i++)
for (var j = 0; j <= 100; j++)
for (var k = 66; k >= 0; k--)
2. Set in the innermost loop if Statement to filter conditions . The conditions are 2: The total amount of grain is 100, The number of horses adds up to 100, Both conditions are indispensable
(parseInt(k / 2) + j + i * 2 == 100) && (i + j + k == 100)
3. Output that meets this condition j,i,k result , And concatenate strings to make the result clearer
console.log(' Malaysia needs ' + i + ' horse ' + '\n' + ' China and Malaysia need ' + j + ' horse ' + '\n' + ' The pony needs ' + k + ' horse ');
Four 、 summary
The topic is to clarify the thinking and organization before moving the handwritten code , Thinking is far more important than writing code . This is the purpose of this exercise . In the process of doing questions , Write if Forget to write the number of horses in the conditional statement =100 This is the condition , Make the result incomplete . After writing the code, you have to look at the title again ! See if there is any omission in the requirements .
边栏推荐
- 如何通过JS动态删除table中的数据行(保留head)
- AcWing 1977. 信息中继(基环树,并查集)
- Buuctf web (III)
- Data accuracy problems caused by inconsistent data types during sum in MySQL
- Edge browser uses bdtab new tab plug-in (BD new tab)
- PHP PNG to webp
- 水仙花升级版(自幂数)
- Maternal and infant supplies wholesale industry uses management software to improve efficiency and realize cost reduction and efficiency increase
- DNS domain name resolution service
- 微服务项目搭建二:数据库设计
猜你喜欢
Dfinity (ICP) identity authentication and ledger quick start-3
Is signed or unsigned selected to create an integer field in MySQL? The answer is as follows:
Notes on development experience: TP5 exp query, SQL analysis, JQ, applet, right-click menu, Linux skills, shell skills, mysql, etc
从零开始-实现JpetStore网站-1-建立项目框架以及项目介绍
Word中批注的使用方法
Overall process analysis of account book operation in fabric0.6
Did decentralized digital identity
Phpexcel 10008 error resolution
Buuctf web (V)
DHCP principle and configuration
随机推荐
Data accuracy problems caused by inconsistent data types during sum in MySQL
How to efficiently manage commodities and inventory in the beverage wholesale industry
LVM management exercise
Is there any good management software to solve the problems faced by tea wholesalers
Shell脚本常用开发规范
HCIP_ OSPF irregular area
ERP basic data concept
Buuctf web (VII)
Edge浏览器使用BdTab新标签页插件(BD新标签页)
Word中批注的使用方法
Did decentralized digital identity
Which is the stronger fresh food distribution and sorting management system?
第115页的gtk+编程例子——最简单的进度条2附带使用Anjuta写gtk程序的步骤
微服务系统架构搭建一:环境搭建
Wechat upload picture material interface
Introduction to dfinity (ICP) -1
Founder of Starbucks: no longer open "public toilets" to non store consumers for safety reasons
ERP basic data Kingdee
Penetration problem (main directory, password explosion, database uploading Trojan horse)
平面合并(MATLAB)