关于Hilbert矩阵的几道编程题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于Hilbert矩阵的几道编程题相关的知识,希望对你有一定的参考价值。

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

 

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矩阵的几道编程题的主要内容,如果未能解决你的问题,请参考以下文章

记录一下做的几道编程题

2.4继续说说昨天的题---关于递归的几道题目

Spring的几道选择题

Oracle同程网络的几道笔试题解答(不一定对,欢迎拍砖)

大厂面试常见的几道SQL题,看你能答吗?

初学者的几道shell脚本题!!!!