题目描述

Li Zhixiang have already been in “Friendship” ocean-going freighter for three months. The excitement has gradually disappeared. He stands on the board, holding the railing and watching the dazzling ocean in the sun silently. Day after day, the same scenery is monotonous and tasteless, even the merry seagulls following the freighter cannot arouse his interest. Hearing the footsteps behind, he turns back to see the old captain is coming towards him. The captain has understood his idea, however, he starts a new topic with the young man.

“Do you know how far our voyage is?” The captain asks. Li Zhixiang feels ashamed because he can not answer. Then the captain says with a smile, “5050 miles. Do you still remember the story of 5050?” This time the young man really blushes. The old captain continues saying:” You definitely know the story of 5050. When the German mathematician, “the prince of mathematicians”, Gauss was 10 years old …” Young man remembers this story and goes on to tell, “ When Gauss was 10 years old, he could add a list of integers from 1 to 100 in a few seconds, which shocked the teachers.” The old captain adds, “Gauss has many other stories like this. When he entered the university at the age of 17, he was able to construct heptadecagon by compass and straightedge. His university teachers were also impressed by his ability. Not only could college graduate students fail to do it, but also they felt hard to understand Gauss’s constructing process.”

At this time, vice-captain greets the old captain. The old captain says to Li Zhixiang: “Come over to my office tonight, let’s continue the conversation.” It is still calm and tranquil in the evening. The freighter travels smoothly on the sea in the silver moonlight. The captain tells the young man the following words.

Among the mathematicians through the ages, there are three greatest mathematicians: Archimedes, Newton and Gauss. Most of Gauss’s mathematical achievements are difficult to understand. Nevertheless, there are some comparatively easy. For instance, when it comes to solving multivariate system of linear equations, there is a solution called “Gauss Elimination”. In the navigation business, many problems can be solved by “Gauss elimination”. If you are interested in it, I will show you a simple question. Try it.”

输入

There are several test cases. In the first line of each case, a number n indicates that there are n equations. The following n lines, each line has n+1 numbers, ai1,ai2,ai3…..ain, bi(1<= i <=n), these numbers indicate the coefficients of systems of the equations. ai1*x1+ai2*x2+......ain*xn=bi. Input is terminated by the end of file.

输出

For each given systems of equations, if there are solutions, output n solutions in the order of appearance in the equations(n<=100),  each solution number is in one line. If solution is not integer, show it in fraction. If no solution, output “No solution.” Leave a blank line after each case.

样例输入

2
1000000000000000000000000 1000000000000000000000000 1000000000000000000000000
-1000000000000000000000000 1000000000000000000000000 0
1
0 4

样例输出

1/2
1/2No solution.
大数分数高斯消元 

