Sort by

recency

|

872 Discussions

|

  • + 0 comments

    include

    include

    include

    include // For controlling the output format

    using namespace std;

    int main() { // Step 1: Read the number of elements (not actually used, we just need the list) int n; cin >> n;

    // Step 2: Read the list of numbers into a vector
    vector<int> nums(n);
    for (int i = 0; i < n; ++i) {
        cin >> nums[i];
    }
    
    // Step 3: Calculate the average
    double sum = 0;
    for (int num : nums) {
        sum += num;
    }
    double average = sum / n;
    
    // Step 4: Find the minimum number
    int min_val = *min_element(nums.begin(), nums.end());
    
    // Step 5: Output the results
    // Print the average rounded to 1 decimal place
    cout << fixed << setprecision(1) << average << endl;
    
    // Print the average rounded to the nearest whole number
    cout << round(average) << endl;
    
    // Print the smallest number
    cout << min_val << endl;
    
    return 0;
    

    }

  • + 0 comments

    For anyone having trouble in Java: I have yet to optimize this but I was able to pass all test cases except the last one (TestCase 3):

    public static void main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
            List<Float> arr = new ArrayList<>();
            Scanner sc = new Scanner(System.in);
            while(sc.hasNext()) {
                float f = sc.nextFloat();
                arr.add(f);
            }
            int size = arr.size()-1;
            arr.remove(0); // remove first element because it's just size of array
            float mean = 0;
            float median = 0;
            float mode = 0;
            
            // Sort array
            Collections.sort(arr);
            
            // Calculate mean
            for(int i = 0; i < arr.size(); i++) {
                mean += arr.get(i);
            }
            mean = (mean/arr.size());
            System.out.println(mean); // Print out mean
            
            // Calculate median 
            if(arr.size() % 2 == 0) {
                float num1 = arr.get((int)(arr.size()/2));
                float num2 = arr.get((int)(arr.size()/2)-1);
                median = ((num1+num2)/2);
            } else {
                median = arr.get(((int)arr.size()/2)+1);
            }
            System.out.println(median); // Print out median
            
            // Calculate mode
            Map<Float,Integer> modeMap = new TreeMap<>();
            
            for(float f: arr) {
                if(modeMap.containsKey(f)) {
                    int count = modeMap.get(f);
                    modeMap.replace(f,count++);
                } else {
                    modeMap.put(f,0);
                }
            }
            
            int count = 1; // counter to exit loop
            for(Map.Entry<Float,Integer> entry1 : modeMap.entrySet()) {
                while(count < arr.size()){
                    for(Map.Entry<Float,Integer> entry2 : modeMap.entrySet()) {
                        if(entry1.getValue() >= entry2.getValue() && entry1.getKey() < entry2.getKey()) {
                            mode = entry1.getKey();
                            break;
                        }
                        else if(entry1.getKey() == entry2.getKey()) {
                            mode = entry1.getKey();
                        }
                        count++;
                    }
                }
            }
        
            System.out.println(mode); // Print out mode
            
        }
    
  • + 0 comments

    Enter your code here. Read input from STDIN. Print output to STDOUT

    ''' from statistics import mean, median, mode

    n = input() x = sorted(list(map(int, input().split(" "))))

    print(mean(x)) print(median(x)) print(mode(x)) ''' from collections import Counter

    n = input() x = sorted(list(map(int, input().split(" "))))

    def mean(i): output = sum(i)/len(i) return output

    def median(i): i.sort() mid = len(i) // 2

    if len(i) % 2 == 0:
        med = (i[mid] + i[mid - 1]) / 2
    else:
        med = i[mid]
    return med
    

    def mode(i): count = Counter(i) max_count = max(count.values()) modes = [key for key, count in count.items() if count == max_count] return min(modes) if len(modes) > 1 else modes[0]

    print(mean(x)) print(median(x)) print(mode(x))

  • + 0 comments

    n = int(input()) raw = input().split() x = [int(i) for i in raw]

    def mean(arr): return sum(arr)/len(arr)

    def median(arr): sorted_arr = sorted(arr) a = (len(arr)-1)//2 b = a + 1 return (sorted_arr[a]+sorted_arr[b])/2

    def mode(arr): sorted_arr = sorted(arr) sorted_set = list( set(sorted_arr)) occurances = {item : sorted_arr.count(item) for item in sorted_set} #in case there are multiple occurances of multiple items n_dept_occ = [] for key, value in occurances.items(): if max(occurances.values()) == 1: return min(occurances.keys()) elif value == max(occurances.values()): n_dept_occ.append(key) return min(n_dept_occ)

    print(mean(x)) print(median(x)) print(mode(x))

  • + 0 comments
    from statistics import mean,median,mode
    N = input()
    X = sorted(list(map(int,input().split(" "))))
    print(mean(X))
    print(median(X))
    print(mode(X))