[大学计算] 自测3 答案

两点间的折半距离

设P1、P2为平面上的两点,坐标分别为(x1, y1)和(x2, y2),
定义P1和P2之间的折半距离为:image
根据给定的两点坐标x1、y1、x2、y2,利用上述公式计算两点间
的折半距离,并将结果存入变量d中。
说明:涉及的开根号、求平方运算,可以用cmath库中sqrt、pow函数实现,
如sqrt(2)是计算根号2、pow(6, 2)是计算6的平方

输入格式:
在一行中输入4个浮点数(即两点坐标),中间用空格分开,
分别输入给变量x1、y1、x2、y2。
输出格式:
在一行中输出d的值。
输入样例:
3 8 6 4
输出样例:
2.50


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

初始代码:

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
    double x1, y1, x2, y2, d;
    cin >> x1 >> y1 >> x2 >> y2;
    /********Program********/
	
	
    /********  End  ********/
    cout << setprecision(2) << fixed;
    cout << d << endl;
    return 0;
} 

答案:

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


int main()
{
    double x1, y1, x2, y2, d;
    cin >> x1 >> y1 >> x2 >> y2;
    /********Program********/
    d = sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2)) / 2;
    

    /********  End  ********/
    cout << setprecision(2) << fixed;
    cout << d << endl;
    return 0;
}

计算小明的骑行总路程

小明周一至周五每天骑行250公里,周六周日不骑行。
假设从周x(其中1<=x<=7,周1-周7分别代表周一至周日)开始算起,
过了n天以后,问小明一共累计骑行了多少公里?
请完成下面的函数int calc(int x, int n),函数返回小明的骑行总路程。


输入格式:
输入包含两个整数 x, n,其中,周x表示周几,n天表示过了n天。
输出格式:
针对输入,打印出一个整数,表示小明累计骑行了多少公里。
输入样例:
3 10
输出样例:
2000


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

初始代码:

#include <iostream>
using namespace std;
int calc(int x, int n) 
{
    /********Program********/
	
	
    /********  End  ********/
}
int main()
{
    unsigned long long n,ans=0; 
    int x;
    cin >> x >> n; 
    ans = calc(x, n);
    cout << ans; 
    return 0;
}

答案:

#include <iostream>
using namespace std;

int calc(int x, int n) 
{
    /********Program********/
	int cnt = 0;
    for (int i = 0; i < n; i ++)
    {
        if ((x + i) % 7 == 0 || (x + i) % 7 == 6)
        {
            continue;
        }
        cnt ++;
    }
    return cnt * 250;
	
	
    /********  End  ********/
}

int main()
{
    unsigned long long n,ans=0; 
    int x;
    cin >> x >> n; 
    ans = calc(x, n);
    cout << ans; 
    return 0;
}

1出现的位置

完成函数int calc(int n)的功能,参数n是一个正整数,
该函数判断n中首次出现1(从左至右,第一位为1)的位置并返回;
若n中没有1,则返回-1。
提示:如果需要可以调用pow函数,pow(x, y)表示计算x的y次方。


输入格式:
输入在一行中给出1个正整数,输入给a(a>0)。
输出格式:
在一行中输出首次出现1的位置。
输入样例:
3423134
输出样例:
5


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

初始代码:

#include <iostream>
#include <cmath>
using namespace std;
int calc(int n)
{
    /********Program********/
	
	
    /********  End  ********/
}
int main()
{
    int a;
    cin >> a;
    cout << calc(a) << endl;
    return 0;
} 

答案:

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

int calc(int n)
{
	/********Program********/
    int a[1000] = {};
    int cnt = 0;
    while (n)
    {
        a[++cnt] = n % 10;
        n /= 10;
    }
    for (int i = cnt; i >= 1; i --)
    {
        if (a[i] == 1)
        {
            return cnt - i + 1;
        }
    }
    return -1;
	
	
    /********  End  ********/
}

int main()
{
    int a;
    cin >> a;
    cout << calc(a) << endl;
    return 0;
}

时间计算

