简单的算法题
问题1
代码示例:
#include <iostream>
#include <iomanip>
using namespace std;
//主函数
void main()
{
int i;
int f[21] = {0,1,0,1};
int j, k, max, min;
//求各元素的值
for (i=4;i<21;i++){
f[i]=f[i-1]+2*f[i-2]+f[i-3];
}
//输出结果
cout<<"The results:"<<endl;
for(i=1;i<21;i++){
if (i%5==0){
cout<<endl;
}
cout<<setw(10)<<f[i];
}
//求最大最大元素
max = min = f[1];
k = j = 1;
for (i = 2; i < 21;i++) {
if (max<f[i]) {
max = f[i];
k = i;
}
if (min>f[i]) {
min = f[i];
j = i;
}
}
//输出最大最小元素
cout << "\nmax:f[" << k << "]=" << max << ",min:f[" << j << "]" << min << '\n';
}
结果:
分析:
本题主要考察一维数组的应用,然后就是求最大最小值,总体难度不大。
问题2
代码分析:
#include <iostream>
using namespace std;
//定义函数
void del_ch(char *p,char ch) {
char *q = p;
for (; *p != '\0';p++) {
if (*p!=ch) {
*q = *p;
q++;
}
}
*q = '\0';//添加字符串的结尾
}
//主函数
void main()
{
void del_ch(char*, char);
char str[80], ch;
cout << "Input a string:\n";
cin.getline(str,80,'\n');
cout << "Input the char deleted:\n";
cin>>ch;
del_ch(str, ch);
cout << "Then new string is:\n";
cout << str << endl;
}
结果:
分析:
问题3
#include <iostream>
using namespace std;
//定义结构体变量
struct student {
char id[8];//学号
char name[10];//姓名
bool sex;//性别
float score;//成绩
};
//定义结构体数组
student st[3];
//定义输入函数
void input(int n) {
cout << "从键盘输入" << n << "个同学的记录:" << endl;
int i, k;
student x;
for (i=0;i<n;i++) {
cin>>x.id;
cin >> x.name;
cin >> k;
if (k == 1) {
x.sex = true;
}
else {
x.sex = false;
}
cin >> x.score;
st[i] = x;
}
}
//定义输出函数
void output(int n) {
cout << "输出" << n << "个同学的记录:" << endl;
cout << "--------------------------------------------------" << endl;
cout << "学号" << '\t' << "姓名" << '\t' << "性别" << '\t' << "成绩" << endl;
for (int i = 0; i < n;i++) {
cout << st[i].id << '\t' << st[i].name << '\t';
if (st[i].sex==true) {
cout << "male" << '\t';
}
else {
cout << "female" << '\t';
}
cout << st[i].score<<endl;
}
}
//主函数
void main()
{
int n;
cout << "请输入学生的个数:";
cin >> n;
input(n);
output(n);
}
结果:
问题4
建立一个链表,实现链表的增添数据和删除数据的操作
代码示例:
#include <iostream>
using namespace std;
//定义节点结构体变量
struct node{
int data;
struct node* next;
};
//定义创建链表的函数
void creat(node *&head,int n) {//head为头指针变量
if (n==0) {
head = NULL;
return;
}
cout << "从键盘输入" << n << "个整数:";
int x;
node *last, *p;
last = head = NULL;
p = new node;
cin >> x;
p->data = x;
p->next = NULL;
head = last = p;
if (n==1) {
return;
}
for (int i = 0; i < n - 1;i++) {
p = new node;
last->next = p;
cin >> x;
p->data = x;
last = p;
}
p->next = NULL;
}
//输出链表的函数
void traverse(node *head) {
node* p;
p = head;
while (p!=NULL) {
cout << p->data << " " ;
p = p->next;
}
cout << endl;
}
//删除节点的函数
void erase(node* &head,int no) {
node* p, * p1;
p = p1 = head;
if (head==NULL) {
cout << "链表为空,无节点可删" << endl;
return;
}
else {
if (p->data==no) {//删除头节点
head = p->next;
p = p->next;
}
else {//不是头节点则往下循环
while (p->data!=no) {
p1 = p;
p = p->next;
}
if (p==NULL) {
cout << "链表中没有要删除的节点" << endl;;
}
else {//找到删除的节点
p1->next = p->next;
delete p;//释放删除节点的空间
cout << "删除节点成功!" << endl;
}
}
}
}
//主函数
void main()
{
node *head = NULL;
int n;
cout << "请输入节点的个数:";
cin >> n;
creat(head,n);//创建链表
traverse(head);//输出链表各节点的数据
cout << "输入要删除的数:";
cin >> n;
erase(head,n);//删除某个节点
traverse(head); //输出删除该节点后的链表各节点的数据
}
结果: