1.二维数组实现3×3矩阵的行、列之和。a[3][3]
int a[3][3];
int col[3] = { 0, };
int rol[3] = { 0, };
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
cin >> a[i][j];
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
cout << a[i][j]<<' ' ;
col[i] += a[i][j];
rol[j] += a[i][j];
}
cout << endl;
}
for (int i = 0; i < 3; i++)
{
cout << "列col [" << i << "] " << col[i] << endl;
}
for (int i = 0; i < 3; i++)
{
cout << "行col [" << i << "] " << rol[i] << endl;
}
1.数组在定义时必须进行初始化,否则会出现乱码。
2.数组a[10]中元素依次向前移,a[7]=a[6]、a[6]=a[5]...
int a[10];
cout << "请输入数字:" << endl;
for (int i = 0; i < 10; ++i)
{
cin >> a[i];
}
int temp = 0;
show(a);
temp = a[9];
for (int i = 9; i >=1; --i) //与向后变换不同这里向前变换,即下标更小的方向。
{
a[i] = a[i-1];
}
a[0] = temp;
show(a);
2.向下标更小的方向的变换。
3.100盏灯默认熄灭,100个小孩,第一个小孩开关倍数为1的灯,
第二个小孩开关倍数为2的灯,
第三个小孩开关倍数为3的灯、、
最后求所有灯的亮灭情况```
bool liang[101];
for (int i = 0; i <=100; ++i)
{
liang[i] =false;
cout << i << '\t';
}
//cout <<"赋值结束"<< endl;
for (int i = 1; i <= 100; ++i)
{
// cout << "外层循环!" << endl;
for (int j = 1; j <= 100; ++j)
{
// cout << "内层循环! " << endl;
if (j % i == 0)
liang[i] =!liang[i];
}
}
for (int i = 1; i <=100; ++i)
{
if ((i - 1) % 10 == 0)
cout << endl;
if (liang[i])
cout << "暗" << '\t';
else
cout << "亮" << '\t';
}
3.1 用`数组liang[101]`来存储,因为每次需要用到下标进行计算,所以不能用100;
3.2 运用%和/操作时,不能出现相关值不得取0;