拼多多9.1提前批笔试

题目:http://mrw.so/6tsmXx
1.打印米字矩阵 3.带负数的0 1背包(学到了,把负体积先装进去,相当于总体积先扩大,总收益起点为ans,遇到负体积的物品相当于取出,收益增加它的相反数,如收益-1 要转变为1 因为拿出-1相当于+1)
#include
#include
using namespace std;
int ju[201][201], temp;
struct pos{
int x, y;
};
bool ifLegal(int x, int y) {
return x >= 0 && x < temp && y >= 0 && y < temp;
}
int d[2][4]={ {-1,0,1,0},{0,1,0,-1}};
void da(int x, int y,int c) {
pos t;
t.x = x;
t.y = y;
queueque;
que.push(t);
while(!que.empty()) {
pos k = que.front();
que.pop();
if (!ifLegal(k.x, k.y))
continue;
if (ju[k.x][k.y] != 0)
continue;
ju[k.x][k.y] = c;
for (int i = 0; i < 4; i ++) {
pos temp;
temp.x = k.x + d[0][i];
temp.y = k.y + d[1][i];
que.push(temp);
}
};
}
int main() {
int n;
cin >> n;
temp = n;
if (temp%2 == 0) {
++ temp;
}
for (int i = 0; i < temp; i ++) {
ju[i][i] = -1;
ju[i][temp - i - 1] = -1;
ju[temp/2][i] = -1;
ju[i][temp/2] = -1;
}
da(0, temp - 2, 1);
da(0, 1, 2);
da(1,0,3);
da(temp - 2, 0, 4);
da(temp - 1, 1, 5);
da(temp - 1, temp - 2, 6);
da(temp - 2, temp - 1, 7);
da(1, temp - 1, 8);
for (int i = 0; i < temp; i ++) {
for (int j = 0; j < temp; j ++) {
if (temp != n && (i == temp/2 || j == temp / 2)){
continue;
}
if (ju[i][j] == -1)
cout << 0;
else
cout << ju[i][j];
cout << " ";
}
if (temp != n && i == temp/2)
continue;
cout << endl;
}
}

法2
#include
using namespace std;

const int N = 210;

int n;
int a[N][N];

int main() {
cin >> n;

for (int i = 0; i < n; i ++ )
    for (int j = 0; j < n; j ++ ) {
        if (i == j || i + j == n - 1 || (n & 1) && (i == n >> 1 || j == n >> 1)) continue;
        else {
            if (i < n >> 1) {
                if (j < n >> 1) {
                    if (i > j) a[i][j] = 3;
                    else a[i][j] = 2;
                } else {
                    if (i + j >= n) a[i][j] = 8;
                    else a[i][j] = 1;
                }
            } else {
                if (j < n >> 1) {
                    if (i + j >= n) a[i][j] = 5;
                    else a[i][j] = 4;
                } else {
                    if (i > j) a[i][j] = 6;
                    else a[i][j] = 7;
                }
            }
        }
    }

for (int i = 0; i < n; i ++ ) {
    for (int j = 0; j < n; j ++ ) cout << a[i][j] << ' ';
    puts("");
}

return 0;

}

作者:xnuohz
链接:https://www.acwing.com/blog/content/3740/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
3.
#include
using namespace std;
const int maxn=410;
int c[maxn],v[maxn],ans;
int dp[40010];
int main()
{
int N,M;
scanf("%d%d",&N,&M);
for(int i=1;i<=N;i++){
scanf("%d%d",&c[i],&v[i]);
if(c[i]<=0){ //负的物体取反
ans+=v[i];
M-=c[i];
c[i]=-c[i];
v[i]=-v[i];
}
}
for(int i=1;i<=N;i++) //01背包
for(int j=M;j>=c[i];j–)
dp[j]=max(dp[j],dp[j-c[i]]+v[i]);

for(int i=0;i<=M;i++) {
        dp[M]=max(dp[M],dp[i]);
}
printf("%d\n",ans+dp[M]);
return 0;

}
2.移动士兵,获得最大集团(没看懂,先放代码)
https://www.nowcoder.com/discuss/495682?type=post&order=time&pos=&page=1&channel=666&source_id=search_post
#include
#include
#include
#include
using namespace std;
bool shibing[400][400];
int shibingS[400][400];
struct pos{
int x, y;

};
int d[2][4]={ {-1,0,1,0},{0,1,0,-1}};
int num[400 * 400];
bool numcc[400*400];
int n, m;
bool ifLegal(int x, int y) {
return x >= 0 && x < n && y >= 0 && y < m;
}
int da(int x, int y,int c) {
pos t;
int ans = 0;
t.x = x;
t.y = y;
queueque;
que.push(t);
while(!que.empty()) {
pos k = que.front();
que.pop();
if (!ifLegal(k.x, k.y))
continue;
if (shibing[k.x][k.y] == 0 || shibingS[k.x][k.y] == c)
continue;
shibingS[k.x][k.y] = c;
ans++;
for (int i = 0; i < 4; i ++) {
pos temp;
temp.x = k.x + d[0][i];
temp.y = k.y + d[1][i];
que.push(temp);
}
};
//cout << ans << endl;
return ans;
}
int main() {
int cnt = 0, alll = 0;
memset(shibingS, -1, sizeof(shibingS));
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j ++) {
cin >> shibing[i][j];
}
}
int ans = 0;
for (int i = 0; i < n; i ++) {
for (int j = 0; j < m; j ++) {
if (shibingS[i][j] == -1 && shibing[i][j]) {
++ cnt;
num[cnt] = da(i, j, cnt);
ans = max(ans, num[cnt]);
alll += num[cnt];
}
}
}
queueque;
// for (int i = 0; i < n; i ++) {
// for (int j = 0; j < m; j ++) {
// cout << shibingS[i][j] << " ";
// }
// cout << endl;
// }
for (int i = 0; i < n; i ++) {
for (int j = 0; j < m; j ++) {
if (shibing[i][j])
continue;
int temp = 0;
for (int k = 0; k < 4; k ++) {
int x = i + d[0][k];
int y = j + d[1][k];
if (!ifLegal(x, y))
continue;
if (shibingS[x][y] != -1 && !numcc[shibingS[x][y]]) {
numcc[shibingS[x][y]] = 1;
temp += num[shibingS[x][y]];
que.push(shibingS[x][y]);
}
}
temp += 1;
ans = max(ans, temp);
while (!que.empty()) {
int k = que.front();
que.pop();
numcc[k] = 0;
}
}
}
cout << min(ans, alll) << endl;
}

你可能感兴趣的:(拼多多9.1提前批笔试)