第二次上机作业
第一题
#include<iostream>
using namespace std;
int main() {
int a[6][1];
int m = 0, n = 0, t;
while (m < 6) {
cin >> a[m][0];
++m;
}//输入一个数组
m = 0;
while (m < 6) {
t = a[0][0];
if (a[m][0] >= t) {
t = a[m][0];
}
++m;
}
while (n < 6) {
if (a[n][0] == t)
break;
++n;
}
a[n][0] = a[0][0];
a[0][0] = t;
m = 0;
while (m < 6) {
cout << a[m][0] << endl;
++m;
}//输出数组
return 0;
}
第二题
#include<iostream>
using namespace std;
int main() {
int a[6][6];
int i, j, t = 0;
for (i = 0; i < 6; ++i) {
for (j = 0; j < 6; ++j) {
cout << "a[" << i << "][" << j << "]";
cin >> a[i][j];
}
}
for (i = 0; i < 6; ++i) {
for (j = 0; j < 6; +j) {
if (i + j < 6) {
t += a[i][j];
}
}
}
cout << "上三角形区域元素的和=" << t;
t = 0;
for (i = 0; i < 6; ++i) {
for (j = 0; j < 6; ++j) {
if (i + j > 5) {
t += a[i][j];
}
}
}
cout << "下三角形区域元素的和=" << t;
t = 0;
for (i = 0; i < 6; ++i) {
j = i;
t += a[i][j];
}
cout << "主对角线所有元素的和=" << t;
return 0;
}
第三题
#include<iostream>
using namespace std;
int main() {
string sentence = "Madam,I'm adam";
string sen;
int i = 0;
for (; i < 14; ++i) {
if (isalpha(sentence[i])) {
sen += sentence[i];
}
}
for (i = 0; i < 7; ++i) {
if (sen[i] != sen[13 - i])
break;
}
if (i = 7) {
cout << "It's a palindrome" << endl;
}
else {
cout << "It's not a palindrome" << endl;
}
return 0;
}
第四题
#include<iostream>
#include<vector>
using namespace std;
int main() {
int m, n, a; //n为人数,m为随机数
int i = 0;
cin >> n >> m;
vector<bool> v(n);
if (n >= m) {
for (; i < n; ++i) {
if (n % m == 0) {
a = m;
auto v(n).pop_top(a);
}
}
}
else {
//不会,没写
}
return 0;
}