Created
September 18, 2015 18:59
-
-
Save gorakhargosh/b28b72a9fd776b1520b8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"math" | |
"math/big" | |
) | |
// readInt reads an integer from standard input. | |
func readInt() (int, error) { | |
var a int | |
_, err := fmt.Scan(&a) | |
return a, err | |
} | |
// readInts reads a slice of integers from standard input. | |
func readInts(n int) ([]int, error) { | |
li := make([]int, n) | |
p := make([]interface{}, n) | |
for i := 0; i < n; i++ { | |
p[i] = &li[i] | |
} | |
b, err := fmt.Scanln(p...) | |
li = li[:b] | |
return li, err | |
} | |
// readBigInt reads a big integer from the standard input. | |
func readBigInt() (*big.Int, error) { | |
w := new(big.Int) | |
_, err := fmt.Scan(w) | |
return w, err | |
} | |
// readBigInts reads a slice of big integers from standard input. | |
func readBigInts(n int) ([]*big.Int, error) { | |
li := make([]big.Int, n) | |
p := make([]interface{}, n) | |
for i := 0; i < n; i++ { | |
p[i] = &li[i] | |
} | |
b, err := fmt.Scanln(p...) | |
ret := make([]*big.Int, b) | |
for i := 0; i < b; i++ { | |
ret[i] = &li[i] | |
} | |
return ret, err | |
} | |
// sumInts calculates the sum of a slice of integers. | |
func sumInts(numbers []int) int { | |
ret := 0 | |
for i := 0; i < len(numbers); i++ { | |
ret += numbers[i] | |
} | |
return ret | |
} | |
// sumBigInts calculates the sum of a slice of big integers. | |
func sumBigInts(numbers []*big.Int) *big.Int { | |
var s = new(big.Int) | |
for i := 0; i < len(numbers); i++ { | |
s.Add(s, numbers[i]) | |
} | |
return s | |
} | |
type IndexTransform func(n, a, b int) (int, int) | |
// IdentityIndex returns the matrix indexes as are. | |
func IdentityIndex(n, a, b int) (int, int) { | |
return a, b | |
} | |
// InverseDiagonalIndex returns the inverse diagonal indexes. | |
func InverseDiagonalIndex(n, a, b int) (int, int) { | |
return a, n - b - 1 | |
} | |
type Matrix [][]int | |
// Diagonal returns the diagonal of the matrix. | |
func (m Matrix) Diagonal(which IndexTransform) []int { | |
n := len(m) | |
ret := make([]int, n) | |
for i, j := 0, 0; i < n; { | |
a, b := which(n, i, j) | |
ret = append(ret, m[a][b]) | |
i++ | |
j++ | |
} | |
return ret | |
} | |
// readIntSquarematrix reads a square matrix of size n of integers from standard | |
// input. | |
func readIntSquareMatrix(n int) (Matrix, error) { | |
ret := make(Matrix, n) | |
for i := 0; i < n; i++ { | |
ints, err := readInts(n) | |
if err != nil { | |
return nil, err | |
} | |
ret[i] = ints | |
} | |
return ret, nil | |
} | |
// diagonaldifference calculates the diagonal difference in a single loop. | |
func diagonalDifference(n int) int { | |
diff := 0 | |
for i := 0; i < n; i++ { | |
ints, err := readInts(n) | |
if err != nil { | |
log.Fatal(err) | |
} | |
diff += ints[i] - ints[n-i-1] | |
} | |
return int(math.Abs(float64(diff))) | |
} | |
func main() { | |
n, err := readInt() | |
if err != nil { | |
log.Fatal(err) | |
} | |
// m, err := readIntSquareMatrix(n) | |
// if err != nil { | |
// log.Fatal(err) | |
// } | |
// v := math.Abs(float64( | |
// sumInts(m.Diagonal(IdentityIndex)) - | |
// sumInts(m.Diagonal(InverseDiagonalIndex)))) | |
fmt.Println(diagonalDifference(n)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment