Tongji University Summer 2023 Transfer Exam (Software Engineering) Programming Test
Last updated on February 10, 2025 pm
This article is currently an experimental machine translation and may contain errors. If anything is unclear, please refer to original Chinese version. I am continuously working to improve the translation.
As the title suggests
There are 3 problems in total, each worth 100 points, with an exam duration of 3 hours.
Problem 1
Problem Description
Given a string $s$ and an integer $k$, starting from the beginning of the string, for every $2k$ characters counted, reverse the first $2k$ characters among those $k$ characters.
If fewer than $k$ characters remain, reverse all the remaining characters.
If the remaining characters are fewer than $2k$ but greater than or equal to $k$, reverse the first $k$ characters and leave the rest unchanged.
Input Format
The input consists of $1$ line, containing a string of pure letters and a non-negative integer, separated by a space.
Output Format
The output is $1$ line; you only need to output the reversed string.
Sample #1
Sample Input
abcdefg 2Sample Output
bacdfegSample #2
Sample Input
abcd 2Sample Output
bacdSolution
This problem is equivalent to dividing the string s into $k$ segments, where the last part with fewer than $k$ characters also counts as one segment. Then, using each segment as an index (starting from $0$), reverse the string of odd-numbered segments (i.e., (i+1)%!=0 or (i+1)&1). Note that the last part with fewer than $k$ characters can be handled using a min function.
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
int k;
cin>>s>>k;
int len=(int)s.size();
int num=len/k;
for(int i=0;i<num;i++){
if((i+1)&1) reverse(s.begin()+i*k,min(s.begin()+i*k+k,s.end()));
}
cout<<s;
return 0;
}Problem 2
Problem Description
Brother Mao bought some candies and plans to distribute them to n = num_people children standing in a line.
Give the first child $1$ candy, the second child $2$ candies, and so on, until the last child receives $n$ candies.
Then, we return to the start of the line and give the first child $n + 1$ candies, the second child $n + 2$ candies, and so on, until the last child receives $2 * n$ candies.
Repeat the above process (each time giving out one more candy than the previous time, and starting again from the beginning of the line after reaching the end) until we have distributed all the candies. Note that even if the remaining candies in our hand are not enough (not more than the previous amount given out), all of them will still be given to the current child.
Return an array of length num_people whose elements sum to candies (we denote it by ans) to represent the final distribution of candies (i.e., ans[i] represents the number of candies the $i$-th child receives).
Input Format
The input consists of $1$ line, containing two non-negative integers: the number of candies (candies) and the number of children (num_people), separated by a space.
Output Format
The output is $1$ line, containing num_people integers separated by spaces. Each integer represents the number of candies a child receives.
Sample #1
Sample Input
7 4Sample Output
1 2 3 1Sample Explanation
The first time, ans[0] += 1, and the array becomes [1,0,0,0].
The second time, ans[1] += 2, and the array becomes [1,2,0,0].
The third time, ans[2] += 3, and the array becomes [1,2,3,0].
The fourth time, ans[3] += 1 (because only 1 candy remains at this point), and the final array becomes [1,2,3,1].
Sample #2
Sample Input
10 3Sample Output
5 2 3Sample Explanation
The first time, ans[0] += 1, and the array becomes [1,0,0].
The second time, ans[1] += 2, and the array becomes [1,2,0].
The third time, ans[2] += 3, and the array becomes [1,2,3].
The fourth time, ans[0] += 4, and the final array becomes [5,2,3].
Constraints
$1$ ≤ candies ≤ $10^9$
$1$ ≤ num_people ≤ $1000$
Solution
Simply simulate according to the problem statement.
代码
#include<bits/stdc++.h>
using namespace std;
int main(){
int candies,num_people;
cin>>candies>>num_people;
vector<int> ans(num_people,0);
int i=0;
int cnt=1;
int sum=0;
while(sum+cnt<=candies){
ans[i%num_people]+=cnt;
sum+=cnt;
cnt++;
i++;
}
ans[i%num_people]+=candies-sum;
for(int x:ans) cout<<x<<" ";
return 0;
}Problem 3
Problem Description
You are given a 2D grid consisting of '1's (land) and '0's (water). Please count the number of islands in the grid.
An island is always surrounded by water, and each island can only be formed by connecting adjacent land horizontally and/or vertically.
In addition, you may assume that all four edges of the grid are surrounded by water.
Input Format
The input consists of $n+1$ lines.
The first line contains two integers $n$ and $m$ separated by a space.
Lines $1$ - $n+1$ form an $n × m$ matrix, where each line contains $m$ non-negative integers separated by spaces.
Output Format
The output is a single integer, i.e., the number of islands.
Sample #1
Sample Input
4 5
0 0 1 1 0
1 1 0 1 0
1 1 0 0 0
0 0 0 0 0Sample Output
2Sample #2
Sample Input
4 5
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1Sample Output
2Sample Explanation
There are no '0's (water) and everything is '1' (land), yet it is still counted as one island.
Constraints
$1$ ≤ $m,n$ ≤ $200$
Solution
This is a standard DFS template problem.
Using the idea of BFS: enumerate the element at each position; if it is 0, skip it; if it is 1, use BFS to check the 4 elements adjacent to that position (provided they are not out of bounds), and determine whether they are 1. If a neighbor is 1, continue checking until the entire block of 1s has been visited. To avoid backtracking, you can define a bool array inq (short for "in queue") to record whether each position has already been enqueued during BFS.
For the current position (x,y), you can set up two increment arrays to access the 4 adjacent positions.
int X[]={0,0,1,-1};
int Y[]={1,-1,0,0};Corresponding to (x,y+1) (x,y-1) (x+1,y) (x-1,y) respectively.
This way, you can use a for loop to enumerate the 4 directions to determine the 4 positions adjacent to the current coordinates (nowX,nowY).
for(int i=0;i<4;i++){
newX=nowX+X[i];
newY=nowY+Y[i];
}代码
//BFS 做法
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn=105;
struct node{
int x,y; //存放位置
}Node;
int n,m; //矩阵大小
int matrix[maxn][maxn]; //01 矩阵
bool inq[maxn][maxn]={false}; //记录是否入过队
int X[4]={0,0,1,-1},Y[4]={1,-1,0,0}; //坐标增量数组
bool judge(int x,int y){
if(x>m||x<1||y>n||y<1||inq[x][y]||matrix[x][y]==0) return false;
//只要出界||入过队||在该点不为 1,就不访问
return true;
}
//访问位置 (x,y) 所在的块,设置一个块内的 1 的 inq 为 true
void bfs(int x,int y){
queue<node> q;
Node.x=x;Node.y=y;
q.push(Node);
while(!q.empty()){
node top=q.front(); //取队首元素 top
q.pop();
for(int i=0;i<4;i++){ //得到相邻位置
int newX=top.x+X[i],newY=top.y+Y[i];
if(judge(newX,newY)){ //如果这个新位置是符合要求的,需要访问
Node.x=newX;Node.y=newY;
q.push(Node); //入队
inq[newX][newY]=true; //已经入过队
}
}
}
}
int main(){
cin>>m>>n;
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
cin>>matrix[i][j];
}
}
int ans=0; //记录答案(块数
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
if(matrix[i][j]==1&&!inq[i][j]){ //如果这个位置是 1 而且没有被访问过
ans++; //块数++
bfs(i,j);
}
}
}
cout<<ans;
}It can also be implemented using DFS.
代码
#include<iostream>
#include<algorithm>
using namespace std;
/*dfs 的思路:
从输入的点开始搜索,如果碰到 0 了就 return
否则走岔路口:往四个方向走*/
const int maxn=105;
int m,n,matrix[maxn][maxn];
int X[4]={0,0,1,-1},Y[4]={1,-1,0,0}; //坐标增量数组
bool visited[maxn][maxn];//记录是否查询过
bool judge(int x,int y){
if(x<1||x>m||y<1||y>n||visited[x][y]||!matrix[x][y]) return false;
//只要出界||入过队||在该点不为 1,就不访问
return true;
}
//搜索一个块内,并标记为 true
void dfs(int x,int y){
if(!judge(x,y)) return;
visited[x][y]=true;
for(int i=0;i<4;i++){
int newX=x+X[i],newY=y+Y[i];
dfs(newX,newY);
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
cin>>matrix[i][j];
}
}
int ans=0; //记录答案(块数
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
if(matrix[i][j]&&!visited[i][j]){ //如果这个位置是 1 而且没有被访问过
dfs(i,j);
ans++;
}
}
}
cout<<ans;
}