我们一般用几小时几分几秒来描述一段时间,为方便计算则一般
用多少小时来表示,现要求编写完成下面的函数
double hours(int h, int m, int s);
完成从几小时几分几秒到多少小时(double类型)的转换,
如1小时30分0秒可以转换成1.5小时。
说明:参数h、m和s分别表示时、分、秒。


输入格式:
输入在一行中给出三个非负整数a,b,c表示a小时b分c秒,中间用空格分开。
输出格式:
在一行中输出1个浮点数表示转换成的小时数。
输入样例:
1 30 0
输出样例:
1.5


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

初始代码:

#include <iostream>
using namespace std;
double hours(int h, int m, int s);
/**********Program**********/


/**********  End  **********/
int main()
{
    int h, m, s;
    cin >> h >> m >> s;
    cout << hours(h, m, s);
    return 0;
}

答案:

#include <iostream>
using namespace std;

double hours(int h, int m, int s);

/**********Program**********/
double hours(int h, int m, int s)
{
    m += s / 60;
    s %= 60;
    h += m / 60;
    m %= 60;
    return h + (double)m / 60 + (double)s / 3600;
}


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

int main()
{
    int h, m, s;
    cin >> h >> m >> s;
    cout << hours(h, m, s);
    return 0;
}

阶乘尾数有几个连续的 0

我们来思考这个问题:给定非负整数 n,计算 n! 最后面连续出现的 0 的数目 f(n)
例如: 5! = 120,因此 f(5) = 1
10! = 3628800,所以 f(10)=2

提示:不要试图计算 n! 的实际值


输入格式: 非负整数 n
输出格式:
在一行中输出f(n)的值。
输入样例:
100
输出样例:
24


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

初始代码:

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


/********  End  ********/
}
int main()
{
  unsigned int n, m;
    cin >> n ;    
    cout << f(n)<<endl;
    return 0;
} 

答案:

#include <iostream>
using namespace std;

unsigned int f(unsigned int n)
{
/********Program********/
  int cnt2 = 0, cnt5 = 0;
  for (int i = 1; i <= n; i ++)
  {
    int a = i;
    while (a % 5 == 0)
    {
      cnt5 ++;
      a /= 5;
    }
    while (a % 2 == 0)
    {
      cnt2 ++;
      a /= 2;
    }
  }
  return min(cnt2, cnt5);


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

int main()
{
  unsigned int n, m;
    cin >> n ;    
    cout << f(n)<<endl;
    return 0;
}

连续退步最大次数

为统计小明考试成绩退步的情况,请实现函数
int getMaxRetrogressCount(int scores[], int n),
该函数功能是统计小明 n 次考试中分数连续降低的最大次数,并返回.
参数说明:在数组 scores 中传入所有考试的分数,n 为考试总次数(n>1)
例如:小明考试6次的成绩分别为 50、72、65、94、93、90
分数连续降低的最大情况是94、93、90,所以连续退步最大次数是2


输入样例:输入10次考试的成绩,各个成绩用空格进行分隔,例如:
50 34 44 60 50 48 36 36 32 62
输出样例:输出10次考试的连续退步最大次数,例如
3
(对应60 50 48 36这几次的分数,注意后续的36 32这两次不符合题目要求)


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

初始代码:

#include <iostream>
using namespace std;
#define SIZE 10
int getMaxRetrogressCount(int scores[], int n)
{
/********Program********/


/********  End  ********/    
}
int main()
{
   int scores[SIZE] = {0};
    for(int i=0; i<SIZE; i++) {
        cin>>scores[i];
    }
    int ret = getMaxRetrogressCount(scores, SIZE);
    cout << ret << endl;
    return 0;
} 

答案:


#include <iostream>

using namespace std;
#define SIZE 10

int getMaxRetrogressCount(int scores[], int n)
{
/********Program********/
	int res = 0;
	for (int i = 0; i < n - 1; i ++)
	{
		int cnt = 0;
		for (int j = i; j < n - 1; j ++)
		{
			if (scores[j] > scores[j + 1])
			{
				cnt ++;
			}
			else
			{
				break;
			}
		}
		res = max(res, cnt);
	}
	return res;
/********  End  ********/	
}

int main()
{
    int scores[SIZE] = {0};
	for(int i=0; i<SIZE; i++) {
		cin>>scores[i];
	}
	
	int ret = getMaxRetrogressCount(scores, SIZE);
	cout << ret << endl;

	return 0;
}

查找共同前缀

英语单词的前缀构成有一定的规律,例如"incorrect"和
"inability"有相同前缀"in"。请完成如下的C++函数功能:
findCommonPrefix(char *word1, char * word2, char *prefix)
该函数分析得到word1和word2两个单词的最长共同前缀,
分析结果存到参数prefix中(题目保证prefix所指向空间足够大)。
例如,"disadvantage"和"disagree"的最长共同前缀是"disa",
"disadvantage"和"misunderstand"的最长共同前缀是空串。
提示:可能用到的字符串函数说明如下,
strlen(char *str):返回字符串str的长度;
strcpy(char *dest, char *src):把字符串src复制到dest;
strcat(char *dest, char *src):把字符串src连接到dest后面;
char *strstr(const char *s1, const char *s2):从s1中查找子串s2第一次出现的位置。


输入格式:
输入在一行中给出两个英文单词(单词内部不含空格等分隔字符),以空格进行分隔。
输出格式:
在一行中输出两个英文单词的最长共同前缀。
输入样例:
disadvantage disagree
输出样例:
disa


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

初始代码:

#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
void findCommonPrefix(char *word1, char *word2, char *prefix)
{
/**********Program**********/


/**********  End  **********/
}
int main()
{
    char word1[100], word2[100];
    char prefix[100];
    cin >> word1 >> word2;
    findCommonPrefix(word1, word2, prefix);
    cout << prefix << endl;
    return 0;
} 

答案:

#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

void findCommonPrefix(char *word1, char *word2, char *prefix)
{
/**********Program**********/
	memset(prefix, 0, sizeof(prefix));
	int len1 = strlen(word1);
	int len2 = strlen(word2);
	for (int i = 0; i < min(len1, len2); i ++)
	{
		if (word1[i] == word2[i])
		{
			prefix[i] = word1[i];
		}
		else
		{
			prefix[i] = '\0';
			return;
		}
	}


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

int main()
{
	char word1[100], word2[100];
	char prefix[100];
	cin >> word1 >> word2;
	findCommonPrefix(word1, word2, prefix);
	cout << prefix << endl;
	return 0;
}

单词简写

给定一个复合词,该复合词的简写是将复合词中所有单词的首字母大写后再连接在一起。
例如,复合词"artificial intelligence"的简写为"AI","express mail service"
的简写为"EMS","People's Liberation Army"的简写为"PLA"。

请完成函数void Abbreviate(char compoundWord[], char abbrWord[]),计算复合
词compoundWord的简写,并将其存在abbrWord中。
提示:strlen(char str[])函数可以返回str中所存储字符串的长度。


输入格式:
输入在一行中给出一个字符串,表示复合词,复合词的长度小于100,复合词中的单词个数
大于等于2且小于10,复合词中的单词之间以1个空格分隔。

输出格式:
输出一行表示该复合词对应的简写。

输入样例:
express mail service

输出样例:
EMS


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

初始代码:

#include <iostream>
#include <cstring>
using namespace std;
void Abbreviate(char compoundWord[], char abbrWord[])
{
/********Program********/


/********  End  ********/
}
int main()
{
    char compoundWord[110], abbrWord[20];
    cin.getline(compoundWord, 110);
    Abbreviate(compoundWord, abbrWord);
    cout << abbrWord << endl;
    return 0;
} 

答案:

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

void Abbreviate(char compoundWord[], char abbrWord[])
{
/********Program********/
    bool judge = 0;
    int len = 0;
    for (int i = 0; compoundWord[i] != '\0'; i ++)
    {
        if (judge == 0)
        {
            if (compoundWord[i] <= 'z' && compoundWord[i] >= 'a')
            {
                abbrWord[len ++] = compoundWord[i] - 'a' + 'A';
            }
            else
            {
                abbrWord[len ++] = compoundWord[i];
            }
            judge = 1;
        }
        if (compoundWord[i] == ' ')
        {
            judge = 0;
        }
    }
    abbrWord[len] = '\0';


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

int main()
{
    char compoundWord[110], abbrWord[20];
    cin.getline(compoundWord, 110);

    Abbreviate(compoundWord, abbrWord);

    cout << abbrWord << endl;

    return 0;
}

演讲比赛的成绩

参加演讲比赛的选手信息用如下结构partinfo表示。
比赛分成少年组和青年组(用type字段进行区分),
每位选手的最终成绩由初赛成绩s1,复赛成绩s2,决赛成绩s3构成,
这三部分的成绩分别占最终成绩的20%、30%、50%。
请编写get函数,该函数返回青年组最低得分选手的参赛编号。
其中,参数lists是参赛选手信息构成的结构数组,参数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
输出样例:
5


注意:仅在标有”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;        // 决赛成绩
};
int 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;        // 决赛成绩
};

int get(partinfo list[], int n)
{
    /**********Program**********/
    float Min = 100000;
    int res;
    for (int i = 0; i < n; i ++)
    {
        if (list[i].type == 'q')
        {
            float tot = list[i].s1 * 0.2 + list[i].s2 * 0.3 + list[i].s3 * 0.5;
            if (Min > tot)
            {
                Min = tot;
                res = list[i].id;
            }
        }
    }
    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;
}

删除孪生素数

下面有结构类型node的定义,指针head中存放着node节点组成的
单向链表的首节点地址。函数node* deleteTwin(node* head, int t)
的功能是将head所指向的链表中素数t对应的孪生素数删除,并返回新链表。
孪生素数就是指相差2的素数对,例如3和5,5和7,11和13.

说明:题目所给数据中,链表已有节点的数和t都是素数,
即无需判断是否为素数。


输入格式:
输入的第一行包含一个整数n和n个素数(n>=0),分别表示给定的
链表的长度和链表所包含的n个素数。
输入的第二行包含一个整数k和k个素数(k>=1),分别表示待处理
的素数的个数和待处理的k个素数。

输出格式:
输出为删除所有给定素数的孪生素数之后的链表。


输入样例:
5 2 3 5 11 17
2 7 13
输出样例:
2->3->17

初始代码:

#include <iostream>
using namespace std;
struct node {
    int data;
    node* next;
};
node* deleteTwin(node* head, int t) 
{
    /********Program********/
	
	
    /********  End  ********/
}
void output(node* head);
int main()
{
    int n;
    cin >> n;
    node* head = NULL, * cur = NULL, * tmp;
    for (int i = 0; i < n; i++)
    {
        tmp = new node;
        cin >> tmp->data;
        tmp->next = NULL;
		if (head == NULL) {
            head = tmp;
            cur = head;
        }
        else {
            cur->next = tmp;
            cur = cur->next;
        }
    }
    int k, t;
    cin >> k;
    for (int i = 0; i < k; i++)
    {
        cin >> t;
        head = deleteTwin(head, t);
    }
    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* deleteTwin(node* head, int t) 
{
	/********Program********/
	node * ptr = head;
	node * pre = NULL;
	while (ptr != NULL)
	{
		if (ptr -> data == t + 2 || ptr -> data == t - 2)
		{
			if (ptr == head)
			{
				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()
{
	int n;
	cin >> n;
	node* head = NULL, * cur = NULL, * tmp;
	for (int i = 0; i < n; i++)
	{
		tmp = new node;
		cin >> tmp->data;
		tmp->next = NULL;
		if (head == NULL) {
			head = tmp;
			cur = head;
		}
		else {
			cur->next = tmp;
			cur = cur->next;
		}
	}

	int k, t;
	cin >> k;
	for (int i = 0; i < k; i++)
	{
		cin >> t;
		head = deleteTwin(head, t);
	}
	output(head);
	return 0;
}

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