*H41 称砝码
设置set数组用来去重,第一个砝码1{0,1,0+1},第二个还是1,{0,1,2}
一个一个砝码累积
temp=v[i],n个就循环n次
set s(weight);//增加一个set数组复制不修改前的set,否则陷入无限循环
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
int x;
vector<int> v;
set<int> weight;
set<int> ::iterator it;
weight.insert(0);
for(int i=0;i<n;i++){
cin>>x;
v.push_back(x);
}
for(int i=0;i<n;i++){
cin>>x;
for(int j=1;j<=x;j++){
int temp=v[i];
set<int> s(weight);//增加一个set数组复制不修改前的set,否则陷入无限循环
for(it=s.begin();it!=s.end();it++){
weight.insert(*it+temp);
}
weight.insert(temp);
}
}
cout<<weight.size()<<endl;
return 0;
}
H42 学英语
太费劲,思路不好
H43 *迷宫问题 BFS
rbegin
语法:const reverse_iterator rbegin();
解释:rbegin()返回一个逆向迭代器,指向字符串的最后一个字符。
rend
语法:const reverse_iterator rend();
解释:rend()函数返回一个逆向迭代器,指向字符串的开头(第一个字符的前一个位置)。
#include<bits/stdc++.h>
using namespace std;
int m,n;
vector<pair<int ,int>> matric;
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int arr[11][11]={0};//存储原矩阵
int tab[11][11]={0};//存储标记
bool dfs(int x,int y){
int p,q;//p行q列
if(x==m-1&&y==n-1){//到达终点
return true;
}
for(int i=0;i<4;i++){
p=x+dir[i][0];
q=y+dir[i][1];
if(p>=0&&p<m&&q>=0&&q<n){
if(arr[p][q]==0 && tab[p][q]==0){//可以走&&没走过
tab[p][q]=1;
if(dfs(p,q)){
matric.push_back(make_pair(p,q));
return true;
}
}
}
}
return false;
}
int main(){
cin>>m>>n;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
cin>>arr[i][j];
}
}
tab[0][0]=1;//起点
cout<<"(0,0)"<<endl;
if(dfs(0,0)==true){
auto it=matric.rbegin();//rbegin()返回一个逆向迭代器,指向字符串的最后一个字符。
while(it!=matric.rend()){
cout<<"("<<it->first<<","<<it->second<<")"<<endl;
it++;
}
matric.clear();
memset(tab, 0, sizeof(tab));
}
return 0;
}
*H48 单链表中删除
学习链表的使用
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,head;
cin>>n>>head;
forward_list<int> linklist;
linklist.push_front(head);
for(int i=1;i<n;i++){
int front,back;
cin>>back>>front;
auto it=find(linklist.begin(),linklist.end(),front);
linklist.insert_after(it,back);//插入节点
}
int last;
cin>>last;
linklist.remove(last);
for(auto it=linklist.begin();it!=linklist.end();it++){
cout<<*it<<" ";
}
cout<<endl;
return 0;
}
*H51 单链表
注意:
单链表的创建以及初始化
struct ListNode
{
int m_nKey;
ListNode* m_pNext;
ListNode(int x):m_nKey(x),m_pNext(NULL){}//初始化
};
调用新建结点:
ListNode *head=new ListNode(val);
#include<bits/stdc++.h>
using namespace std;
struct ListNode
{
int m_nKey;
ListNode* m_pNext;
ListNode(int x):m_nKey(x),m_pNext(NULL){}//初始化
};
//建立单链表
int main(){
int n;
while(cin>>n){
int val;
cin>>val;
ListNode *head=new ListNode(val);
ListNode *p=head;
for(int i=1;i<n;i++){
cin>>val;
ListNode *q=new ListNode(val);
p->m_pNext=q;
p=p->m_pNext;
}
int k;
cin>>k;
if(k==0) cout<<0<<endl;
else{
ListNode *q;
p=head;
q=head;
while(k!=1){
p=p->m_pNext;
--k;
}
while(p->m_pNext!=NULL){
q=q->m_pNext;
p=p->m_pNext;
}
cout<<q->m_nKey<<endl;
}
}
return 0;
}
*H44 sudoku数独问题
使用深度优先遍历数组,当某一格为0时,遍历1-9,看是否符合行,列,九宫格,符合,则将这一格设置为相应数,继续DFS下一格,否则,回溯,将这一格设为0,再次寻找合适数
#include<bits/stdc++.h>
using namespace std;
int a[9][9];
bool flag=false;
bool check(int h,int l,int n){
//检查行
for(int i=0;i<9;i++){
if(a[h][i]==n){
return false;
}
}
//column
for(int i=0;i<9;i++){
if(a[i][l]==n){
return false;
}
}
//9宫格
int q[3]={0,3,6};
int x=q[h/3];
int y=q[l/3];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(a[x+i][y+j]==n){
return false;
}
}
}
return true;
}
void DFS(int n){
if(n==81){
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
cout<<a[i][j]<<' ';
}
cout<<'\n';
}
flag=true;
return;
}
int h=n/9;
int l=n%9;
if(a[h][l]==0){
for(int i=1;i<=9;i++){//列举所有情况
if(check(h,l,i)){
a[h][l]=i;
DFS(n+1);//寻找下一个
if(flag){
return;
}
}
a[h][l]=0;
}
}
else{
DFS(n+1);
}
}
int main(){
int l=0;
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
cin>>a[i][j];
}
}
DFS(0);
return 0;
}
*H50 四则运算
设置符号栈和数字栈,遇到(入栈,判断负号优先级,遇到)出栈,计算
#include<bits/stdc++.h>
using namespace std;
void compute(stack<int> &num,stack<char> & fuhao){
int b=num.top();
num.pop();
int a=num.top();
num.pop();
char op=fuhao.top();
fuhao.pop();
switch(op){
case '+':a=a+b;break;
case '-':a=a-b;break;
case '*':a=a*b;break;
case '/':a=a/b;break;
}
num.push(a);
}
bool priority(char m,char n){
if(m=='('){
return false;
}
else if((m=='+'||m=='-')&&(n=='*'||n=='/')){
return false;
}
else return true;
}
int main(){
string str;
cin>>str;//400+3*5-(2*6)+1
stack<int> num;
stack<char> fuhao;
fuhao.push('(');
str+=')';
bool flag=false;
for(int i=0;i<str.length();i++){
if(str[i]=='('){
fuhao.push(str[i]);
}
else if(str[i]==')'){
while(fuhao.top()!='('){//注意这个while
compute(num,fuhao);
}
fuhao.pop();
}
else if(flag){
while(priority(fuhao.top(),str[i])){
compute(num,fuhao);
}
fuhao.push(str[i]);
flag=false;
}
else{
int k=0;
int fu=0;
if(str[i]=='-'){
fu=1;
i++;
}
while(isdigit(str[i])){
k=k*10+str[i]-'0';
i++;
}
if(fu==1){
k=0-k;
}
num.push(k);
i--;
flag=true;
}
}
cout<<num.top()<<endl;
return 0;
}