daily pastebin goal
62%
SHARE
TWEET

Untitled

a guest May 26th, 2018 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <malloc.h>
  3.  
  4. void swap(double *x, double *y) {
  5.     double t = *x;
  6.     *x = *y;
  7.     *y = t;
  8. }
  9.  
  10. double get_mx(double *a, int n, int m) {
  11.     int mx = *a;
  12.     for (int i = 1; i < n; i++) {
  13.         if (mx < *(a + m * i))
  14.             mx = *(a + m * i);
  15.     }
  16.     return mx;
  17. }
  18. void sort(double *a, int n, int m) {
  19.     for (int i = 1; i < n; i++) {
  20.         for (int j = 0; j <  n - i; j++) {
  21.             if (get_mx(a + j, n, m) > get_mx(a + j + 1, n, m)) {
  22.                 for (int k = 0; k < n; k++) {
  23.                     swap(a + j + k * m, a + j + 1 + k * m);
  24.                 }
  25.             }
  26.         }
  27.     }
  28. }
  29.  
  30.    
  31.    
  32.  
  33. int main(void) {
  34.     FILE* in = fopen("input.txt", "r");
  35.    
  36.     int n, m;
  37.     fscanf(in, "%d %d", &n, &m);
  38.     double *a = malloc(n * m * sizeof(double));
  39.     for (int i = 0; i < n; i++) {
  40.         for (int j = 0; j < m; j++) {
  41.             fscanf(in, "%f", a + i * m + j);
  42.         }
  43.     }
  44.    
  45.     sort(a, n, m);
  46.     for (int i = 0; i < n; i++) {
  47.         double ans = 0;
  48.         for (int j = 0; j < m; j++) {
  49.             ans += (*(a + j)) * (*(a + j + i * m));
  50.         }
  51.     }
  52.     return 0;
  53. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top