Missing number in matrix
来源:互联网 发布:药品物流配送软件 编辑:程序博客网 时间:2024/06/06 17:02
**
Missing number in matrix
**
matrix
Given a matrix A[][] of size N such that it has only one 0, Find the number to be placed in place of the 0 such that sum of the numbers in every row, column and two diagonals become equal.
Note: Diagonals should be only of the form A[i][i] and A[i][N-i-1]. If there is a matrix of size 1, with an element 0, then print 1.
Input:
First line contains the test cases, T . Then T test cases follow. Each test case contains a single integer N which represents the number of rows and columns of the matrix. Then in the next line are N*N space separated of the matrix A.
Output:
Display the number in place of 0 if possible, otherwise display -1. If there is a matrix of size 1, with an element 0, then print 1.
Constraints:
1<=T<=25
1<=n<=100
1<=ai<=1000000000
Example:
Input
225 5 5 031 2 0 3 1 2 2 3 1
Output:
5-1
Explanation:
(i)In the first test case the matrix is
5 55 0
Therefore If we place 5 instead of 0, all the element of matrix will become 5. Therefore row 5+5=10, column 5+5=10 and diagonal 5+5=10, all are equal.
(ii)Second sample, it is not possible to insert an element in place of 0 so that the condition is satisfied.thus result is -1.
简单题,不解释,好久没练了,熟悉下手感。代码如下:
#include<iostream>#include<string.h>#include<vector>//#include <cassert>#include <algorithm>#include <string>#include <iterator>using namespace std;class Cite_Sum{public: unsigned long long sum; bool Inf_Used; Cite_Sum(){sum=0;Inf_Used=0;} //bool operator == (const Cite_Sum & s);};bool operator == ( Cite_Sum & a,Cite_Sum & b){return a.sum==b.sum;} bool operator <( Cite_Sum& a,Cite_Sum& b){ return a.sum<b.sum;}bool compare(Cite_Sum& a,Cite_Sum& b){ return a.Inf_Used<b.Inf_Used; }int main(){ int Count,row,column; cin>>Count; while(Count--) { int N; int Infu_num=2; cin>>N; if (N==1) { unsigned long c; cin>>c; if (c==0) cout<<"1"<<endl; else cout<<"-1"<<endl; } else { vector<Cite_Sum> Su(2*N+2); int ps=1; unsigned long **p=new unsigned long*[N]; for (int i=0;i<N;i++) p[i]=new unsigned long[N]; for(int i=0;i<N;i++) { for(int j=0;j<N;j++) { cin>>p[i][j]; if (p[i][j]==0) { row=i; column=j; //行列对于哪些SUM有影响 Su[i].Inf_Used=1; Su[j+N].Inf_Used=1; if(i==N-j-1) { Su[2*N+1].Inf_Used=1; Infu_num++; } if(i==j) { Su[2*N].Inf_Used=1; Infu_num++; } } if(i==j) Su[2*N].sum+=p[i][j]; if(i==N-j-1) Su[2*N+1].sum+=p[i][j]; Su[i].sum+= p[i][j]; Su[j+N].sum+= p[i][j]; } } unsigned long long s=Su[row].sum; if(Su[row].sum==Su[column+N].sum) { ps=1; if(Su[2*N].Inf_Used==1&&Su[row].sum!=Su[2*N].sum) ps=0; if(Su[2*N+1].Inf_Used==1&&Su[row].sum!=Su[2*N+1].sum) ps=0; }//被影响行列的SUM是否相等 if (ps!=0) { //int s=Su[row].sum; vector<Cite_Sum>::iterator new_end; sort(Su.begin(),Su.end(),compare); while (Infu_num--) Su.pop_back(); new_end=unique(Su.begin(),Su.end()); //assert(Su.size()==N); Su.erase(new_end,Su.end()); int h=Su.size(); if (h!=1) ps=0; } if (ps!=0&&Su[0].sum>s) { unsigned long long re=Su[0].sum-s; cout<<re<<endl; } else cout<<"-1"<<endl; for (int i=0;i<N;i++) delete p[i]; } } return 0; }
- Missing number in matrix
- Missing number in C
- Find missing number in a sorted array
- 【Leetcode】:268. Missing Number 问题 in JAVA
- LintCode_401 Kth Smallest Number in Sorted Matrix
- Missing number
- Missing number
- Missing number
- Missing number
- Missing Number
- Missing Number
- Missing Number
- Missing Number.
- Missing Number
- Missing Number
- Missing Number
- Missing Number
- Missing Number
- windows下安装MySQL 5.7,创建数据库和数据库表
- CCF 2015 12-1 数位之和
- 【J2SE】IO初步
- gulp入门
- 判断第二个日期比第一个日期大
- Missing number in matrix
- Jedis连接Redis
- CodeForces 439D Devu and his Brother
- Tomcat的优化经验
- 网络复习日志1
- CCF 2016 4-1 折点计数
- 数据结构与算法分析笔记与总结(java实现)--链表问题面试笔试注意点
- 移动App测试一些崩溃原因及触发崩溃的场景
- HTTP请求的GET与POST方式的区别