因为侵犯慕课网版权而被投诉了可还行……
第五章 编程题
1
参考答案
#include <iostream>
#include <cstring>
using namespace std;
int main(){
char str[80];
int count = 0;
cin >> str;
for (int i = 0;i < strlen(str);i++){
if (str[i] >= 'a' && str[i] <= 'z'){
str[i] += 'A' - 'a';
count++;
}
}
cout << str << endl << count;
}
2
参考答案
#include <iostream>
using namespace std;
int main(){
int array[10];
for (int i = 0;i < 10;i++){
cin >> array[i];
}
int search;
bool thereBe = false;
cin >> search;
for (int i = 0;i < 10;i++){
if (array[i] == search){
thereBe = true;
cout << i + 1 << endl;
}
}
if(!thereBe)
cout << "数组中没有该数";
}
3
参考答案
#include <iostream>
using namespace std;
int main(){
float score[3][5];
for (int i = 0; i < 3;i++){
float sum = 0;
for (int j = 0; j < 3; j++){
cin >> score[i][j];
sum += score[i][j];
}
score[i][3] = sum;
score[i][4] = sum / 3;
}
int rank[3];
if (score[1][3] > score[2][3]){
rank[0] = 1;
rank[1] = 2;
}
else {
rank[0] = 2;
rank[1] = 1;
}
if (score[0][3] > score[ rank[0] ][3]){
rank[1] = rank[0];
rank[0] = 0;
}else if (score[0][3] > score[ rank[1] ][3]){
rank[2] = rank[1];
rank[1] = 0;
}else rank[2] = 0;
for (int i = 0; i < 3; i++){
for (int j = 0; j < 5; j++){
cout << score[ rank[i] ][j] << " ";
}
cout << i + 1 << " " << endl;
}
}
第六章 编程题
1
参考答案
#include <iostream>
using namespace std;
int main(){
char str1[80], str2[40];
int p;
for (int i = 0; i < 80; i++){
str1[i] = 0;
}
cin >> str1 >> str2 >> p;
char *point = &str1[p];
char *pt;
for (int i = 0; str2[i] != 0; i++){
for (pt = str1; *pt; pt++);
for (; pt != point; pt--){
*pt = *(pt - 1);
}
*point = str2[i];
point++;
}
cout << str1;
}
2
参考答案
#include <iostream>
#include <cstring>
using namespace std;
int main(){
char xnum[40];
int tnum = 0, sxtn = 1;
cin >> xnum;
char *point = &xnum[strlen(xnum) - 1];
while (*point != '\0'){
if (*point >= '0' && *point <= '9')
tnum += (*point - '0') * sxtn;
else if (*point >= 'A' && *point <= 'F')
tnum += (*point - 'A' + 10) * sxtn;
else if (*point >= 'a' && *point <= 'f')
tnum += (*point - 'a' + 10) * sxtn;
else if (*point == '-')
tnum = -tnum;
else {
cout << "input data error!" << endl;
return 0;
}
sxtn *= 16;
point--;
}
cout << tnum << endl;
}
3
参考答案
#include <iostream>
using namespace std;
int main(){
char str[40];
int sum[64];
for (int i = 0; i <<