8月25日 笔试
code1
#include<iostream>
#include<queue>
#include<set>
#include<algorithm>
using namespace std;
int main()
{
int n;
cin >> n;
set<int> s;//遍历过
queue<int> q;
int** map = new int*[n];
for (int i = 0; i < n; i++)
{
map[i] = new int[n];
for (int j = 0; j < n; j++)
{
cin >> map[i][j];
}
}
int count = 0;
for (int i = 0; i < n; i++)
{
if (s.find(i) != s.end())//找到了
{
continue;
}
else
{
s.insert(i);
count++;
q.push(i);
while (!q.empty())
{
int temp = q.front();
q.pop();
for (int j = 0; j < n; j++)
{
if (map[temp][j] >=3 && s.find(j) == s.end())
{
q.push(j);
}
}
s.insert(temp);
}
}
}
cout << count;
return 0;
}
code2
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int m;
cin >> m;
int **bottle;
bottle = new int*[m];
for (int i = 0; i < m; i++)
{
bottle[i] = new int[m];
for (int j = 0; j < m; j++)
{
cin >> bottle[i][j];
}
}
int flag = 1;
int *flag1 = new int[m];
memset(flag1, 0, sizeof(int)*m);
for (int i = 0; i < m - 1; i++)
{
for (int j = i + 1; j < m; j++)
{
if (bottle[i][j] >= 3)
{
if (!flag1[i] || !flag1[j])
{
if (!flag1[i])
flag = flag1[i];
else
flag = flag1[j];
}
flag1[i] = flag;
flag1[j] = flag;
}
}
flag++;
}
cout << flag;
}
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n;
cin >> n;
if (n == 2)
{
cout << 1;
return 0;
}
long long* num = new long long [1001] ();
num[0] = 1;
for (int i = 2; i <= n; i = i + 2)
{
for (int j = 2; j <= i; j = j + 2)
{
long long temp = (num[j - 2] * num[i - j]) % 1000000007;
num[i] = (num[i] + temp) % 1000000007;
}
}
cout << num[n];
return 0;
}
#include<iostream>
#include<queue>
#include<set>
#include<algorithm>
using namespace std;
int main()
{
int d;
cin >> d;
int m[4][4];
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
cin >> m[i][j];
}
}
if (d == 1)
{
for (int j = 0; j < 4; j++)
{
for (int i = 0; i < 3; i++)//下落
{
if (m[i][j] == 0)
{
for (int k = i + 1; k < 4; k++)
{
if (m[k][j] != 0)
{
swap(m[k][j], m[i][j]);
break;
}
}
}
}
for (int i = 0; i < 3; i++)//合并
{
if (m[i][j] == m[i + 1][j])
{
m[i][j] *= 2;
m[i + 1][j] = 0;
}
}
for (int i = 0; i < 3; i++)//下落
{
if (m[i][j] == 0)
{
for (int k = i + 1; k < 4; k++)
{
if (m[k][j] != 0)
{
swap(m[k][j], m[i][j]);
break;
}
}
}
}
}
}
if (d == 2)
{
for (int j = 0; j < 4; j++)
{
for (int i = 3; i > 0; i--)//下落
{
if (m[i][j] == 0)
{
for (int k = i - 1; k >= 0; k--)
{
if (m[k][j] != 0)
{
swap(m[k][j], m[i][j]);
break;
}
}
}
}
for (int i = 3; i >0; i--)//合并
{
if (m[i][j] == m[i - 1][j])
{
m[i][j] *= 2;
m[i - 1][j] = 0;
}
}
for (int i = 3; i >0 ; i--)//下落
{
if (m[i][j] == 0)
{
for (int k = i - 1; k >=0; k--)
{
if (m[k][j] != 0)
{
swap(m[k][j], m[i][j]);
break;
}
}
}
}
}
}
if (d == 3)
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 3; j++)//下落
{
if (m[i][j] == 0)
{
for (int k = j + 1; k < 4; k++)
{
if (m[i][k] != 0)
{
swap(m[i][k], m[i][j]);
break;
}
}
}
}
for (int j = 0; j < 3; j++)//合并
{
if (m[i][j] == m[i][j + 1])
{
m[i][j] *= 2;
m[i][j + 1] = 0;
}
}
for (int j = 0; j < 3; j++)//下落
{
if (m[i][j] == 0)
{
for (int k = j + 1; k < 4; k++)
{
if (m[i][k] != 0)
{
swap(m[i][k], m[i][j]);
break;
}
}
}
}
}
}
if (d == 4)
{
for (int i = 0; i < 4; i++)
{
for (int j = 3; j > 0; j--)//下落
{
if (m[i][j] == 0)
{
for (int k = j - 1; k >= 0; k--)
{
if (m[i][k] != 0)
{
swap(m[i][k], m[i][j]);
break;
}
}
}
}
for (int j = 3; j > 0; j--)//合并
{
if (m[i][j] == m[i][j - 1])
{
m[i][j] *= 2;
m[i][j - 1] = 0;
}
}
for (int j = 3; j > 0; j--)//下落
{
if (m[i][j] == 0)
{
for (int k = j - 1; k >= 0; k--)
{
if (m[i][k] != 0)
{
swap(m[i][k], m[i][j]);
break;
}
}
}
}
}
}
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
cout << m[i][j] << " ";
}
cout << endl;
}
return 0;
}
面试题1:
#include<iostream>
using namespace std;
class Solution {
public:
int findpos(int& a) {
int count = 1;
while (!(a&1)) {
count++;
a = a >> 1;
}
return count;
}
};
int main() {
int a = 16;
int pos = Solution().findpos(a);
cout << pos << endl;
system("pause");
return 0;
}
面试题2:
code1
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void traverse(vector<vector<int>> &A, int i, int j)
{
if(i<0||i>=A.size()||j<0||j>=A[0].size()||A[i][j]==0)
return;
A[i][j] = 0;
traverse(A,i-1,j);
traverse(A,i+1,j);
traverse(A,i,j-1);
traverse(A,i,j+1);
return;
}
int main()
{
int count = 0;
vector<vector<int>> A(5,vector<int>(8,0));
for(int i=0;i<5;i++)
{
for(int j=0;j<8;j++)
{
cin>>A[i][j];
}
}
for(int i=0;i<A.size();i++)
{
for(int j=0;j<A[0].size();j++)
{
if(A[i][j]==1)
{
traverse(A,i,j);
count+=1;
}
}
}
cout<<count<<endl;
return 0;
}
题目1(贪心算法):
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
vector<int> A, B;
int n;
int i, j;
int sum;
int tworst, kworst, tbest, kbest;
while (cin >> n && n)
{
A.resize(n);
B.resize(n);
for (i = 0; i < n; i++)
{
cin >> A[i];
}
for (i = 0; i < n; i++)
{
cin >> B[i];
}
sort(A.begin(), A.end());
sort(B.begin(), B.end());
//for(i=0;i<n;i++)
//{
// cout<<tian[i]<<" "<<king[i]<<endl;
//}
tworst = 0; kworst = 0;
tbest = n - 1; kbest = n - 1;
sum = 0;
while (tworst <= tbest)
{
if (A[tbest] > B[kbest])
{
sum += 1;
tbest--;
kbest--;
}
else if (A[tbest] < B[kbest])
{
sum -= 1;
kbest--;
tworst++;
}
else if (A[tbest] == B[kbest] && A[tworst] > B[kworst])
{
sum += 1;
tworst++;
kworst++;
}
else if (A[tbest] == B[kbest] && A[tworst] <= B[kworst])
{
if (A[tworst] < B[kbest])
sum -= 1;
tworst++;
kbest--;
}
}
cout << sum << endl;
A.clear();
B.clear();
}
return 0;
}
题目2(动态规划):
#include <iostream>
#include <vector>
using namespace std ;
int calsum(const vector<int>& nums, int i, int j)
{
int sum =0;
for(int start = i; start <=j; start++ )
{
sum += nums[start];
}
return sum ;
}
int findMaxScore(vector<int>& nums)
{
vector<vector<int>> dp(nums.size(),vector<int>(nums.size(), -1));
dp[0][0] = nums[0];
dp[0][1] = nums[0]+nums[1];
int maxRes = 0;
for(int i=1; i<dp.size();i++)
{
for(int j=i;j<dp[0].size();j++)
{
if( j-i<=1)
{
int max = -1;
for(int length =1; length<=4; length ++ )
{
if(length <= 2)
{
for(int changdu = 1; changdu<=2; changdu++ )
{
if(i-length-changdu >= 0 &&
dp[i-length-changdu][i-length-1] != -1)
{
if(dp[i-length-changdu][i-length-1]>max)
max = dp[i-length-changdu][i-length-1];
}
}
}
else
{
int changdu = 2 ;
if(i-length-changdu >= 0 &&
dp[i-length-changdu][i-length-1] != -1)
{
if(dp[i-length-changdu][i-length-1]>max)
max = dp[i-length-changdu][i-length-1];
}
}
}
if(max != -1)
dp[i][j] = max+calsum(nums, i, j);
}
if(dp[i][j]>maxRes)
maxRes = dp[i][j];
}
}
return maxRes;
}
int main()
{
//5
//2 7 9 4 3 : 9
vector<int> nums;
nums.push_back(9);
nums.push_back(7);nums.push_back(9);
nums.push_back(4);nums.push_back(3);
cout << findMaxScore(nums)<<endl;
//cout << calsum( nums, 2, 3);
return 0;
}
题目3:
code1
#include<iostream>
#include<vector>
using namespace std;
int isswap(vector<int> arr, int from, int to)
{
for (int i = from; i < to; i++)
{
if (arr[i] == arr[to])
return 0;
}
return 1;
}
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
vector<int> permutations(vector<int> arr, int k, int n,vector<int> &post)
{
if (k == n)
{
for (int i = 0; i < n; i++)
{
post.push_back(arr[i]);
//cout << arr[i] << " ";
//++count;
}
//cout << endl;
}
else
{
for (int i = k; i < n; i++)
{
if (isswap(arr, k, i))
{
swap(&arr[k], &arr[i]);
permutations(arr, k + 1, n,post);
swap(&arr[i], &arr[k]);
}
}
}
return post;
}
int main()
{
int n,m;
int count=0;
cin >> n >> m;
vector<int> A(n);
vector<int> post;
for (int i = 0; i < n; ++i) cin >> A[i]; cout << endl;
post = permutations(A, 0, n,post);
count = post.size() / n;
//cout << count << endl;
for (int i = 1; i < post.size(); ++i)
{
if (abs(post[i - 1] - post[i]) > m & (i) % n != 0)
--count;
if (i > n - 2 && (i+1) % n == 0 && abs(post[i - n + 1] - post[i]) > m)
--count;
//cout << post[i]<<" ";
}
cout << count<<endl;
system("pause");
return 0;
}
code2
//#include<bits/stdc++.h>
#include<iostream>
#include<map>
#include<vector>
#include <algorithm>
#define pb push_back
using namespace std;
const int N = 2e5 + 11;
int n, m, a[N], ans, b[N];
map<vector<int>, int>vis;
int main() {
ios::sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= n; i++)cin >> a[i], b[i] = i;
do {
int sta = 0;
for (int i = 2; i <= n; i++) {
if (abs(a[b[i]] - a[b[i - 1]]) > m) {
sta = 1;
break;
}
}
if (abs(a[b[1]] - a[b[n]]) > m)sta = 1;
if (!sta) {
vector<int>gao;
int pos;
for (int i = 1; i <= n; i++) {
if (b[i] == 1) {
pos = i;
break;
}
}
int cnt = 0;
while (cnt < n) {
gao.pb(b[pos]);
pos++;
cnt++;
if (pos > n)pos = 1;
}
if (!vis[gao])ans++, vis[gao] = 1;
}
} while (next_permutation(b + 1, b + 1 + n));
cout << ans << endl;
system("pause");
return 0;
}
题目4:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int n, m;
cin >> n >> m;
vector<int> dp(n + 1);
vector<int>a(m + 1, 0);
vector<int>b(m + 1, 0);
for (int i = 0; i < m; i++)
cin >> a[i] >> b[i];
for (int j = 0; j <= n; j++)
dp[j] = (j >= a[0] ? b[0] : 0);
for (int i = 1; i < m; i++)
{
for (int j = n; j >= a[i]; j--)
{
dp[j] = max(dp[j], dp[j - a[i]] + b[i]);
}
}
cout << dp[n] << endl;
system("pause");
return 0;
}