LoginSignup
8
7

More than 5 years have passed since last update.

行列のRankを求めるプログラム

Last updated at Posted at 2014-10-31
rank.c

int rank(int mat[][100], int n){    
    int ltmp[100], tmp, a_tmp[100], b_tmp[100];
    int i, j, k;
    int count;
    int all_zero;

    for(i = 0; i < n; i++){
        all_zero = 0;
        if(mat[i][i] == 0){ 
            for(j = 0; j < n; j++){
                if(mat[j][i] != 0){
                    for(k = 0; k < n; k++){
                        tmp = mat[i][k];
                        mat[i][k] = mat[j][k];
                        mat[j][k] = tmp;
                    }
                } else if(j == n - 1)
                    all_zero = 1;
            }
        }



        if(!all_zero){      
            for(j = i + 1; j < n; j++){
                for(k = 0; k < n; k++){
                    a_tmp[k] = mat[i][k] * mat[j][i];
                    b_tmp[k] = mat[j][k] * mat[i][i];
                }
                for(k = 0; k < n; k++)
                    mat[j][k] = b_tmp[k] - a_tmp[k];
            }
        }
    }


    count = 0;
    for(i = 0; i < n; i++){
        if(mat[i][n] == 0)
            count++;
    }

    return (n - count);
}

時給すら発生しないのに、頼まれたので書いた。
ループが多すぎる。何か良い方法はないだろうか。

8
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7