转专业机试结束了,经过大一下用C语言写机考题目两次都是只写对两道的惨痛教训时候,大二上这次转专业我痛定思痛狂补C++,终于是让我在这次考试中写对了4道机考题目。面试的话这次软工12个名额机考才过了3个应该不会卡我吧?应该吧?
给未来参与浙工大转专业机考的各位同学衷心劝告,能用C++就用C++,C++的STL库可以为你机考做题节省大量时间,且C语言的输入输出(尤其是格式化输入)容易出错
CSDN上的往年真题练完后最好再去浙工大ACM平台练习把通关考例题刷完
题号 | 题目 |
问题 A | 最好的你 |
问题 B | 判断坐标 |
问题 C | 画字母M |
问题 D | 去除质数 |
问题 E | 分析文章 |
问题 A: 最好的你
题目描述
参加转专业测试的你在人群中一定是闪闪发光的那一个。请在屏幕上输出我对你的肯定和对你的祝福吧!
输入
无。
输出
按题目样本描述输出示范内容,行末回车。
样例输入
无。
样例输出
~ \You're the best!/ ~
=You will be succeed.=
代码
照例送分题,转义\即可
#include <bits/stdc++.h>
using namespace std;
int main(){
cout<<"~ \\You're the best!/ ~"<<endl<<"=You will be succeed.=";
return 0;
}
问题 B: 判断坐标
题目描述
整数直角坐标系中的一个位置被描述为由横坐标和纵坐标构成的一对值,可以使用(x,y)这样的形式描述。横纵坐标将直角坐标系分隔为7个不相交的部分,包括:第1象限,第2象限,第3象限,第4象限,x轴,y轴和原点。请你根据给定的坐标值判断位于直角坐标系的哪个部分?7个部分分别表示如下:
(1)第1象限: First quadrant
(2)第2象限: Second quadrant
(3)第3象限:Third quadrant
(4)第4象限: Forth quadrant
(5)x轴: X axis
(6)y轴: Y axis
(7)原点: Origin
输入
多组数据,每组数据一行,由一个坐标值构成,描述为(x,y)。横纵坐标分别用逗号分隔,可能会使用+/-符号,无多余空格,使用圆括号标记为一组。参考样本输入示范。
(所有坐标值均不超过int范围)
输出
对应每组输入,输出一行描述对应坐标位于直角坐标系的哪个部分。行末直接回车。参考样本输出示范
样例输入
(3,+3)
(-1,0)
(0,-0)
样例输出
First quadrant
X axis
Origin
代码
格式化输入或者字符串处理
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
while (cin >> s) {
int a = s.find('(');
int b = s.find(',');
int c = s.find(')');
int d = stoi(s.substr(a + 1, b - a - 1));
int e = stoi(s.substr(b + 1, c - b - 1));
if (d == 0 && e == 0) {
cout << "Origin" << endl;
} else if (d == 0) {
cout << "Y axis" << endl;
} else if (e == 0) {
cout << "X axis" << endl;
} else if (d > 0 && e > 0) {
cout << "First quadrant" << endl;
} else if (d < 0 && e > 0) {
cout << "Second quadrant" << endl;
} else if (d < 0 && e < 0) {
cout << "Third quadrant" << endl;
} else {
cout << "Forth quadrant" << endl;
}
}
return 0;
}
这里分享一位用C语言写代码的同学写的方法,其中这个代码最重要的就是一定要加上getchar否则会死循环 (说实话我也不太懂这是为什么,希望有评论区大佬可以解答一下)
#include <stdio.h>
int main() {
int a, b;
while (scanf("(%d,%d)", &a, &b) != EOF) {
getchar();
if (a == 0 && b == 0) {
printf("Origin\n");
} else if (a == 0) {
printf("Y axis\n");
} else if (b == 0) {
printf("X axis\n");
} else if (a > 0 && b > 0) {
printf("First quadrant\n");
} else if (a < 0 && b > 0) {
printf("Second quadrant\n");
} else if (a < 0 && b < 0) {
printf("Third quadrant\n");
} else if (a > 0 && b < 0) {
printf("Forth quadrant\n");
}
}
return 0;
}
问题 C: 画字母M
题目描述
小M想用计算机画自己的名字M,现在请你来帮达成这个心愿吧!
输入
多组数据输入,每组数据给出M的高度值H(Hϵ[4,60] ),字母的宽度为2H-1。参考样本输入示范。
输出
对于每组数据,画出一个对应的M,由‘*’构成,字母的画笔粗细单位为1。每行的最后一个‘*’后直接回车,每个M之后有一个空行。参考样本输出示范。
样例输入
4
5
样例输出
** **
* * * *
* * *
* *
** **
* * * *
* * * *
* * *
* *
代码
画图题,setw还挺好用的可以学学,如果不是空格的填充再用setfill修改
#include <bits/stdc++.h>
using namespace std;
int main() {
int num;
while (cin>>num) {
for (int i=0;i<num-2;i++) {
cout<<'*'<<setw(i+1)<<'*'<<setw(2*num-4-2*i)<<'*'<<setw(i+1)<<'*'<<endl;
}
cout<<'*'<<setw(num-1)<<'*'<<setw(num-1)<<'*'<<endl;
cout<<'*'<<setw(2*num-2)<<'*'<<endl;
}
return 0;
}
问题 D: 去除质数
题目描述
现在有个工作,需要挑拣两个正整数集合的所有非质数,现在请你来编程完成这个工作吧!
输入
多组数据输入,每组数据给定两个正整数集合,分别由两行给出。每行的第一个数字给出集合的数据个数,随后跟随对应个数的正整数。每个集合的数据规模不超过50,单个集合内无重复元素,集合的数据范围均不超过int。参考输入样本示范。
输出
对于每组数据,挑拣出两个集合所有的非质数,并按升序输出求解结果,使用逗号分隔。若没有非质数,那么输出" None " 。行末直接回车。参考输出样本示范。
样例输入
5 23 12 4 7 9
9 4 6 13 3 12 7 23 2 11
4 2 1 3 7
5 3 13 17 1 7
2 3 5
1 2
样例输出
4,6,9,12
1
None
代码
#include<bits/stdc++.h>
using namespace std;
bool pd(int a) {
if (a == 1) {
return false;
}
if (a == 2) {
return true;
}
for (int i = 2; i <= sqrt(a); i++) {
if (a % i == 0) {
return false;
}
}
return true;
}
int main() {
int num1, num2;
while (cin >> num1) {
int a[num1];
for (int i = 0; i < num1; i++) {
cin >> a[i];
if (pd(a[i])) {
a[i] = -1;
}
}
cin >> num2;
int b[num2];
for (int i = 0; i < num2; i++) {
cin >> b[i];
if (pd(b[i])) {
b[i] = -1;
}
}
int c[num1 + num2], s = 0;
for (int i = 0; i < num1; i++) {
if (a[i] == -1) {
continue;
}
c[s++] = a[i];
}
for (int i = 0; i < num2; i++) {
if (b[i] == -1) {
continue;
}
c[s++] = b[i];
}
if (s == 0) {
cout << "None" << endl;
continue;
}
sort(c, c + s);
for (int i = 0; i < s; i++) {
if ((i == s - 2 && c[i] == c[i + 1]) || ( i == s - 1 && c[i] != c[i - 1])) {
cout << c[s - 1] << endl;
continue;
}
if (i == 0) {
cout << c[i] << ',';
continue;
}
if (c[i] == c[i - 1]) {
continue;
}
cout << c[i] << ',';
}
}
return 0;
}
我自己考场写的代码有点复杂且去重判断有点复杂,我让AI用vector方法写了下
#include <bits/stdc++.h>
using namespace std;
// 判断是否为质数
bool isPrime(int a) {
if (a < 2) return false; // 1 和小于1的数都不是质数
if (a == 2) return true; // 2 是质数
if (a % 2 == 0) return false; // 除了 2 的偶数都不是质数
for (int i = 3; i * i <= a; i += 2) { // 只需检查奇数因子
if (a % i == 0) return false;
}
return true;
}
int main() {
int num1, num2;
while (cin >> num1) {
vector<int> c; // 存放结果的容器
for (int i = 0; i < num1; i++) {
int x;
cin >> x;
if (!isPrime(x)) c.push_back(x); // 非质数加入结果
}
cin >> num2;
for (int i = 0; i < num2; i++) {
int x;
cin >> x;
if (!isPrime(x)) c.push_back(x); // 非质数加入结果
}
if (c.empty()) { // 如果没有非质数
cout << "None" << endl;
continue;
}
sort(c.begin(), c.end()); // 排序
c.erase(unique(c.begin(), c.end()), c.end()); // 去重
for (size_t i = 0; i < c.size(); i++) {
if (i > 0) cout << ','; // 输出逗号分隔符
cout << c[i];
}
cout << endl; // 换行
}
return 0;
}
问题 E: 分析文章
题目描述
Alex接到了一个给英文文章分级的任务:统计文章的实际词汇量(即不重复的单词数量),并按出现顺序列出构成文章的所有词汇清单和出现的次数。现在请你来试试他的工作吧!
输入
多组数据输入,每组数据的第一行给出一篇文章构成的行数N(N ϵ[1,30])。随后跟随N行文章的内容,每行内容由空格分隔的若干完整单词和标点符号(除字母、空格、回车以外的其他可显示字符)构成。所有文章的每一行均不超过200个字符。假设所有文章的单词仅由连续的大小写字母构成,例如I’m 这种表达当成2个单词计算,分别为I和m。每篇文章至少包含1个单词,且不会超过5000个单词。
参考样本输入示范。
输出
对于每篇文章,完成分析文章的任务。即给出每篇文章对应的词汇数量,列出词汇清单和出现的次数。
输出格式为:第一行给出词汇量,第二行开始按出现顺序列出词汇清单和出现数量。行末回车。参考样本输出示范。
样例输入
2
I love my home town.
I think she will become better and better in the future.
1
!Let's*go!!!
样例输出
Sum of words:14
I:2
love:1
my:1
home:1
town:1
think:1
she:1
will:1
become:1
better:2
and:1
in:1
the:1
future:1
Sum of words:3
Let:1
s:1
go:1
代码
考场上来不及写了,上传个AI写的代码吧,有高手有更好的方法也可以评论区说说
#include <iostream>
#include <map>
#include <vector>
#include <sstream>
#include <cctype>
#include <string>
#include <algorithm>
using namespace std;
// 去掉标点符号并分割单词
vector<string> splitWords(const string& line) {
vector<string> words;
string word;
for (char c : line) {
if (isalpha(c)) {
word += c; // 保留字母
} else {
if (!word.empty()) {
words.push_back(word); // 将已完成的单词加入结果
word.clear();
}
}
}
if (!word.empty()) words.push_back(word); // 处理最后一个单词
return words;
}
int main() {
int N;
while (cin >> N) { // 多组数据输入
cin.ignore(); // 忽略行数后的换行符
map<string, int> wordCount; // 存储单词及其出现次数
vector<string> wordOrder; // 存储单词的出现顺序
for (int i = 0; i < N; ++i) {
string line;
getline(cin, line); // 逐行读取文章内容
vector<string> words = splitWords(line);
for (string& word : words) {
// 将单词转换为小写
transform(word.begin(), word.end(), word.begin(), ::tolower);
if (wordCount[word] == 0) {
wordOrder.push_back(word); // 如果单词首次出现,记录顺序
}
wordCount[word]++;
}
}
// 输出结果
cout << "Sum of words:" << wordOrder.size() << endl;
for (const string& word : wordOrder) {
cout << word << ":" << wordCount[word] << endl;
}
}
return 0;
}