{"id":393,"date":"2023-03-19T15:32:18","date_gmt":"2023-03-19T15:32:18","guid":{"rendered":"https:\/\/yunshangtulv.com.cn\/?p=393"},"modified":"2023-03-19T15:32:18","modified_gmt":"2023-03-19T15:32:18","slug":"%e5%be%aa%e7%8e%af%e8%af%a6%e8%a7%a3%e5%8f%8a%e5%ba%94%e7%94%a8%e4%b8%be%e4%be%8b","status":"publish","type":"post","link":"https:\/\/yunshangtulv.com.cn\/?p=393","title":{"rendered":"\u5faa\u73af\u8be6\u89e3\u53ca\u5e94\u7528\u4e3e\u4f8b"},"content":{"rendered":"<p>R\u8bed\u8a00\u4e2d\u6709\u5f88\u591a\u79cd\u5faa\u73af\u8bed\u53e5\uff0c\u5305\u62ecfor\u5faa\u73af\u3001while\u5faa\u73af\u3001repeat\u5faa\u73af\u7b49\u3002\u8fd9\u4e9b\u5faa\u73af\u8bed\u53e5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u7a0b\u5e8f\u4e2d\u591a\u6b21\u6267\u884c\u540c\u4e00\u6bb5\u4ee3\u7801\uff0c\u4ee5\u5b9e\u73b0\u4e00\u4e9b\u91cd\u590d\u6027\u7684\u4efb\u52a1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u57fa\u672c\u7684R\u8bed\u8a00\u5faa\u73af\u8bed\u53e5\u6559\u7a0b\uff1a<\/p>\n<h2>for\u5faa\u73af<\/h2>\n<p>for\u5faa\u73af\u7528\u4e8e\u5728\u56fa\u5b9a\u6b21\u6570\u7684\u60c5\u51b5\u4e0b\u5faa\u73af\u6267\u884c\u4e00\u6bb5\u4ee3\u7801\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-R\"><span class=\"hljs-keyword\">for<\/span> <span class=\"hljs-punctuation\">(<\/span>variable <span class=\"hljs-keyword\">in<\/span> sequence<span class=\"hljs-punctuation\">)<\/span> <span class=\"hljs-punctuation\">{<\/span><br \/>\n   statement<br \/>\n<span class=\"hljs-punctuation\">}<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<p>\u5176\u4e2d\uff0csequence\u662f\u4e00\u4e2a\u5411\u91cf\u6216\u8005\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0cvariable\u662f\u5faa\u73af\u4e2d\u7684\u8ba1\u6570\u5668\u53d8\u91cf\uff0cstatement\u662f\u9700\u8981\u5faa\u73af\u6267\u884c\u7684\u4ee3\u7801\u6bb5\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u53ef\u4ee5\u8ba1\u7b971\u523010\u4e4b\u95f4\u7684\u6240\u6709\u6574\u6570\u7684\u548c\uff1a<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-R\"><span class=\"hljs-built_in\">sum<\/span> <span class=\"hljs-operator\">&lt;-<\/span> 0<br \/>\n<span class=\"hljs-keyword\">for<\/span> <span class=\"hljs-punctuation\">(<\/span>i <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-number\">1<\/span><span class=\"hljs-operator\">:<\/span><span class=\"hljs-number\">10<\/span><span class=\"hljs-punctuation\">)<\/span> <span class=\"hljs-punctuation\">{<\/span><br \/>\n  <span class=\"hljs-built_in\">sum<\/span> <span class=\"hljs-operator\">&lt;-<\/span> <span class=\"hljs-built_in\">sum<\/span> <span class=\"hljs-operator\">+<\/span> i<br \/>\n<span class=\"hljs-punctuation\">}<\/span><br \/>\nprint<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-built_in\">sum<\/span><span class=\"hljs-punctuation\">)<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<h2>while\u5faa\u73af<\/h2>\n<p>while\u5faa\u73af\u7528\u4e8e\u5728\u6761\u4ef6\u4e3a\u771f\u7684\u60c5\u51b5\u4e0b\u91cd\u590d\u6267\u884c\u4e00\u6bb5\u4ee3\u7801\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-R\"><span class=\"hljs-keyword\">while<\/span> <span class=\"hljs-punctuation\">(<\/span>condition<span class=\"hljs-punctuation\">)<\/span> <span class=\"hljs-punctuation\">{<\/span><br \/>\n   statement<br \/>\n<span class=\"hljs-punctuation\">}<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<p>\u5176\u4e2d\uff0ccondition\u662f\u4e00\u4e2a\u903b\u8f91\u8868\u8fbe\u5f0f\uff0cstatement\u662f\u9700\u8981\u5faa\u73af\u6267\u884c\u7684\u4ee3\u7801\u6bb5\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u53ef\u4ee5\u8ba1\u7b971\u523010\u4e4b\u95f4\u7684\u6240\u6709\u6574\u6570\u7684\u548c\uff1a<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-R\"><span class=\"hljs-built_in\">sum<\/span> <span class=\"hljs-operator\">&lt;-<\/span> 0<br \/>\ni <span class=\"hljs-operator\">&lt;-<\/span> 1<br \/>\n<span class=\"hljs-keyword\">while<\/span> <span class=\"hljs-punctuation\">(<\/span>i <span class=\"hljs-operator\">&lt;=<\/span> <span class=\"hljs-number\">10<\/span><span class=\"hljs-punctuation\">)<\/span> <span class=\"hljs-punctuation\">{<\/span><br \/>\n  <span class=\"hljs-built_in\">sum<\/span> <span class=\"hljs-operator\">&lt;-<\/span> <span class=\"hljs-built_in\">sum<\/span> <span class=\"hljs-operator\">+<\/span> i<br \/>\n  i <span class=\"hljs-operator\">&lt;-<\/span> i <span class=\"hljs-operator\">+<\/span> <span class=\"hljs-number\">1<\/span><br \/>\n<span class=\"hljs-punctuation\">}<\/span><br \/>\nprint<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-built_in\">sum<\/span><span class=\"hljs-punctuation\">)<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<h2>repeat\u5faa\u73af<\/h2>\n<p>repeat\u5faa\u73af\u7528\u4e8e\u65e0\u9650\u5faa\u73af\u6267\u884c\u4e00\u6bb5\u4ee3\u7801\uff0c\u76f4\u5230\u6ee1\u8db3\u67d0\u4e2a\u6761\u4ef6\u8df3\u51fa\u5faa\u73af\u4e3a\u6b62\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-R\"><span class=\"hljs-keyword\">repeat<\/span> <span class=\"hljs-punctuation\">{<\/span><br \/>\n   statement<br \/>\n   <span class=\"hljs-keyword\">if<\/span> <span class=\"hljs-punctuation\">(<\/span>condition<span class=\"hljs-punctuation\">)<\/span> <span class=\"hljs-punctuation\">{<\/span><br \/>\n      <span class=\"hljs-keyword\">break<\/span><br \/>\n   <span class=\"hljs-punctuation\">}<\/span><br \/>\n<span class=\"hljs-punctuation\">}<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<p>\u5176\u4e2d\uff0cstatement\u662f\u9700\u8981\u5faa\u73af\u6267\u884c\u7684\u4ee3\u7801\u6bb5\uff0ccondition\u662f\u4e00\u4e2a\u903b\u8f91\u8868\u8fbe\u5f0f\u3002\u5982\u679c\u6ee1\u8db3condition\uff0c\u53ef\u4ee5\u901a\u8fc7break\u8bed\u53e5\u8df3\u51fa\u5faa\u73af\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u53ef\u4ee5\u8ba1\u7b971\u523010\u4e4b\u95f4\u7684\u6240\u6709\u6574\u6570\u7684\u548c\uff1a<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-R\"><span class=\"hljs-built_in\">sum<\/span> <span class=\"hljs-operator\">&lt;-<\/span> 0<br \/>\ni <span class=\"hljs-operator\">&lt;-<\/span> 1<br \/>\n<span class=\"hljs-keyword\">repeat<\/span> <span class=\"hljs-punctuation\">{<\/span><br \/>\n  <span class=\"hljs-built_in\">sum<\/span> <span class=\"hljs-operator\">&lt;-<\/span> <span class=\"hljs-built_in\">sum<\/span> <span class=\"hljs-operator\">+<\/span> i<br \/>\n  i <span class=\"hljs-operator\">&lt;-<\/span> i <span class=\"hljs-operator\">+<\/span> <span class=\"hljs-number\">1<\/span><br \/>\n  <span class=\"hljs-keyword\">if<\/span> <span class=\"hljs-punctuation\">(<\/span>i <span class=\"hljs-operator\">&gt;<\/span> <span class=\"hljs-number\">10<\/span><span class=\"hljs-punctuation\">)<\/span> <span class=\"hljs-punctuation\">{<\/span><br \/>\n    <span class=\"hljs-keyword\">break<\/span><br \/>\n  <span class=\"hljs-punctuation\">}<\/span><br \/>\n<span class=\"hljs-punctuation\">}<\/span><br \/>\nprint<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-built_in\">sum<\/span><span class=\"hljs-punctuation\">)<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<h2>\u5e94\u7528\u5b9e\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u5faa\u73af\u8bed\u53e5\u7684\u5b9e\u4f8b\uff0c\u5b83\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5411\u91cf\u4e2d\u7684\u5143\u7d20\u6309\u7167\u6307\u5b9a\u7684\u987a\u5e8f\u8fdb\u884c\u6392\u5217\uff1a<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-r\"><span class=\"hljs-comment\"># \u5b9a\u4e49\u4e00\u4e2a\u5411\u91cf<\/span><br \/>\nvec <span class=\"hljs-operator\">&lt;-<\/span> <span class=\"hljs-built_in\">c<\/span><span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-number\">3<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-number\">5<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-number\">2<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-number\">6<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-number\">1<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-number\">4<\/span><span class=\"hljs-punctuation\">)<\/span><\/p>\n<p><span class=\"hljs-comment\"># \u5b9a\u4e49\u4e00\u4e2a\u6307\u5b9a\u987a\u5e8f\u7684\u7d22\u5f15\u5411\u91cf<\/span><br \/>\nindex <span class=\"hljs-operator\">&lt;-<\/span> <span class=\"hljs-built_in\">c<\/span><span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-number\">5<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-number\">3<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-number\">1<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-number\">6<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-number\">2<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-number\">4<\/span><span class=\"hljs-punctuation\">)<\/span><\/p>\n<p><span class=\"hljs-comment\"># \u5b9a\u4e49\u4e00\u4e2a\u65b0\u5411\u91cf\u7528\u4e8e\u5b58\u50a8\u6392\u5e8f\u540e\u7684\u7ed3\u679c<\/span><br \/>\nsorted_vec <span class=\"hljs-operator\">&lt;-<\/span> numeric<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-built_in\">length<\/span><span class=\"hljs-punctuation\">(<\/span>vec<span class=\"hljs-punctuation\">)<\/span><span class=\"hljs-punctuation\">)<\/span><\/p>\n<p><span class=\"hljs-comment\"># \u4f7f\u7528for\u5faa\u73af\u6309\u7167\u6307\u5b9a\u987a\u5e8f\u6392\u5e8f<\/span><br \/>\n<span class=\"hljs-keyword\">for<\/span> <span class=\"hljs-punctuation\">(<\/span>i <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-number\">1<\/span><span class=\"hljs-operator\">:<\/span><span class=\"hljs-built_in\">length<\/span><span class=\"hljs-punctuation\">(<\/span>vec<span class=\"hljs-punctuation\">)<\/span><span class=\"hljs-punctuation\">)<\/span> <span class=\"hljs-punctuation\">{<\/span><br \/>\n  sorted_vec<span class=\"hljs-punctuation\">[<\/span>index<span class=\"hljs-punctuation\">[<\/span>i<span class=\"hljs-punctuation\">]<\/span><span class=\"hljs-punctuation\">]<\/span> <span class=\"hljs-operator\">&lt;-<\/span> vec<span class=\"hljs-punctuation\">[<\/span>i<span class=\"hljs-punctuation\">]<\/span><br \/>\n<span class=\"hljs-punctuation\">}<\/span><\/p>\n<p><span class=\"hljs-comment\"># \u8f93\u51fa\u6392\u5e8f\u540e\u7684\u7ed3\u679c<\/span><br \/>\nprint<span class=\"hljs-punctuation\">(<\/span>sorted_vec<span class=\"hljs-punctuation\">)<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-csharp\">[<span class=\"hljs-meta\">1<\/span>] <span class=\"hljs-number\">1<\/span> <span class=\"hljs-number\">2<\/span> <span class=\"hljs-number\">3<\/span> <span class=\"hljs-number\">4<\/span> <span class=\"hljs-number\">5<\/span> <span class=\"hljs-number\">6<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u6307\u5b9a\u987a\u5e8f\u7684\u7d22\u5f15\u5411\u91cf<code>index<\/code>\u6765\u6307\u5b9a\u5143\u7d20\u7684\u6392\u5217\u987a\u5e8f\uff0c\u7136\u540e\u4f7f\u7528for\u5faa\u73af\u5c06\u539f\u5411\u91cf\u4e2d\u7684\u5143\u7d20\u6309\u7167\u6307\u5b9a\u987a\u5e8f\u5b58\u50a8\u5230\u4e00\u4e2a\u65b0\u5411\u91cf<code>sorted_vec<\/code>\u4e2d\uff0c\u6700\u7ec8\u5f97\u5230\u4e86\u6309\u7167\u6307\u5b9a\u987a\u5e8f\u6392\u5217\u7684\u5411\u91cf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>R\u8bed\u8a00\u4e2d\u6709\u5f88\u591a\u79cd\u5faa\u73af\u8bed\u53e5\uff0c\u5305\u62ecfor\u5faa\u73af\u3001while\u5faa\u73af\u3001repeat\u5faa\u73af\u7b49\u3002\u8fd9\u4e9b\u5faa\u73af\u8bed\u53e5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u7a0b\u5e8f\u4e2d [&hellip;]<\/p>\n","protected":false},"author":111,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-393","post","type-post","status-publish","format-standard","hentry","category-r"],"_links":{"self":[{"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/393","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=\/wp\/v2\/users\/111"}],"replies":[{"embeddable":true,"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=393"}],"version-history":[{"count":0,"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/393\/revisions"}],"wp:attachment":[{"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}