码迷,mamicode.com
首页 > 其他好文 > 详细

关于Hilbert矩阵的几道编程题

时间:2016-09-08 18:23:37      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

最后一个敲了好久,写篇日志纪念一下,有需要的自取吧。 

 

2.以浮点数的形式打印 n 阶 Hilbert 矩阵

//editor: Jan Tang
//problem: 2
#include <iostream>
#include <cstdio>
using namespace std;
#define set0(a) memset(a,0,sizeof(a));
#define CIN(a,n) for(int i=1;i<=n;i++) cin>>a[i];
typedef long long ll;
typedef unsigned long long ull;
const int Mod = 1e9+7;
const int maxn = 100005;
const int inf = 0x3f3f3f3f;
int m,n;
/*==============================head==========================*/
int main(){
    while(scanf("%d",&n)!=EOF){
        for(int i = 1; i <= n; i++){
            putchar([);
            for(int j = 1; j <= n; j++){
                printf("%.3lf", 1.0/(i+j-1));
                if(j!=n) printf("\t ");
            }
            putchar(]);
            printf("\n");
            if(i==n) continue;

            putchar([);
            for(int j = 1; j <= n-1; j++){
                putchar(\t);
            }
            printf("      ");
            putchar(]);
            printf("\n");
        }
    }
    return 0;
}

 

4.以有理数的形式打印 n 阶 Hilbert 矩阵

输出格式挺难控制的,用-左对齐

//editor: Jan Tang
//problem: 4
#include <iostream>
#include <cstdio>
using namespace std;
#define set0(a) memset(a,0,sizeof(a));
#define CIN(a,n) for(int i=1;i<=n;i++) cin>>a[i];
typedef long long ll;
typedef unsigned long long ull;
const int Mod = 1e9+7;
const int maxn = 100005;
const int inf = 0x3f3f3f3f;
int m,n;
/*==============================head==========================*/
int main(){
    while(scanf("%d",&n)!=EOF){
        for(int i = 1; i <= n; i++){
            putchar([);
            for(int j = 1; j <= n; j++){
                printf("1/%-2d", j+i-1);
                if(j!=n) printf("\t ");
            }
            putchar(]);
            printf("\n");
            if(i==n) continue;

            putchar([);
            for(int j = 1; j <= n-1; j++){
                putchar(\t);
            }
            printf("     ");
            putchar(]);
            printf("\n");
        }
    }
    return 0;
}

 

8&9.求n阶 Hilbert 矩阵的逆矩阵,并用有理数的形式打印
要用高斯消元,结合分数类来做,输出格式不想管了,放弃治疗了。

//editor: Jan Tang
//problem: 8 & 9
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <cstdlib>
using namespace std;
#define set0(a) memset(a,0,sizeof(a));
#define CIN(a,n) for(int i=1;i<=n;i++) cin>>a[i];
typedef long long ll;
typedef unsigned long long ull;
const int Mod = 1e9+7;
const int maxn = 1005;
const int inf = 0x3f3f3f3f;
int m,n;
ll gcd(ll s, ll t){
    return t==0 ? s : gcd(t, s%t);
}

struct fraction{ //分数类
    long long num, den;  //分子 numerator, 分母 denominator
    fraction(long long num = 0, long long den = 1){ //构造函数,默认值为0/1
        if(den < 0){                                //保持分母为正
            num = -num;
            den = -den;
        }
//        assert(den != 0);                            //断言:如果不满足,程序直接停止
        long long g =gcd(abs(num), den);
        this->num = num / g;
        this->den = den / g;
    }
    fraction operator +(const fraction &o) const{
        return fraction(num * o.den + den * o.num, den * o.den);
    }
    fraction operator -(const fraction &o) const{
        return fraction(num * o.den - den * o.num, den * o.den);
    }
    fraction operator *(const fraction &o) const{
        return fraction(num * o.num, den * o.den);
    }
    fraction operator /(const fraction &o) const{
        return fraction(num * o.den, den * o.num);
    }
    bool operator <(const fraction &o) const{
        return num * o.den < den * o.num;
    }
    bool operator ==(const fraction &o) const{
        return num * o.den == den * o.num;
    }
};                                    //不能漏了分号

vector<fraction> aa[maxn], bb[maxn];

inline vector<fraction> operator * (vector<fraction> a, fraction b){
    int n = a.size();
    vector<fraction> res;
    for(int i = 0; i < n ;i++){
        res.push_back(b*a[i]);
    }
    return res;
}

inline vector<fraction> operator - (vector<fraction> a, vector<fraction> b){
    int n = a.size();
    vector<fraction> res;
    for(int i = 0; i < n ;i++){
        res.push_back(a[i]-b[i]);
    }
    return res;
}

inline void inverse(vector<fraction> A[], vector<fraction> C[], int n){
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            if(j == i) C[i].push_back(fraction(1,1));
            else C[i].push_back(fraction(0,1));
        }
    }
    for(int i = 0; i < n; i++){
        for(int j = i; j < n; j++){
            if(A[j][i].num != 0){
                swap(A[i], A[j]);
                swap(C[i], C[j]);
                break;
            }
        }
        C[i] = C[i] * (fraction(1,1) / A[i][i]);
        A[i] = A[i] * (fraction(1,1) / A[i][i]);
        for(int j = 0; j < n; j++)
            if(j != i && A[j][i].num != 0){
                C[j] = C[j] - C[i] * A[j][i];
                A[j] = A[j] - A[i] * A[j][i];
            }
    }
}
/*==============================head==========================*/
int main(){
    while(scanf("%d",&n)!=EOF){
        for(int i = 0; i < n; i++){
            aa[i].clear();
            bb[i].clear();
        }
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                aa[i].push_back(fraction(1, i+j+1));
//                printf("%lld/%lld\t", aa[i][j].num, aa[i][j].den);
            }
//            cout<<endl;
        }
    //    fraction tmp1 = fraction(2,2), tmp2 = fraction(2,4);
    //    swap(tmp1, tmp2);
    //    printf("%lld/%lld\t", tmp1.num, tmp1.den);

        inverse(aa, bb, n); //这里不用加方括号

        for(int i = 0; i < n; i++){
            putchar([);
            for(int j = 0; j < n; j++){
                printf("%lld/%lld\t", bb[i][j].num, bb[i][j].den);
            }
            putchar(]);
            printf("\n");
            if(i==n-1) continue;

            putchar([);
            for(int j = 1; j <= n-1; j++){
                putchar(\t);
            }
            printf("        ");
            putchar(]);
            printf("\n");
        }
    }
    return 0;
}

 

关于Hilbert矩阵的几道编程题

标签:

原文地址:http://www.cnblogs.com/jantang/p/5853929.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!