使用JavaScript解答2018第九届蓝桥杯C/C++省赛A组试题
大三時(shí)參加過第七屆藍(lán)橋杯個(gè)人賽及團(tuán)隊(duì)賽,轉(zhuǎn)眼已經(jīng)兩年,最近看了看第九屆藍(lán)橋杯試題,打算用JavaScript實(shí)現(xiàn)一下。
題目1
標(biāo)題:分?jǐn)?shù)
1/1 + 1/2 + 1/4 + 1/8 + 1/16 + ….
每項(xiàng)是前一項(xiàng)的一半,如果一共有20項(xiàng),
求這個(gè)和是多少,結(jié)果用分?jǐn)?shù)表示出來。
類似:3/2 。當(dāng)然,這只是加了前2項(xiàng)而已。分子分母要求互質(zhì)。
注意:
需要提交的是已經(jīng)約分過的分?jǐn)?shù),中間任何位置不能含有空格。請(qǐng)不要填寫任何多余的文字或符號(hào)。
解答:
以下討論不使用數(shù)學(xué)公式,僅使用JavaScript編程解決的方法。
class Question1 {constructor () {this.result = ''}// 輔助方法->求兩個(gè)數(shù)的最小公倍數(shù)lcm (a, b) {const minNum = Math.min(a, b)const maxNum = Math.max(a, b)let temp = 0for (let i = minNum; i <= maxNum; i++) {temp = minNum * iif (temp % maxNum === 0) {return tempbreak}}}// 輔助方法->求兩個(gè)數(shù)的最大公約數(shù)gcd (a, b) {const minNum = Math.min(a, b)const maxNum = Math.max(a, b)for (let i = minNum; i > 0; i--) {if (maxNum % i === 0 && minNum % i ===0) {return i}}}// 主方法calculate () {let a = []let numerator = 1let denominator = 1// 將分?jǐn)?shù)推送進(jìn)二維數(shù)組,第一個(gè)元素為分子,第二個(gè)元素為分母for (let i = 0; i < 20; i++) {a[i] = [numerator, denominator]denominator *= 2}// 求所有分母最小公倍數(shù)let lcmResult = 0for (let i = 0; i < a.length; i++) {lcmResult = this.lcm(a[i][0], a[i][1])}// numeratorSum (通分后的分子共和,初始化為0)let numeratorSum = 0// 分子乘以(最小公倍數(shù)/分母),分母都變?yōu)閘cmResult,即:通分for (let i = 0; i < a.length; i++) {a[i][0] *= lcmResult / a[i][1]a[i][1] = lcmResultnumeratorSum += a[i][0]}// 求分子分母最大公約數(shù)const gcdResult = this.gcd(numeratorSum, lcmResult)// 計(jì)算結(jié)果if (gcdResult === 1) {this.result = numeratorSum + '/' + lcmResult} else {this.result = numeratorSum / gcdResult + '/' + lcmResult / gcdResult}}// 展示結(jié)果display () {console.log('結(jié)果為:' + this.result)} } const obj = new Question1() obj.calculate() obj.display()在node環(huán)境執(zhí)行此js文件,如:node Question1.js,可得到結(jié)果為:1048575/524288,如下:
解析請(qǐng)看源碼中的注釋,請(qǐng)從實(shí)例化對(duì)象部分(Question1類外)開始閱讀。
題目2
標(biāo)題:星期一
整個(gè)20世紀(jì)(1901年1月1日至2000年12月31日之間),一共有多少個(gè)星期一?
以下為本人使用JavaScript解答的源碼,相關(guān)解釋請(qǐng)查看代碼中的注釋:
使用node運(yùn)行此js文件,如:node Question2.js,可得到以下結(jié)果:
可見運(yùn)行結(jié)果為5217。
題目3
標(biāo)題:乘積尾零
如下的10行數(shù)據(jù),每行有10個(gè)整數(shù),請(qǐng)你求出它們的乘積的末尾有多少個(gè)零?
5650 4542 3554 473 946 4114 3871 9073 90 4329
2758 7949 6113 5659 5245 7432 3051 4434 6704 3594
9937 1173 6866 3397 4759 7557 3070 2287 1453 9899
1486 5722 3135 1170 4014 5510 5120 729 2880 9019
2049 698 4582 4346 4427 646 9742 7340 1230 7683
5693 7015 6887 7381 4172 4341 2909 2027 7355 5649
6701 6645 1671 5978 2704 9926 295 3125 3878 6785
2066 4247 4800 1578 6652 4616 1113 6205 3264 2915
3966 5291 2904 1285 2193 1428 2265 8730 9436 7074
689 5510 8243 6114 337 4096 8199 7313 3685 211
以下為本人使用JavaScript解答的源碼,相關(guān)解釋代碼中的注釋:
// Created by xiaoqiang on 29/05/2018.// 創(chuàng)建二維數(shù)組 let array = [[5650, 4542, 3554, 473, 946, 4114, 3871, 9073, 90, 4329], [2758, 7949, 6113, 5659, 5245, 7432, 3051, 4434, 6704, 3594], [9937, 1173, 6866, 3397, 4759, 7557, 3070, 2287, 1453, 9899], [1486, 5722, 3135, 1170, 4014, 5510, 5120, 729, 2880, 9019], [2049, 698 ,4582, 4346, 427, 646, 9742, 7340, 1230, 7683], [5693, 7015, 6887, 7381, 4172, 4341, 2909, 2027, 7355, 5649], [6701, 6645, 1671, 5978, 2704, 9926, 295, 3125, 3878, 6785], [2066, 4247, 4800, 1578, 6652, 4616, 1113, 6205, 3264, 2915], [3966, 5291, 2904, 1285, 2193, 1428, 2265, 8730, 9436, 7074], [689, 5510, 8243, 6114, 337 ,4096, 8199, 7313, 3685, 211]]// 零的總數(shù) let sum = 0 // 元素末尾為2的數(shù)量 let endWithTwo = 0 // 元素末尾為5的數(shù)量 let endWithFive = 0 for (let i = 0; i < 10; i++) {for (let j = 0; j < 10; j++) {while (array[i][j] % 10 === 0) {// 元素末尾為0,兩數(shù)乘積末尾必為0,直接加1sum += 1// 減去此元素末尾的一個(gè)0array[i][j] /= 10}while (array[i][j] % 2 === 0) {endWithTwo += 1array[i][j] /= 2}while (array[i][j] % 5 === 0) {endWithFive += 1array[i][j] /= 5}} } // 元素末尾為2的數(shù)量與元素末尾為5的數(shù)量比較,取小的,因?yàn)槎喑鰜砟莻€(gè)也沒對(duì)應(yīng)的和它相乘,不會(huì)產(chǎn)生0。 let select = endWithTwo > endWithFive ? endWithFive : endWithTwo // 將 sum += select console.log('乘積的末尾有' + sum + '個(gè)零')使用node運(yùn)行此js文件,如:node Question3.js,可得到以下結(jié)果:
可見結(jié)果為:31
有錯(cuò)誤歡迎指出,其他題目有空再來解答,晚安。
總結(jié)
以上是生活随笔為你收集整理的使用JavaScript解答2018第九届蓝桥杯C/C++省赛A组试题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Git pull 错误:fatal: r
- 下一篇: phpcms配置404的方法