{"id":5290,"date":"2023-01-05T12:23:29","date_gmt":"2023-01-05T04:23:29","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5290"},"modified":"2023-01-05T12:23:29","modified_gmt":"2023-01-05T04:23:29","slug":"%e5%ad%b8%e7%bf%92typescript%e4%b8%ad%e6%b1%82%e6%9c%80%e5%a4%a7%e5%85%ac%e5%9b%a0%e6%95%b8%e7%9a%84%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/typescript\/%e5%ad%b8%e7%bf%92typescript%e4%b8%ad%e6%b1%82%e6%9c%80%e5%a4%a7%e5%85%ac%e5%9b%a0%e6%95%b8%e7%9a%84%e6%96%b9%e6%b3%95\/","title":{"rendered":"\u5b78\u7fd2TypeScript\u4e2d\u6c42\u6700\u5927\u516c\u56e0\u6578\u7684\u65b9\u6cd5"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"TypeScript, \u9663\u5217, \u6700\u5927\u516c\u56e0\u6578, greatestCommonFactor\"><\/p>\n<h1>TypeScript \u9663\u5217\u7684\u6c42\u6700\u5927\u516c\u56e0\u6578(greatestCommonFactor)<\/h1>\n<p>TypeScript \u662f\u4e00\u7a2e JavaScript \u7684\u8d85\u96c6\uff0c\u5b83\u64c1\u6709 JavaScript \u7684\u6240\u6709\u529f\u80fd\uff0c\u4e26\u4e14\u63d0\u4f9b\u4e86\u984d\u5916\u7684\u7279\u6027\uff0c\u4f8b\u5982\u985e\u578b\u6aa2\u67e5\u548c\u975c\u614b\u5206\u6790\u3002TypeScript \u652f\u63f4\u9663\u5217\uff0c\u53ef\u4ee5\u7528\u4f86\u8a08\u7b97\u6700\u5927\u516c\u56e0\u6578\uff08greatestCommonFactor\uff09\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f\u6700\u5927\u516c\u56e0\u6578\uff1f<\/h2>\n<p>\u6700\u5927\u516c\u56e0\u6578\uff08greatestCommonFactor\uff09\u662f\u6307\u5169\u500b\u6216\u591a\u500b\u6578\u5b57\u5171\u6709\u7684\u6700\u5927\u7684\u6b63\u6574\u6578\u56e0\u6578\u3002\u4f8b\u5982\uff0c6 \u548c 8 \u7684\u6700\u5927\u516c\u56e0\u6578\u662f 2\uff0c\u56e0\u70ba 2 \u662f 6 \u548c 8 \u7684\u5171\u540c\u56e0\u6578\uff0c\u4e14\u662f\u6700\u5927\u7684\u3002<\/p>\n<h2>\u5982\u4f55\u4f7f\u7528 TypeScript \u8a08\u7b97\u6700\u5927\u516c\u56e0\u6578\uff1f<\/h2>\n<p>TypeScript \u652f\u63f4\u9663\u5217\uff0c\u53ef\u4ee5\u7528\u4f86\u8a08\u7b97\u6700\u5927\u516c\u56e0\u6578\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u7bc4\u4f8b\uff0c\u53ef\u4ee5\u7528\u4f86\u8a08\u7b97\u5169\u500b\u6578\u5b57\u7684\u6700\u5927\u516c\u56e0\u6578\uff1a<\/p>\n<pre class=\"brush: typescript\">\nfunction greatestCommonFactor(a: number, b: number): number {\n    let arrA = [];\n    let arrB = [];\n    let result = 0;\n\n    \/\/ \u5c07 a \u8207 b \u7684\u56e0\u6578\u653e\u5165\u9663\u5217\n    for (let i = 1; i <= a; i++) {\n        if (a % i == 0) {\n            arrA.push(i);\n        }\n    }\n    for (let i = 1; i <= b; i++) {\n        if (b % i == 0) {\n            arrB.push(i);\n        }\n    }\n\n    \/\/ \u6bd4\u8f03\u5169\u500b\u9663\u5217\uff0c\u627e\u51fa\u6700\u5927\u516c\u56e0\u6578\n    for (let i = 0; i < arrA.length; i++) {\n        for (let j = 0; j < arrB.length; j++) {\n            if (arrA[i] == arrB[j]) {\n                result = arrA[i];\n            }\n        }\n    }\n\n    return result;\n}\n\nlet a = 6;\nlet b = 8;\nlet gcf = greatestCommonFactor(a, b);\nconsole.log(`${a} \u548c ${b} \u7684\u6700\u5927\u516c\u56e0\u6578\u662f ${gcf}`);\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u9996\u5148\u5b9a\u7fa9\u4e86\u4e00\u500b\u51fd\u5f0f <code>greatestCommonFactor()<\/code>\uff0c\u5b83\u63a5\u53d7\u5169\u500b\u53c3\u6578\uff0c\u5206\u5225\u662f\u8981\u6c42\u6700\u5927\u516c\u56e0\u6578\u7684\u5169\u500b\u6578\u5b57\u3002\u63a5\u8457\uff0c\u6211\u5011\u5c07\u5169\u500b\u6578\u5b57\u7684\u56e0\u6578\u653e\u5165\u5169\u500b\u9663\u5217\u4e2d\uff0c\u7136\u5f8c\u6bd4\u8f03\u5169\u500b\u9663\u5217\uff0c\u627e\u51fa\u6700\u5927\u516c\u56e0\u6578\u3002\u6700\u5f8c\uff0c\u6211\u5011\u5c07\u7d50\u679c\u8f38\u51fa\u5230\u63a7\u5236\u53f0\u3002<\/p>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u53ef\u4ee5\u7528\u4f86\u8a08\u7b97\u5169\u500b\u6578\u5b57\u7684\u6700\u5927\u516c\u56e0\u6578\uff0c\u4f46\u662f\u5982\u679c\u8981\u8a08\u7b97\u591a\u500b\u6578\u5b57\u7684\u6700\u5927\u516c\u56e0\u6578\uff0c\u5c31\u9700\u8981\u6539\u5beb\u4e00\u4e0b\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<pre class=\"brush: typescript\">\nfunction greatestCommonFactor(arr: number[]): number {\n    let result = 0;\n\n    \/\/ \u5c07\u9663\u5217\u4e2d\u7684\u6bcf\u500b\u6578\u5b57\u7684\u56e0\u6578\u653e\u5165\u9663\u5217\n    let arrA = [];\n    for (let i = 0; i < arr.length; i++) {\n        let tempArr = [];\n        for (let j = 1; j <= arr[i]; j++) {\n            if (arr[i] % j == 0) {\n                tempArr.push(j);\n            }\n        }\n        arrA.push(tempArr);\n    }\n\n    \/\/ \u6bd4\u8f03\u9663\u5217\uff0c\u627e\u51fa\u6700\u5927\u516c\u56e0\u6578\n    for (let i = 0; i < arrA[0].length; i++) {\n        let isCommonFactor = true;\n        for (let j = 0; j < arrA.length; j++) {\n            if (arrA[j].indexOf(arrA[0][i]) == -1) {\n                isCommonFactor = false;\n                break;\n            }\n        }\n        if (isCommonFactor) {\n            result = arrA[0][i];\n        }\n    }\n\n    return result;\n}\n\nlet arr = [6, 8, 12];\nlet gcf = greatestCommonFactor(arr);\nconsole.log(`${arr} \u7684\u6700\u5927\u516c\u56e0\u6578\u662f ${gcf}`);\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u51fd\u5f0f <code>greatestCommonFactor()<\/code>\uff0c\u5b83\u63a5\u53d7\u4e00\u500b\u53c3\u6578\uff0c\u662f\u4e00\u500b\u5305\u542b\u591a\u500b\u6578\u5b57\u7684\u9663\u5217\u3002\u63a5\u8457\uff0c\u6211\u5011\u5c07\u9663\u5217\u4e2d\u7684\u6bcf\u500b\u6578\u5b57\u7684\u56e0\u6578\u653e\u5165\u9663\u5217\uff0c\u7136\u5f8c\u6bd4\u8f03\u9663\u5217\uff0c\u627e\u51fa\u6700\u5927\u516c\u56e0\u6578\u3002\u6700\u5f8c\uff0c\u6211\u5011\u5c07\u7d50\u679c\u8f38\u51fa\u5230\u63a7\u5236\u53f0\u3002<\/p>\n<p>\u4f7f\u7528 TypeScript \u7684\u9663\u5217\uff0c\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u8a08\u7b97\u6700\u5927\u516c\u56e0\u6578\uff0c\u4e0d\u8ad6\u662f\u5169\u500b\u6578\u5b57\u9084\u662f\u591a\u500b\u6578\u5b57\u3002\u9019\u6a23\u53ef\u4ee5\u8b93\u6211\u5011\u66f4\u5feb\u901f\u5730\u8a08\u7b97\u51fa\u6700\u5927\u516c\u56e0\u6578\uff0c\u4e26\u4e14\u53ef\u4ee5\u66f4\u5bb9\u6613\u5730\u7dad\u8b77\u7a0b\u5f0f\u78bc\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>TypeScript \u652f\u63f4\u9663\u5217\uff0c\u53ef\u4ee5\u7528\u4f86\u8a08\u7b97\u6700\u5927\u516c\u56e0\u6578\uff08greatestCommonFactor\uff09\u3002\u4f7f\u7528 TypeScript \u7684\u9663\u5217\uff0c\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u8a08\u7b97\u6700\u5927\u516c\u56e0\u6578\uff0c\u4e0d\u8ad6\u662f\u5169\u500b\u6578\u5b57\u9084\u662f\u591a\u500b\u6578\u5b57\u3002\u9019\u6a23\u53ef\u4ee5\u8b93\u6211\u5011\u66f4\u5feb\u901f\u5730\u8a08\u7b97\u51fa\u6700\u5927\u516c\u56e0\u6578\uff0c\u4e26\u4e14\u53ef\u4ee5\u66f4\u5bb9\u6613\u5730\u7dad\u8b77\u7a0b\u5f0f\u78bc\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39TypeScript\u4e2d\u6c42\u53d6\u6700\u5927\u516c\u56e0\u6578\u7684\u65b9\u6cd5\uff0c\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528greatestCommonFactor\u51fd\u6578\uff0c\u4ee5\u53ca\u5982\u4f55\u5229\u7528\u5b83\u4f86\u89e3\u6c7a\u554f\u984c\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[187],"tags":[186],"class_list":["post-5290","post","type-post","status-publish","format-standard","hentry","category-typescript","tag-typescript"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1nk","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5290","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=5290"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5290\/revisions"}],"predecessor-version":[{"id":5291,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5290\/revisions\/5291"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}