程序设计1-10题
注: 数据来源网学天地,如有侵权,告知删除。 练习程序用!
11. 合并数组的字符串
问题
答案
#include <iostream>
using namespace std;
int main() {
string strs[5] = {"AAAAA","BBBBB","PPPPP"};
string zong = "";
for(int i;i<3; i++) {
zong+=strs[i];
}
cout<<zong;
return 0;
}
运行结果:
12. 二维数组数据按列存到一维数组
问题
答案
#include <iostream>
using namespace std;
// 处理二维数组为数组到一维
void zhuanHuan(int a[][3], int data[]) {
int k=0;
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
data[k++] = a[j][i];
}
}
}
int main() {
int a[3][3] = {
{1,2,3},
{4,5,6},
{7,8,9}
};
int data[100]={0};
zhuanHuan(a,data);
int i=0;
while(data[i]!=0) {
cout<<data[i]<<ends;
i++;
}
return 0;
}
运行结果:
13. 二维数组数据按行存到一维数组
问题
答案
#include <iostream>
using namespace std;
// 处理二维数组为数组到一维
void zhuanHuan(int a[][3], int data[]) {
int k=0;
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
data[k++] = a[i][j];
}
}
}
int main() {
int a[3][3] = {
{1,2,3},
{4,5,6},
{7,8,9}
};
int data[100]={0};
zhuanHuan(a,data);
int i=0;
while(data[i]!=0) {
cout<<data[i]<<ends;
i++;
}
return 0;
}
运行结果:
14. 判断是不是回文串
问题
答案
#include <iostream>
#include <string>
using namespace std;
// 判断是不是回文的函数
int isHuiWen(string str);
int main() {
string str;
int n; // 用于接收判断返回值
cout<<"请输入一个字符串:";
cin>>str;
n = isHuiWen(str);
cout<<(n==1?"YES":"NO");
}
int isHuiWen(string str) {
for(int i=0; i<str.length()/2; i++) {
if(str[i] != str[str.length()-i-1]) {
return 0; // 不是回文
}
}
return 1; // 是回文
}
运行结果:
15. 单链表找出最高分
问题
答案
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef struct stu{
int score;
struct stu* next;// 下一个学生
} stu, *StuLinkList;
// 寻找最大分数函数
void findMaxFenShu(StuLinkList T){
stu *s= T->next;// 第一个结点赋值
int max_score = s->score;
s = s->next;
while(s!=NULL) {
if(s->score>max_score) {
max_score = s->score;
}
// 结点后移
s = s->next;
}
cout<<max_score;
}
int main() {
StuLinkList stus;
// 分配空间
stus = new stu;
stus->next = NULL;
// 生成模拟数据
for(int i=0; i<7; i++) {
stu *p = new stu;
p->score = rand()%100;
// 头插
p->next = stus->next;
stus->next = p;
}
// 展示模拟数据
stu *s= stus->next;
while(s != NULL) {
cout<<s->score<<ends;
s = s->next;
}
cout<<endl;
findMaxFenShu(stus);
}
运行结果:
16. 除了首尾中间字符进行排序
问题
答案
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
cout<<"请输入一个字符串:";
cin>>str;
// 简单选择排序
for(int i=1; i<str.length()-2; i++) {
int max_index = i; //记录最大的下标
for(int j=i+1; j<str.length()-1; j++) {
if(str[j]>str[max_index]) {
max_index = j;
}
}
// 交换
int temp = str[max_index];
str[max_index] = str[i];
str[i] = temp;
}
cout<<str;
return 0;
}
运行结果:
17. 检索指定成绩范围内的学生
问题
答案
#include <iostream>
#include <string>
using namespace std;
typedef struct stu{
string sno;//学号
int score; //成绩
} stu;
// 将指定成绩范围学生放入指定数组
void chuLi(stu stus[], stu stuT[], int low, int high, int n);
int main() {
// 学生成绩模拟数据
stu stus[15] = {
{"dsa", 15},{"dsa", 16},{"dsa", 71},{"dsa", 25},{"dsa", 35},{"dsa", 40},{"dsa", 42},
{"dsa", 50},{"dsa", 52},{"dsa", 60},{"dsa", 82},{"dsa", 12},{"dsa", 40},{"dsa", 90},
{"dsa", 100}
};
stu b[15];
int low, high;
cout<<"请输入low,high:";
cin>>low>>high;
// 处理
chuLi(stus, b,low, high, 15);
// 输出
int i=0, len=sizeof(b)/sizeof(b[0]);
while(i<len) {
cout<<b[i].sno<<":"<<b[i].score<<endl;
i++;
}
}
// 函数实现
void chuLi(stu stus[], stu stuT[], int low, int high, int n){
int i,k;
i=k=0;
while(i<n) {
if(stus[i].score>=low && stus[i].score<=high) {
stuT[k++] = stus[i];
}
i++;
}
};
运行结果:
18. 删除ascll码为奇数的字符
问题
答案
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
char t[100];
int k=0; // 指向字符数组
cout<<"请输入一个字符串:";
cin>>str;
for(int i=0; i<str.length(); i++) {
// 判断一下ascll为偶数的放入t
if(str[i]%2==0) {
t[k++] = str[i];
}
}
cout<<t;
}
运行结果:
19. 按指定规则合并函数
问题
答案
#include <iostream>
using namespace std;
// 处理函数
int fun(int a, int b);
int main() {
int a,b,c;
cout<<"请输入a,b的值:";
cin>>a>>b;
c = fun(a,b);
cout<<c;
}
// 函数实现
int fun(int a, int b) {
return (b%10)*1000 + (a/10)*100 + (b/10)*10 + (a%10);
}
运行结果:
20.
问题
答案
#include <iostream>
using namespace std;
int main() {
int n,m;// 标明二维数组的行列数
cout<<"请输入二维数组n,m:";
cin>>n>>m;
int data[n][m] = {// 假如输入的图片所示
{1,3,5,7,9},
{2,9,9,9,4},
{6,9,9,9,8},
{1,3,5,7,0}
};
int sum=0;
if(n==1) {
// 只有一行的时候
for(int i=0; i<n; i++) {
sum+=data[0][i];
}
}else if(m==1) {
// 只有一列
for(int i=0; i<n; i++) {
sum+=data[i][0];
}
}else {
for(int i=0; i<n; i++) {
// 计算两侧列
sum= sum + data[i][0] + data[i][m-1];
}
for(int j=1;j<m-1; j++) {
// 计算两行
sum = sum+ data[0][j] + data[n-1][j];
}
}
cout<<sum;
}
运行结果: