[大学计算] 小测2 答案

氨基酸的平均原子量

蛋白质中的氨基酸是由氧、碳、氮、硫、氢5种元素构成,
这些元素的相对原子量如下表示:
————————
元素 相对原子量
————————
氧(O) 15.9994
碳(C) 12.011
氮(N) 14.0067
硫(S) 32.066
氢(H) 1.0079
————————
请完成一个计算某氨基酸的平均原子量的函数,该函数原型是:
double averageAtomicWeight(int O, int C, int N, int S, int H);
即输入的5个参数O、C、N、S、H分别表示该氨基酸中氧、碳、氮、硫、氢的原子数量,函数返回平均相对原子量的值。

例如,甘氨酸(O2C2NH5)中含2个氧原子、2个碳原子、1个氮原子、0个硫原子、5个氢原子,所以甘氨酸的平均相对原子量为7.5067。


输入格式:
在一行中输入氨基酸中氧、碳、氮、硫、氢的5种原子数量,
中间用空格分开,分别输入给变量O, C, N, S, H。

输出格式:
在一行中输出该氨基酸的平均原子量的值。

输入样例:
2 2 1 0 5
输出样例:
7.5067


注意:仅在标有”Program”和”End”的注释行之间补充填写代码。
请勿改动主函数main和其它任何已有内容。

初始代码:

#include <iostream>
#include <iomanip>
using namespace std;
double averageAtomicWeight(int O, int C, int N, int S, int H)
{
    /********Program********/
	
	
    /********  End  ********/
}
int main()
{   
    int O, C, N, S, H;
    cin >> O >> C >> N >> S >> H;
    double ret = averageAtomicWeight(O, C, N, S, H);
    cout << setprecision(4) << fixed << ret << endl;
    return 0;
}

答案:

#include <iostream>
#include <iomanip>
using namespace std;

double averageAtomicWeight(int O, int C, int N, int S, int H)
{
    /********Program********/
	double res = 0;
	res += O * 15.9994;
	res += C * 12.011;
	res += N * 14.0067;
	res += S * 32.066;
	res += H * 1.0079;
	return res / (O + C + N + S + H);
    
	
    /********  End  ********/
}

int main()
{   
	int O, C, N, S, H;
	cin >> O >> C >> N >> S >> H;
	double ret = averageAtomicWeight(O, C, N, S, H);
	cout << setprecision(4) << fixed << ret << endl;
	return 0;
}

最大公约数函数和最小公倍数函数

实现计算两个正整数的最大公约数函数gcd和最小公倍数函数lcm。


输入格式:
输入包含两个正整数,中间用空格分开,分别输入给变量a, b。
输出格式:
输出这两个正整数的大公约数和最小公倍数,中间用空格分开。
输入样例:
12 3
输出样例:
3 12


注意:仅在标有”Program”和”End”的注释行之间补充填写代码。
请勿改动主函数main和其它任何已有内容。

初始代码:

#include <iostream>
using namespace std;
//计算a和b的最大公约数
int gcd(int a, int b)
{
    /********Program********/
	
	
    /********  End  ********/
}
//计算a和b的最小公倍数
int lcm(int a, int b)
{
    /********Program********/
	
	
    /********  End  ********/
}
int main()
{
    int a, b;
    cin >> a >> b; 
    cout << gcd(a, b) << " " << lcm(a, b) << endl; 
    return 0;
}

答案:

#include <iostream>
using namespace std;

//计算a和b的最大公约数
int gcd(int a, int b)
{
    /********Program********/
	return a % b == 0 ? b : gcd(b, a % b);
	
    /********  End  ********/
}

//计算a和b的最小公倍数
int lcm(int a, int b)
{
    /********Program********/
	return a * b / gcd(a, b);
	
    /********  End  ********/
}

int main()
{
    int a, b;
    cin >> a >> b; 
    cout << gcd(a, b) << " " << lcm(a, b) << endl; 
    return 0;
}

Hamonic数

第n个Hamonic数的计算公式为:1+1/2+…+1/n,请完成函数double hamonic(n),计算第n个Hamonic数。例如,
hamonic(10)=2.92897,hamonic(10000)=9.78761。


输入格式:
输入包含一个正整数,输入给变量n。
输出格式:
输出对应的Hamonic数。
输入样例:
10
输出样例:
2.92897


注意:仅在标有”Program”和”End”的注释行之间补充填写代码。
请勿改动主函数main和其它任何已有内容。

初始代码:

#include <iostream>
using namespace std;
double hamonic(int n) 
{
/**********Program**********/


/**********  End  **********/
}
int main()
{
    int n;
    cin >> n;
    cout << hamonic(n) << endl;
    return 0;
} 

答案:

#include <iostream>
using namespace std;

double hamonic(int n) 
{
/**********Program**********/
    double res = 0;
    for (int i = 1; i <= n; i ++)
    {
        res += (double)1 / i;
    }
    return res;
	
/**********  End  **********/
}


int main()
{
    int n;
	cin >> n;
    cout << hamonic(n) << endl;
    return 0;
}

图像反色

反色是数字图像处理中的基本操作之一。对于只有黑白两种颜色的图像,
用0表示黑色,1表示白色,反色即是将黑色变为白色、白色变为黑色;
而对于怀旧的老照片,则是由256种不同浓淡的灰色描绘的,
一般用整数0~255编码表示这256种灰色,反色即是将某种
灰色变为它的补色,例如:12代表的灰色,它的补色则是255-12=243代表的灰色。
请实现函数int antiColor(int type,int color);
根据type的值计算color的反色值,type为0表示黑白两色图像,
type为1表示256灰度图像,type为其他值时函数返回-1。


输入格式:
输入包含两个整数,中间用空格分开,分别输入给变量type, color。
输出格式:
输出对应的反色值。
输入样例:
1 12
输出样例:
243


注意:仅在标有”Program”和”End”的注释行之间补充填写代码。
请勿改动主函数main和其它任何已有内容。

初始代码:

#include <iostream>
using namespace std;
int antiColor(int type, int color);
/********Program********/


/********  End  ********/
int main()
{
    int type, color;
    cin >> type >> color;
    cout << antiColor(type, color) << endl;
    return 0;
} 

答案:

#include <iostream>
using namespace std;

int antiColor(int type, int color);

/********Program********/
int antiColor(int type, int color)
{
    if (type == 0)
    {
        return !color;
    }
    if (type == 1)
    {
        return 255 - color;
    }
    return -1;
}


/********  End  ********/

int main()
{
    int type, color;
    cin >> type >> color;
    cout << antiColor(type, color) << endl;
    return 0;
}

递归输出

编写递归函数void convert(ostream &out, unsigned long num),
实现将无符号整数num的各位数字之间插入字符’-‘输出。
例如,如果num为1234,则输出为1-2-3-4。
说明:参数out为输出流,其使用方式与标准输出流cout一样,
例如,语句out<<num表示向流out中输出num的值。

递归提示:要实现1234按要求输出,可以考虑先把123按要求输出,
最后再输出字符'-'和4。


输入格式:
输入包含一个无符号整数,输入给变量A。
输出格式:
A的各位数字之间插入字符'-'输出。
输入样例:
1234
输出样例:
1-2-3-4


注意:仅在标有”Program”和”End”的注释行之间补充填写代码。
请勿改动主函数main和其它任何已有内容。

初始代码:

#include <iostream>
using namespace std;
void convert(ostream &out, unsigned long x)
{ 
/*********Program*********/


/*********  End  *********/
}
int main()
{
    unsigned long A;
    cin >> A;
    convert(cout, A);
    cout << endl;
    return 0;
}

答案:

#include <iostream>
using namespace std;

