ACM竞赛学习整理--Gauss求解POJ1166
這道題目和1830比較類似,1830是求解的個數,這道題目相當于求線性方程組的整數解。
題目主要內容:有9個鐘,其中9個操作方法來扳動上面的指針,每個操作每次只能把指針移動90度,且每個操作是對一組鐘進行操作(即執行每個操作之后有幾個鐘的狀態被改變了90度而不是單獨某個鐘的狀態改變了90度)。操作與其影響的鐘的對應表如表一所示:
表一
題目要求的是給出這9個鐘的初始狀態(0表示指向12點,1表示指向3點,2表示指向6點,3表示指向6點)用操作1-9采用最少的移動次數操作這些鐘,使它們都指向12點。
思路:根據操作與鐘表的關系我們可以構造一個關系矩陣,如表二所示
行表示鐘,列表示操作,例如,操作1,2,4被采用之后A都會受到影響,移動一步(即90度)。所以題目的意思就是求操作1-9都被操作了多少次,使得每個鐘表從初始狀態被調整到12點的那個狀態,即每個鐘表的狀態變化=操作1操作次數+操作2操作次數+…+操作9*操作次數。
#include <iostream> #include <math.h> #include<fstream> #include<cstdlib> using namespace std;class Mat { public:Mat(int x, int y, double d[]) {rows = x;cols = y;if (x<y - 1)cout << "Not enough" << endl;int c = 0;for (int i = 0; i<x; i++) {for (int j = 0; j<y; j++) {data[i][j] = d[c];c++;}}}void print();int matcols();int matrows();void gauss();void change(int x);void result();void value();void threeDeterminant(); private:int rows;int cols;double data[200][200];};void Mat::print() {for (int i = 0; i<rows; i++) {for (int j = 0; j<cols; j++) {cout.width(4);cout << data[i][j] << " ";}cout << endl;}cout << endl; }int Mat::matcols() {return cols; }int Mat::matrows() {return rows; }void Mat::gauss() {double m = 0;double flag = 0;for (int i = 0; i<rows; i++) {change(i);print();if (data[i][i] != 0) {for (int j = i + 1; j<rows; j++) {//m = 1;if (data[j][i] == 0)continue;m = fabs(1.0 * data[i][i] / data[j][i]);cout<<data[i][i];cout<<endl;cout<<data[j][i];cout<<endl;if ((data[j][i] > 0 && data[i][i] > 0) || (data[j][i] < 0 && data[i][i] < 0)) {flag = 1;}else{flag = -1;}for (int k = i; k<cols; k++) {data[j][k] = data[j][k] * m - flag*data[i][k];}print();}}} }void Mat::change(int x) {double temp = 0;double max = fabs(data[x][x]);int loc = x;for (int i = x; i < rows; i++) {if (fabs(data[i][x]) > max) {max = fabs(data[i][x]);loc = i;}}if (x != loc) {for (int i = x; i < cols; i++) {temp = data[x][i];data[x][i] = data[loc][i];data[loc][i] = temp;}} }void Mat:: value(){ double value = 1; for (int i = 1; i < cols; i++) {value = data[i-1][i-1]*value; }cout << value<< " "; cout<<endl; }void Mat:: threeDeterminant() {double value = 0;value = data[0][0]*data[1][1]*data[2][2]+ data[1][0]*data[2][1]*data[0][2]+ data[2][0]*data[0][1]*data[1][2]- data[2][0]*data[1][1]*data[0][2]- data[1][0]*data[0][1]*data[2][2]- data[0][0]*data[2][1]*data[1][2];cout<<"Determinant = "<< value<<endl; }void Mat::result() {double ans[100] = {0};double sum = 0;for (int i = 1; i < cols; i++) {sum = data[cols - 1- i][cols -1 ];for (int j = 1; j < i; j++) {sum -= ans[cols - j] * data[rows - i][rows - j];}ans[cols - i] = sum / data[cols-1 - i][cols - 1 - i];}for (int i = 1; i < cols; i++) {cout << ans[i] << " ";} }int main(int argc, char** argv) {double v[100000] = {};int n;int cc; ifstream infile("1.txt",ios::in);int count =0;infile>>n;while(infile){infile>>cc;v[count] = cc;count++;}Mat a(n, n+1, v);a.print(); // a.threeDeterminant();a.gauss();a.print();a.result();cout<<endl;a.value();return 0; }1.TXT 文件內容
9
1 1 0 1 0 0 0 0 0 12
1 1 1 0 1 0 0 0 0 12
0 1 1 0 0 1 0 0 0 12
1 0 0 1 1 0 1 0 0 12
1 0 1 0 1 0 1 0 1 12
0 0 0 1 0 0 1 1 0 12
0 0 0 0 1 0 1 1 1 12
0 0 0 0 0 1 0 1 1 12
ps: 求解線性方程組的方法有很多,Gauss方法也是手段之一。
總結
以上是生活随笔為你收集整理的ACM竞赛学习整理--Gauss求解POJ1166的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ACM竞赛学习整理--矩阵运算
- 下一篇: Gauss 消元法求解线性方程组