import java.math.BigInteger;
import java.util.Scanner;class Number{BigInteger a,b;Number() {a=BigInteger.valueOf(1);b=BigInteger.valueOf(1);}Number(BigInteger x,BigInteger y) {a=x;b=y;}Number sub(Number x){Number c=new Number();c.b=b.multiply(x.b);c.a=a.multiply(x.b).subtract(x.a.multiply(b));BigInteger d=c.a.gcd(c.b);if (d.compareTo(BigInteger.valueOf(0))!=0){c.a=c.a.divide(d); c.b=c.b.divide(d);}return c;}Number mul(Number x){Number c=new Number();c.b=b.multiply(x.b);c.a=a.multiply(x.a);BigInteger d=c.a.gcd(c.b);if (d.compareTo(BigInteger.valueOf(0))!=0){c.a=c.a.divide(d); c.b=c.b.divide(d);}return c;}Number div(Number x) {Number c=new Number();c.b=b.multiply(x.a);c.a=a.multiply(x.b);BigInteger d=c.a.gcd(c.b);if (d.compareTo(BigInteger.valueOf(0))!=0){c.a=c.a.divide(d); c.b=c.b.divide(d);}return c;}int com(Number x) {BigInteger p=a.multiply(x.b);BigInteger q=x.a.multiply(b);if (p.compareTo(BigInteger.valueOf(0))<0) p=p.multiply(BigInteger.valueOf(-1));if (q.compareTo(BigInteger.valueOf(0))<0) q=q.multiply(BigInteger.valueOf(-1));return p.compareTo(q);}
}
public class Main {public static boolean Guss(int n,Number a[][],Number b[]){int k=1,col=1;while (k<=n && col<=n) {int max_r=k;for (int i=k+1;i<=n;i++) if (a[i][col].com(a[max_r][col])>0)max_r=i;if (a[max_r][col].com(new Number(BigInteger.valueOf(0),BigInteger.valueOf(1)))==0) return false;if (k!=max_r) {for (int j=col;j<=n;j++) {Number tmp=a[k][j];a[k][j]=a[max_r][j];a[max_r][j]=tmp;}Number tmp=b[k]; b[k]=b[max_r]; b[max_r]=tmp;}b[k]=b[k].div(a[k][col]);for (int j=col+1;j<=n;j++) a[k][j]=a[k][j].div(a[k][col]);a[k][col].a=BigInteger.valueOf(1);a[k][col].b=BigInteger.valueOf(1);for (int i=1;i<=n;i++) {if (i!=k) {b[i]=b[i].sub(b[k].mul(a[i][col]));for (int j=col+1;j<=n;j++) a[i][j]=a[i][j].sub(a[k][j].mul(a[i][col]));a[i][col].a=BigInteger.valueOf(0);}}k++; col++;}return true;}public static void main(String[] args) {Number a[][] = new Number[105][105];Number b[] = new Number[105];for (int i=1;i<=100;i++) {for (int j=1;j<=100;j++) a[i][j]=new Number();b[i]=new Number();}int n;Scanner in = new Scanner(System.in);while (in.hasNext()) {n=in.nextInt();for (int i=1;i<=n;i++){for (int j=1;j<=n;j++){a[i][j].a = in.nextBigInteger();a[i][j].b = BigInteger.valueOf(1);}b[i].a=in.nextBigInteger();b[i].b=BigInteger.valueOf(1);}if (Guss(n,a,b)==true) {for (int i=1;i<=n;i++) {BigInteger d=b[i].a.gcd(b[i].b);if (d.compareTo(BigInteger.valueOf(0))!=0){b[i].a=b[i].a.divide(d); b[i].b=b[i].b.divide(d);}//  System.out.println(1+" "+b[i].b+" "+b[i].b.compareTo(BigInteger.valueOf(0)));if (b[i].b.compareTo(BigInteger.valueOf(0))<0){//  System.out.println("*");b[i].b=b[i].b.multiply(BigInteger.valueOf(-1));b[i].a=b[i].a.multiply(BigInteger.valueOf(-1));}//  System.out.println(2+" "+b[i].b+" "+b[i].b.compareTo(BigInteger.valueOf(0)));if (b[i].a.compareTo(BigInteger.valueOf(0))==0) b[i].b=BigInteger.valueOf(1);if (b[i].b.compareTo(BigInteger.valueOf(1))==0) System.out.println(b[i].a);else System.out.println(b[i].a+"/"+b[i].b);}} else System.out.println("No solution.");System.out.println();}}
}

View Code

 

转载于:https://www.cnblogs.com/tetew/p/11317599.html

ICPC2008哈尔滨-E-Gauss Elimination相关推荐

  1. C语言gauss elimination高斯消元法算法(附完整源码)