void convert(ostream &out, unsigned long x)
{ 
/*********Program*********/
    if (x == 0)
    {
        return;
    }
    convert(out, x / 10);
    if (x / 10 == 0)
    {
        out << x % 10;
    }
    else
    {
        cout << '-' << x % 10;
    }
	
	
/*********  End  *********/
}

int main()
{
    unsigned long A;
	cin >> A;
    convert(cout, A);
	cout << endl;
    return 0;
}

查找均值最接近元素

函数closed_index的功能是在长度为 len 的浮点数数组 A 中寻找与均值最接近元素的下标。
注意:如果这样的元素不止一个,返回满足条件的最小下标。
提示:计算一个数的绝对值可以使用cmath中定义的fabs函数。


输入格式:
输入在一行中给出10个浮点数,以空格进行分隔。
输出格式:
输出符合要求的元素的下标。
样例数据:
22.1 90.6 96.3 31.9 78.4 24.6 49.1 68.8 46.4 70.6
输出样例:
6


注意:仅在标有”Program”和”End”的注释行之间补充填写代码。
请勿改动主函数main和其它任何已有内容。

初始代码:

#include <iostream>
#include <cmath>
using namespace std;
#define N 10
int closed_index(double A[], int len) 
{
/**********Program**********/


/**********  End  **********/
}
int main()
{
    double A[N];
    for (int i = 0; i < N; i++)
        cin >> A[i];
    cout << closed_index(A, N) << endl;
    return 0;
} 

答案:

#include <iostream>
#include <cmath>
using namespace std;

#define N 10

int closed_index(double A[], int len) 
{
/**********Program**********/
    double ave = 0;
	for (int i = 0; i < len; i ++)
	{
		ave += A[i];
	}
	ave /= len;
	double Min = 100000;
	int index;
	for (int i = 0; i < len; i ++)
	{
		if (fabs(ave - A[i]) < Min)
		{
			Min = fabs(ave - A[i]);
			index = i;
		}
	}
	return index;
	
	
/**********  End  **********/
}

int main()
{
	double A[N];
	for (int i = 0; i < N; i++)
		cin >> A[i];
    cout << closed_index(A, N) << endl;
	return 0;
} 

逆序数

函数rev_num (int A[], int len) 的功能是计算数组 A
的“逆序数”,其中 len 是数组的长度。设 a 是 A 中的元素,
则 a 的“逆序数”定义为 A 中出现在 a 之后(索引值大于a的
索引值的所有元素)比 a 大的元素数目;而 A 的逆序数则是 A
中所有元素逆序数之和。


输入格式:
输入在一行中给出10个整数,以空格进行分隔,并存储到数组A。
输出格式:
数组A的“逆序数”。
样例数据:
22 90 96 31 78 24 49 68 46 70
输出样例:
23


注意:仅在标有”Program”和”End”的注释行之间补充填写代码。
请勿改动主函数main和其它任何已有内容。

初始代码:

#include <iostream>
#include <cmath>
using namespace std;
#define N 10
int rev_num(int A[], int len) 
{
/**********Program**********/


/**********  End  **********/
}
int main()
{
    int A[N];
    for (int i = 0; i < N; i++)
        cin >> A[i];
    cout << rev_num(A, N) << endl;
    return 0;
}

答案:

#include <iostream>
#include <cmath>
using namespace std;

#define N 10

int rev_num(int A[], int len) 
{
/**********Program**********/
	int cnt = 0;
    for (int i = 0; i < len - 1; i ++)
	{
		for (int j = i + 1; j < len; j ++)
		{
			if (A[i] < A[j])
			{
				cnt ++;
			}
		}
	}
	return cnt;
	

/**********  End  **********/
}

int main()
{
	
	int A[N];
	for (int i = 0; i < N; i++)
		cin >> A[i];
    cout << rev_num(A, N) << endl;
	return 0;
}

