c++学习笔记(5)
二维数组:
数组声明: int matrix[2][2] = {{1,2}, {3,4}}
数组索引: 嵌套循环:
二维数组初始化+随机洗牌(reshuflle)
code:
#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <time.h>
using namespace std;
const int ROW_SIZE = 10;
const int COL_SIZE = 10;
void printMatrix(int matrix[][COL_SIZE], int row, int col);
void reshuffleMatrix(int matrix[][COL_SIZE], int, int);
int matrixSum(int matrix[][COL_SIZE], int, int);
int main(int argc, char *argv[])
{
//srand(time(0));
int matrix[ROW_SIZE][COL_SIZE];
for(int row=0; row<ROW_SIZE; row++)
{
for(int col=0; col<COL_SIZE; col++)
{
matrix[row][col] = row + col;
}
}
cout << "The matrix before reshuffle: " << endl;
printMatrix(matrix, ROW_SIZE, COL_SIZE);
reshuffleMatrix(matrix, ROW_SIZE, COL_SIZE);
cout << "The matrix after reshuffle: " << endl;
printMatrix(matrix, ROW_SIZE, COL_SIZE);
int mat_sum = matrixSum(matrix, ROW_SIZE, COL_SIZE);
cout << "The sum of the matrix is " << mat_sum << endl;
return 0;
}
void printMatrix(int matrix[][COL_SIZE], int row, int col)
{
for(int i=0; i<row; i++)
{
for(int j=0; j<col; j++)
{
cout << setw(4) << matrix[i][j] << " ";
}
cout << endl;
}
}
void reshuffleMatrix(int matrix[][COL_SIZE], int row, int col)
{
srand(time(0));
for(int i=0; i<ROW_SIZE; i++)
{
for(int j=0; j<COL_SIZE; j++)
{
int rand_i = rand() % ROW_SIZE; //
int rand_j = rand() % COL_SIZE;
// swap the matrix
int tmp = matrix[i][j];
matrix[i][j] = matrix[rand_i][rand_j];
matrix[rand_i][rand_j] = tmp;
}
}
}
int matrixSum(int matrix[][COL_SIZE], int row, int col)
{
int sum = 0;
for(int i=0; i<ROW_SIZE; i++)
{
for(int j=0; j<COL_SIZE; j++)
{
sum += matrix[i][j];
}
}
return sum;
}
二维数组做为函数参数:
C++规定,二维数组作为函数参数,必须声明二维数组的列的大小。
问题:找最近邻点对。
#include <iostream>
#include <cmath>
using namespace std;
double getDistance(double, double, double, double);
int main(int argc, char *argv[])
{
const int NUMBER_OF_POINT = 8;
double point[NUMBER_OF_POINT][2];
// 输入点的数据
cout << "Enter " << NUMBER_OF_POINT << " Points." << endl;
for(int i=0; i<NUMBER_OF_POINT; i++)
{
cin >> point[i][0] >> point[i][1];
}
int p1 = 0;
int p2 = 1;
double shortest_distance = getDistance(point[p1][0], point[p1][1], point[p2][0], point[p2][1]);
// 计算点之间的距离
for(int i=0; i<NUMBER_OF_POINT-1; i++)
{
for(int j=i+1; j<NUMBER_OF_POINT; j++)
{
double tmp_distance = getDistance(point[i][0], point[i][1], point[j][0], point[j][1]);
cout << "Distance is " << tmp_distance << endl;
if(tmp_distance < shortest_distance)
{
shortest_distance = tmp_distance;
p1 = i; // 记录下坐标
p2 = j;
}
}
}
cout << "The shotrst distance is " << shortest_distance << endl;
cout << "The point is " << "(" << point[p1][0] << "," << point[p1][1] << ")" << " and " << "(" << point[p2][0] << "," << point[p2][1] << ")" << endl;
return 0;
}
double getDistance(double x1, double y1, double x2, double y2)
{
return sqrt(pow(x1-x2, 2) + pow(y1-y2, 2));
}
结果:
改进: 如果有多个最小点怎么找到
数度游戏
给定一个数独问题的解,验证其是否正确
即需要验证:
(1)每一行的姐是否满足条件
(2)每一列的解是否满足条件
(3)每一个3X3的网格内是否满足条件
#include <iostream>
#include <cmath>
using namespace std;
bool isValid(int, int, int grid[][9]);
bool isValid(int grid[][9]);
int main(int argc, char *argv[])
{
int solution[9][9] = {
{5,3,4,6,7,8,9,1,2},
{6,7,2,1,9,5,3,4,8},
{1,9,8,3,4,2,5,6,7},
{8,5,9,7,6,1,4,2,3},
{4,2,6,8,5,3,7,9,1},
{7,1,3,9,2,4,8,5,6},
{9,6,1,5,3,7,2,8,4},
{2,8,7,4,1,9,6,3,5},
{3,4,5,2,8,6,1,7,9}
};
if(isValid(solution))
{
cout << "The Sudoku solution is legal " << endl;
}
else
{
cout << "The Sudoku solution is illegal" << endl;
}
return 0;
}
bool isValid(int i, int j, int grid[][9])
{
// 判断数独的解是否合法
for(int col=0; col<9; col++) // 判断第i行
{
if(col!=j && grid[i][j]==grid[i][col])
return false;
}
for(int row=0; row<9; row++) // 判断第j列
{
if(row!=i && grid[row][j]==grid[i][j])
return false;
}
for(int row=(i/3)*3; row<(i/3)*3+3; row++) // 注意这里的处理方法,很重要
{
for(int col=(j/3)*3; col<(j/3)*3+3; col++)
{
if(row!=i && col!=j && grid[row][col]==grid[i][j])
return false;
}
}
return true;
}
bool isValid(int grid[][9])
{
for(int i=0; i<9; i++)
{
for(int j=0; j<9; j++)
{
if(grid[i][j]<1 || grid[i][j] >9 || !isValid(i, j, grid))
return false;
}
}
return true;
}
多维数组作为参数:
猜生日:多维数组解决
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main(int argc, char *argv[])
{
int day = 0;
char answer;
int dates[5][4][4] = {
{
{1,3,5,7},
{9,11,13,15},
{17,19,21,23},
{25,27,29,31},
},
{
{2,3,6,7},
{10,11,14,15},
{18,19,22,23},
{26,27,30,31}
},
{
{4,5,6,7},
{12,13,14,15},
{20,21,22,23},
{28,29,30,31}
},
{
{8,9,10,11},
{12,13,14,15},
{24,25,26,27},
{28,29,30,31}
},
{
{16,17,18,19},
{20,21,22,23},
{24,25,26,27},
{28,29,30,31}
}
};
for(int i=0; i<5; i++)
{
cout << "Is your birthday in set ?" << endl;
for(int j=0; j<4; j++)
{
for(int k=0; k<4; k++)
{
cout << setw(3) << dates[i][j][k];
}
cout << endl;
}
cout << endl << "Enter yes or no(y/n)" << endl;
cin >> answer;
if(answer=='y' || answer=='Y')
day += dates[i][0][0];
}
cout << "Your birthday is " << day << endl;
return 0;
}