    gauss elimination算法 实现以下几个相关接口 实现gauss elimination算法的完整源码(定义,实现,main函数测试) 实现以下几个相关接口 void display(fl ...

  2. Gauss elimination Template

    Gauss elimination : #include <iostream> #include <cstdlib> #include <cstring> #inc ...

  3. 高斯消元法java语言设计_高斯消元法(Gauss Elimination)【超详解模板】

    高斯消元法,是线性代数中的一个算法,可用来求解线性方程组,并可以求出矩阵的秩,以及求出可逆方阵的逆矩阵. 高斯消元法的原理是: 若用初等行变换将增广矩阵 化为 ,则AX = B与CX = D是同解方程 ...

  4. Gauss Elimination算法分析与实现

    2019独角兽企业重金招聘Python工程师标准>>> 高斯消去法分为两个过程:第一步是前向消元(forward elimination),也就是将系数矩阵转化成上三角矩阵的过程:第 ...

  5. 高斯消元法(Gauss Elimination)

    是线性代数中的一个算法. 可用来求解线性方程组,可以求出矩阵的秩和可逆方阵的逆矩阵. 通过逐步消除未知数来将原始线性系统转化为另一个更简单的等价系统. 原理:用初等行变换将增广矩阵转换为行阶梯矩阵,然 ...

  6. ICPC2008哈尔滨-A-Array Without Local Maximums

    题目描述 Ivan unexpectedly saw a present from one of his previous birthdays. It is array of n numbers fr ...

  7. 线性代数-矩阵-【5】矩阵化简 C和C++实现

    点击这里可以跳转至 [1]矩阵汇总:http://www.cnblogs.com/HongYi-Liang/p/7287369.html [2]矩阵生成:http://www.cnblogs.com/ ...

  8. c# lu分解的代码_LU分解(1)

    1/6 LU 分解 LU 分解可以写成A = LU,这里的L代表下三角矩阵,U代表上三角矩阵.对应的matlab代码如下: function[L, U] =zlu(A) % ZLU - LU deco ...

  9. 【001】Visual Studio

    叨叨在前:后天我们有个期末考试,内容是关于专业的一些计算程序设计,现在我要做的就是在这大概一天半的时间里把几个程序过完加背完, 下面是我的流水记录相关问题,我发现这样记录,我学得快一点,不知道为啥. ...

最新文章

  1. JVM 垃圾收集器CMS相关参数
  2. Keras之小众需求:自定义优化器
  3. Spring boot 默认静态资源路径与手动配置访问路径的方法
  4. SAP Spartacus 4.0 源代码模式下开启 SSR,为什么会从本地去加载 all.css?
  5. 如何使用SAP Gigya的登录服务和您的网站集成
  6. [systemd]systemd使用
  7. 电子测量与仪器第四版pdf_准确选择表面粗糙度仪的测量参数应该从哪些地方着手好【电子仪器吧】...
  8. PyCharm,IDEA配置mongo插件
  9. android 磁盘日志记录,GitHub - xflyandroid/XLog: 一个简易的日志打印框架(支持打印策略自定义,默认提供2种策略:logcat打印和磁盘打印)...
  10. 使用python读取excel
  11. WEBPACK+ES6+REACT入门(7/7)-React中绑定文本框与state中的值
  12. php获取qqkey源码,易语言取QQKEY源码
  13. 云服务器安全使用原则
  14. SSL基础:12:查询证书详细信息
  15. [ARC093-F][容斥原理][DP]Dark Horse
  16. easyui ValidateBox validType验证规则
  17. 华为回应出售手机业务传闻:假消息;微软将ChatGPT整合到更多工具中:不用写代码就能开发应用;苹果更新Mac产品线|极客头条
  18. Vue引用第三方动画库animate.css
  19. 搭建自己的dns服务器
  20. tsp matlab,TSP matlab

热门文章

  1. 七天学会ASP.NET MVC(七)——创建单页应用
  2. android 解压版sdk安装的问题
  3. Flex Application里的addChild()
  4. 通过圆的颜色并结合霍夫变换检测目标圆的OpenCV代码
  5. linux编辑器翻页,Linux的Vim编辑器的使用Part1:输入模式、移动光标和翻页
  6. C++中类的静态成员变量和静态成员函数
  7. C语言主应用程序在哪设置,哪位师傅知道51单片机怎样编写子程序?C语言的。在主程序里调...
  8. OPENSSL_Uplink(0098E000,07): no OPENSSL_Applink 错误分析
  9. windows下的mongodb安装与配置
  10. 搭建“双11”大型网站架构必须掌握的 5 个核心知识