查找字符最后位置

函数find_last的功能是在给定的字符串str中查找字符ch,
如果包含字符ch,则返回该字符在str中最后一次出现的下标,
否则返回-1。需要注意下标是从0开始。
提示:可能用到的字符串函数说明如下,
strlen(char *str):返回字符串str的长度;
strchr(char *str, char ch):从str中查找第一个字符ch的位置;
strrchr(char *str, char ch):从str中查找最后一个字符ch的位置。


输入格式:
输入在一行中给出一个字符串(内部不含空格等分隔字符)和一个字符。
输出格式:
输出该字符在目标字符串中出现的最后位置。
样例数据:
asdfasdefgrhsaaad f
输出样例:
8


注意:仅在标有”Program”和”End”的注释行之间补充填写代码。
请勿改动主函数main和其它任何已有内容。

初始代码:

#include <iostream>
using namespace std;
int find_last(char *str, char ch)
{
/**********Program**********/


/**********  End  **********/
}
int main()
{
    char str[200];
    char ch;
    cin >> str;
    cin >> ch;
    cout << find_last(str, ch) << endl;
    return 0;
} 

答案:


#include <iostream>
using namespace std;

int find_last(char *str, char ch)
{
/**********Program**********/
	int len = 0;
	for (len = 0; str[len] != '\0'; len ++);
	for (int i = len - 1; i >= 0; i --)
	{
		if (str[i] == ch)
		{
			return i;
		}
	}
	return -1;
	
	
/**********  End  **********/
}

int main()
{
	char str[200];
	char ch;
	cin >> str;
	cin >> ch;
    cout << find_last(str, ch) << endl;
	return 0;
}

分析演讲比赛的成绩

参加演讲比赛的选手信息用如下结构partinfo表示。
比赛分成少年组和青年组(用type字段进行区分),
每位选手的最终成绩由初赛成绩s1、复赛成绩s2、决赛成绩s3构成,
这三部分的成绩分别占最终成绩的30%、30%、40%。
请编写get函数,该函数返回少年组最低得分选手的姓名。
其中,参数list是参赛选手信息构成的结构数组,参数n是参赛选手的数量。
说明:题目所给数据中既有少年组的选手,也有青年组的选手,
且无需考虑总成绩相等的情况。


样例数据:
1, “Zhangsan”, ‘s’, 94, 97, 91
2, “Lisi”, ‘q’, 90, 86, 90
3, “Wangwu”, ‘s’, 94, 88, 91
4, “Zhangliu”, ‘s’, 92, 95, 81
5, “Xuqi”, ‘q’, 90, 92, 85
输出样例:
Zhangliu


注意:仅在标有”Program”和”End”的注释行之间补充填写代码。
请勿改动主函数main和其它任何已有内容。

初始代码:

#include <iostream>
using namespace std;
#define SIZE 5
struct partinfo {
    int  id;         // 参赛编号
    char name[10];   // 姓名
    char type;       // 组别:'s'、'q'分别代表少年组和青年组
    float s1;        // 初赛成绩
    float s2;        // 复赛成绩
    float s3;        // 决赛成绩
};
char* get(partinfo list[], int n)
{
    /**********Program**********/
	
	
    /**********  End  **********/
}
int main()
{
   partinfo list[SIZE];
    for (int i = 0; i < SIZE; i++)
        cin >> list[i].id >> list[i].name >> list[i].type >> list[i].s1 >> list[i].s2 >> list[i].s3;
    cout<< get(list, SIZE)<<endl;
    return 0;
} 

答案:


#include <iostream>
using namespace std;
#define SIZE 5

struct partinfo {
    int  id;         // 参赛编号
    char name[10];   // 姓名
    char type;       // 组别:'s'、'q'分别代表少年组和青年组
    float s1;        // 初赛成绩
    float s2;        // 复赛成绩
    float s3;        // 决赛成绩
};

char* get(partinfo list[], int n)
{
	/**********Program**********/
    double Min = 1000000;
    char * res;
    for (int i = 0; i < n; i ++)
    {
        if(list[i].type == 's')
        {
            float tot = list[i].s1 * 0.3 + list[i].s2 * 0.3 + list[i].s3 * 0.4;
            // cout << tot << endl;
            if (tot < Min)
            {
                Min = tot;
                res = list[i].name;
            }
        }
    }
    return res;

	
	/**********  End  **********/
}

int main()
{
    partinfo list[SIZE];
	for (int i = 0; i < SIZE; i++)
        cin >> list[i].id >> list[i].name >> list[i].type >> list[i].s1 >> list[i].s2 >> list[i].s3;
	
    cout<< get(list, SIZE)<<endl;
	return 0;
}

删除链表素数节点

函数deletesushu删除head指向的链表中data值为素数的
结点并返回删除结点后的新链表。例如链表8->7->5->3->2->9->6->4->1->0
删除data值为素数得到的链表为:8->9->6->4->1->0。
请实现该函数。
说明:素数必须大于等于2。


输入格式:
输入包含10个整数,用于构造一个单向链表。
输出格式:
输出为删除素数节点之后的链表。
输入样例:
8 7 5 3 2 9 6 4 1 0
输出样例:
8->9->6->4->1->0


注意:仅在标有”Program”和”End”的注释行之间补充填写代码。
请勿改动主函数main和其它任何已有内容。

初始代码:

#include <iostream>
using namespace std;
struct node
{
    int data;
    node *next;
};
node *deletesushu(node *head)
{
/**********Program**********/


/**********  End  **********/
}
void output(node* head);
int main()
{
    node* head = NULL, * cur = NULL, * tmp;
    for (int i = 0; i < 10; i++)
    {
        tmp = new node;
        cin >> tmp->data;
        tmp->next = NULL;
        if (head == NULL) {
            head = tmp;
            cur = head;
        }
        else {
            cur->next = tmp;
            cur = cur->next;
        }
    }
    head = deletesushu(head);
    output(head);
    return 0;
}

void output(node* head) {
    while (head)
    {
        cout << head->data;
        head = head->next;
        if (head) cout << "->";
    }
    cout << endl;
} 

答案:

#include <iostream>
using namespace std;

struct node
{
    int data;
    node *next;
};

node *deletesushu(node *head)
{
/**********Program**********/
    node * ptr = head;
	node * pre = NULL;
	while (ptr != NULL)
	{
		int a = ptr -> data;
		bool judge = 1;
		if (a != 0 && a != 1)
		{
			for (int i = 2; i <= a / 2; i ++)
			{
				if (a % i == 0)
				{
					judge = 0;
					break;
				}
			}
		}
		else
		{
			judge = 0;
		}
		if (judge == 1)
		{
			if (pre == NULL)
			{
				head = head -> next;
				ptr = ptr -> next;
			}
			else
			{
				pre -> next = ptr -> next;
				ptr = ptr -> next;
			}
		}
		else
		{
			pre = ptr;
			ptr = ptr -> next;
		}
	}
	return head;
	
	
/**********  End  **********/
}

void output(node* head);

int main()
{
	node* head = NULL, * cur = NULL, * tmp;
	for (int i = 0; i < 10; i++)
	{
		tmp = new node;
		cin >> tmp->data;
		tmp->next = NULL;
		if (head == NULL) {
			head = tmp;
			cur = head;
		}
		else {
			cur->next = tmp;
			cur = cur->next;
		}
	}

	head = deletesushu(head);
	output(head);
    return 0;
}

void output(node* head) {
	while (head)
	{
		cout << head->data;
		head = head->next;
		if (head) cout << "->";
	}
	cout << endl;
} 
posted @ 2024-05-16 00:47  RuntimeError-J  阅读(96)  评论(0)    收藏  举报