ערכתי לאחרונה בתאריך 19.01.17 בשעה 10:45 בברכה, jon snow
Return the "centered" average of an array of ints, which we'll say is the mean average of the values, except ignoring the largest and smallest values in the array. If there are multiple copies of the smallest value, ignore just one copy, and likewise for the largest value. Use int division to produce the final average. You may assume that the array is length 3 or more.centeredAverage() → 3
centeredAverage() → 5
centeredAverage() → -3
הפתרון שלי שעובד אבל לא משהו בכלל לדעתי (יש לי מבחן ביום ראשון עבור איזה משרה והרבה זמן לא נגעתי בג'אווה)
יש עוד פתרון שחשבתי עליו אבל לא היה לי כח לממש זה מרג' סורט על המערך ופשוט להתחיל את הסכימה בלולאה ממקום 1 עד מקום לפני אחרון
public int centeredAverage(int nums) { int min = nums{0}; int max = nums{0}}; int sum = 0; for (int i = 0 ; i < nums.length ; i++){ if (nums > max){ max = nums; } if(nums{i} < min){ min = nums{i}; } sum += nums{i}; } sum -= max; sum -= min; return sum/(nums.length-2); }
|
שמתי סוגריים מסולסלים כי לא רואים את המרובעים בבלוק קוד.