1.挖地雷
哈我来搞地雷呵呵,可以拿钱,也可能花不了.....(讲个笑话)
回归正题,见题!
在一个地图上有N个地窖(N≤200),每个地窖中埋有一定数量的地雷。同时,给出地窖之间的连接路径,并规定路径都是单向的。某人可以从任一处开始挖地雷,然后沿着指出的连接往下挖(仅能选择一条路径),当无连接时挖地雷工作结束。设计一个挖地雷的方案,使他能挖到最多的地雷。
输入
N {地窖的个数}
W1,W2,⋯,WN{每个地窖中的地雷数}
X1,Y1{表示从 X1可到 Y1}
X2,Y2
……
0,0{表示输入结束}输出
MAX {最多挖出的地雷数}
样例输入 Copy
6 5 10 20 5 4 5 1 2 1 4 2 4 3 4 4 5 4 6 5 6 0 0样例输出 Copy
34
AC代码如下:
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,p[205],dp[205];
bool visit[205][205];
cin >> n;
for(int i = 1; i <= n ; i ++)
cin >> p[i],dp[i] = p[i];
int x,y;
while(cin >> x >> y){
if(x == 0 && y == 0)
break;
visit[x][y] = true;
}
for(int i = 1; i <= n ; i ++)
for(int j = 1; j <= n ; j ++){
if(visit[i][j])
dp[j] = max(dp[j],dp[i] + p[j]);
}
int ans = -1;
for(int i = 1; i <= n ; i ++)
ans = (ans > dp[i]) ? ans : dp[i];
cout << ans;
return 0;
}
这道题当时写的时候,主要是功克每个路径之间的联系,于是运用了一个二维布尔数组来解决关联问题,我觉得这个可以多多运用到其他地方。
while(cin >> x >> y){
if(x == 0 && y == 0)
break;
visit[x][y] = true;
}
2.二分查找
给定大小为N(0<N<1000),从小到大排列的整数数组A[],以及待查找的整数X,目的是找到X在数组A[]中第一次出现的位置并输出。如没有找到则输出-1;
输入
第一行 数组大小 N
第二行 数组A[]
第三行 带查找的整数X输出
如找到X,则输出第一次出现的位置。如未找到,则输出-1。
样例输入 Copy
5 1 2 4 4 5 4样例输出 Copy
3
这道题写的时候问题出在了除重上,于是没有解决找第一个X的位置问题,然后用了除重操作之后就迎刃而解了
问题所在点(没有这步操作):
int cnt = unique(a+1,a+1+n) - a -1;
AC代码如下:
#include<bits/stdc++.h>
using namespace std;
int BinSearch(int R[],int n,int k)
{
int low=1,high=n,mid;
while (low<=high)
{ mid=(low+high)/2;
if (R[mid]==k)
return mid;
if (k<R[mid])
high=mid - 1;
else
low=mid + 1;
}
return -1;
}
int main(){
int n,m;
int a[1005];
cin >> n;
for(int i = 1; i <= n ;i ++)
cin >> a[i];
cin >> m;
sort(a+1,a+1+n);
int cnt = unique(a+1,a+1+n) - a -1;
cout << BinSearch(a,cnt,m);
return 0;
}
3.模拟栈
这个没什么好说的,记录一下嘻嘻!
#include<iostream>
using namespace std;
const int N = 1e5 + 10;
int st[N];
int top = -1;
int n;
int main()
{
cin >> n;
while(n)
{
string ch;
cin >> ch;
if(ch == "push")
{
int x;
cin >> x;
st[++top] = x;
}
if(ch == "pop")
{
top --;
}
if(ch == "query")
{
cout << st[top] << endl;
}
if(ch == "empty")
{
cout << (top == -1 ? "YES" : "NO") << endl;
}
n --;
}
return 0;
}
哦这个是没有用结构体的栈应用,听说会更方便省时,真的吧哈哈。(有问题的话,告诉我一声呗,我还啥也不知道)
4.数的划分
题目找不到了呜呜呜
那就见代码吧——
#include<iostream>
using namespace std;
const int N = 205;
int f[N][N];
int main()
{
int n,k;
cin >> n >> k;
f[0][0] = 1;
for(int i = 1; i <= n ; i ++)
for(int j = 1; j <= i && j <= k; j ++){
f[i][j] = f[i - j][j] + f[i - 1][j - 1];
}
cout << f[n][k] << endl;
return 0;
}
当时问题出在哪里呢,主要是这个状态转移方程没想到,现在知道理解了,耶耶耶。
继续加油,继续努力!