Timur and three other people are running a marathon. The value a is the distance that Timur has run and b, c, d correspond to the distances the other three participants ran.
Output the number of participants in front of Timur.
中文翻譯
給予你 4 個獨立的整數 a, b, c, d。
Timur 跟另外三人正在參與一場馬拉松中。 a 是 Timur 已經跑的距離,而 b, c, d 則表示了另外三位參賽者跑的距離。
輸出在 Timur 前面的參賽者一共有幾位。
輸入
The first line contains a single integer t (1≤t≤104) — the number of test cases.
The description of each test case consists of four distinct integers a, b, c, d (0≤a,b,c,d≤104).
第一行包含了一個整數 t (1≤t≤104) — 測試案例的數量
每一個測試案例都由 4 個獨立的整數組成 a, b, c, d (0≤a,b,c,d≤104)。
輸出
For each test case, output a single integer — the number of participants in front of Timur.
For the first test case, there are 2 people in front of Timur, specifically the participants who ran distances of 3 and 4. The other participant is not in front of Timur because he ran a shorter distance than Timur.
For the second test case, no one is in front of Timur, since he ran a distance of 10000 while all others ran a distance of 0, 1, and 2 respectively.
For the third test case, only the second person is in front of Timur, who ran a total distance of 600 while Timur ran a distance of 500.
在第一個測試案例中,有兩個人在 Timur 前面,分別是跑了3以及4距離的參賽者,而唯一一位不在 Timur 前面的參賽者則是因為他跑得比 Timur 還要少的距離。
第二個測試案例中,沒有人在 Timur 前面,因為他跑了10000 的距離 ,而其他人分別是 0、1、2而已
第三個測試案例中,只有第二個人跑在 Timur 前面,就是那個總共跑了600的距離的而Timur 只跑了 500
解題思路
有幾種方式可以解決這一題,就讓我們來看一下吧
然後來比較一下各自的寫法或是限制
C# 解決方案
方案1 – for 迴圈
int t = int.Parse(Console.ReadLine());
for(int i=0; i<t; i++){
int[] arr = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
int cnt = 0;
int timur = arr[0];
for(int j=1;j<arr.Length;j++){
if(arr[j]>timur){
cnt++;
}
}
Console.WriteLine(cnt);
}
最常見的作法,迴圈判斷是否大於並加在計數器上
方案2
int t = int.Parse(Console.ReadLine());
for(int i=0; i<t; i++){
int[] arr = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
int cnt = 0;
cnt+=arr[1]>arr[0]?1:0;
cnt+=arr[2]>arr[0]?1:0;
cnt+=arr[3]>arr[0]?1:0;
Console.WriteLine(cnt);
}
已知只有4位參賽者,故也可以用3個三元不等式來判斷,當然如果數量大的話就非常不適合了
方案3 – LINQ
int t = int.Parse(Console.ReadLine());
for(int i=0; i<t; i++){
int[] arr = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
int timur = arr[0];
var cnt = arr.Count(x=>x>timur);
Console.WriteLine(cnt);
}
var uagb_data = {"ajax_url":"https:\/\/zyrastory.com\/wp-admin\/admin-ajax.php","uagb_masonry_ajax_nonce":"f6e3b40b7e"};
var uagb_data = {"ajax_url":"https:\/\/zyrastory.com\/wp-admin\/admin-ajax.php","uagb_masonry_ajax_nonce":"f6e3b40b7e","uagb_grid_ajax_nonce":"ff760de2bb"};
( function() {
let elements = document.querySelectorAll( '.uagb-post-grid.uagb-block-567f2d62 .uagb-post-pagination-wrap a' );
elements.forEach(function(element) {
element.addEventListener("click", function(event){
event.preventDefault();
const link = event.target.getAttribute('href').match( /\/page\/\d+\// )?.[0] || '';
const regex = /\d+/; // regular expression to match a number at the end of the string
const match = link.match( regex ) ? link.match( regex )[0] : 1; // match the regular expression with the link
const pageNumber = parseInt( match ); // extract the number and parse it to an integer
window.UAGBPostGrid._callAjax({"btnBorderTopWidth":1,"btnBorderLeftWidth":1,"btnBorderRightWidth":1,"btnBorderBottomWidth":1,"btnBorderTopLeftRadius":30,"btnBorderTopRightRadius":30,"btnBorderBottomLeftRadius":30,"btnBorderBottomRightRadius":30,"btnBorderStyle":"none","overallBorderColor":"","block_id":"567f2d62","categories":"271","displayPostExcerpt":false,"displayPostComment":false,"displayPostImage":false,"imgSize":"medium","linkBox":false,"align":"center","orderBy":"rand","excludeCurrentPost":true,"postPagination":true,"pageLimit":0,"imageRatio":"2-3","imgEqualHeight":true,"extended_widget_opts":{"id_base":-1,"column":{"desktop":"12","tablet":"12","mobile":"12"},"alignment":{"desktop":"default","tablet":"default","mobile":"default"},"roles":{"state":"","options":"hide"},"visibility":{"selected":"0","options":"hide","acf":{"visibility":"hide","field":"","condition":"","value":""}},"author_page":{"author_pages":{"selections":"1"}},"devices":{"options":"hide"},"days":{"options":"hide"},"dates":{"options":"hide","from":"","to":""},"styling":{"selected":"0","bg_image":"","background":"","background_hover":"","heading":"","text":"","links":"","links_hover":"","border_color":"","border_type":"","border_width":"","background_input":"","text_input":"","border_color_input":"","border_type_input":"","border_width_input":"","background_submit":"","background_submit_hover":"","text_submit":"","border_color_submit":"","border_type_submit":"","border_width_submit":"","list_border_color":"","table_border_color":""},"class":{"selected":"0","link":"","id":"","classes":"","animation":"","event":"enters","speed":"","offset":"","delay":"","logic":""},"tabselect":"0"},"UAGHideMob":true,"UAGHideTab":true,"UAGResponsiveConditions":true,"btnBorderLink":true,"btnBorderRadiusLink":true,"overallBorderLink":true,"overallBorderRadiusLink":true,"inheritFromTheme":true,"postType":"post","postDisplaytext":"No post found!","taxonomyType":"category","postsToShow":6,"enableOffset":false,"postsOffset":0,"displayPostDate":true,"excerptLength":15,"displayPostAuthor":false,"displayPostTitle":true,"displayPostTaxonomy":false,"hideTaxonomyIcon":true,"taxStyle":"default","displayPostTaxonomyAboveTitle":"withMeta","imgPosition":"top","bgOverlayColor":"#000000","overlayOpacity":"50","displayPostLink":true,"newTab":false,"ctaText":"Read More","inheritFromThemeBtn":false,"buttonType":"primary","btnHPadding":"","btnVPadding":"","columns":3,"tcolumns":2,"mcolumns":1,"width":"wide","order":"desc","rowGap":20,"rowGapTablet":20,"rowGapMobile":20,"columnGap":20,"bgType":"color","bgColor":"#f6f6f6","titleTag":"h4","titleFontSize":"","titleFontSizeType":"px","titleFontFamily":"","titleLineHeightType":"em","titleLoadGoogleFonts":false,"metaColor":"","highlightedTextColor":"#fff","highlightedTextBgColor":"#3182ce","metaFontSize":"","metaFontSizeType":"px","metaFontFamily":"","metaLineHeightType":"em","metaLoadGoogleFonts":false,"excerptColor":"","excerptFontSize":"","excerptFontSizeType":"px","excerptFontFamily":"","excerptLineHeightType":"em","excerptLoadGoogleFonts":false,"displayPostContentRadio":"excerpt","ctaBgType":"color","ctaBgHType":"color","ctaFontSize":"","ctaFontSizeType":"px","ctaFontFamily":"","ctaLineHeightType":"em","ctaLoadGoogleFonts":false,"paddingTop":20,"paddingBottom":20,"paddingRight":20,"paddingLeft":20,"contentPadding":20,"ctaBottomSpace":0,"ctaBottomSpaceTablet":0,"ctaBottomSpaceMobile":0,"imageBottomSpace":15,"titleBottomSpace":15,"metaBottomSpace":15,"excerptBottomSpace":25,"contentPaddingUnit":"px","rowGapUnit":"px","columnGapUnit":"px","excerptBottomSpaceUnit":"px","paginationSpacingUnit":"px","imageBottomSpaceUnit":"px","taxonomyBottomSpaceUnit":"px","titleBottomSpaceUnit":"px","metaBottomSpaceUnit":"px","ctaBottomSpaceUnit":"px","paddingBtnUnit":"px","mobilePaddingBtnUnit":"px","tabletPaddingBtnUnit":"px","paddingUnit":"px","mobilePaddingUnit":"px","tabletPaddingUnit":"px","isPreview":false,"taxDivider":", ","titleLetterSpacing":"","titleLetterSpacingType":"px","metaLetterSpacing":"","metaLetterSpacingType":"px","ctaLetterSpacing":"","ctaLetterSpacingType":"px","excerptLetterSpacing":"","excerptLetterSpacingType":"px","useSeparateBoxShadows":true,"boxShadowColor":"#00000070","boxShadowHOffset":0,"boxShadowVOffset":0,"boxShadowBlur":"","boxShadowSpread":"","boxShadowPosition":"outset","boxShadowColorHover":"","boxShadowHOffsetHover":0,"boxShadowVOffsetHover":0,"boxShadowBlurHover":"","boxShadowSpreadHover":"","boxShadowPositionHover":"outset","borderWidth":"","borderStyle":"none","borderColor":"","borderRadius":"","blockName":"post-grid","equalHeight":true,"paginationBgActiveColor":"#e4e4e4","paginationActiveColor":"#333333","paginationBgColor":"#e4e4e4","paginationColor":"#777777","paginationMarkup":"","paginationLayout":"filled","paginationBorderColor":"#888686","paginationBorderSize":1,"paginationSpacing":20,"paginationAlignment":"left","paginationPrevText":"\u00ab Previous","paginationNextText":"Next \u00bb","layoutConfig":[["uagb\/post-image"],["uagb\/post-taxonomy"],["uagb\/post-title"],["uagb\/post-meta"],["uagb\/post-excerpt"],["uagb\/post-button"]],"post_type":"grid","equalHeightInlineButtons":false,"paginationType":"ajax","isLeftToRightLayout":false,"wrapperTopPadding":"","wrapperRightPadding":"","wrapperLeftPadding":"","wrapperBottomPadding":"","wrapperTopPaddingTablet":"","wrapperRightPaddingTablet":"","wrapperLeftPaddingTablet":"","wrapperBottomPaddingTablet":"","wrapperTopPaddingMobile":"","wrapperRightPaddingMobile":"","wrapperLeftPaddingMobile":"","wrapperBottomPaddingMobile":"","wrapperPaddingUnit":"px","wrapperPaddingUnitTablet":"px","wrapperPaddingUnitMobile":"px","wrapperPaddingLink":false,"wrapperAlign":"row","wrapperAlignPosition":"center","extended_widget_opts_block":{},"extended_widget_opts_state":"","extended_widget_opts_clientid":"","dateUpdated":""}, pageNumber, '567f2d62');
});
});
} )();
ai_front = {"insertion_before":"BEFORE","insertion_after":"AFTER","insertion_prepend":"PREPEND CONTENT","insertion_append":"APPEND CONTENT","insertion_replace_content":"REPLACE CONTENT","insertion_replace_element":"REPLACE ELEMENT","visible":"VISIBLE","hidden":"HIDDEN","fallback":"FALLBACK","automatically_placed":"Automatically placed by AdSense Auto ads code","cancel":"Cancel","use":"Use","add":"Add","parent":"Parent","cancel_element_selection":"Cancel element selection","select_parent_element":"Select parent element","css_selector":"CSS selector","use_current_selector":"Use current selector","element":"ELEMENT","path":"PATH","selector":"SELECTOR"};