第一周训练 | STL和基本数据结构
A - 圆桌问题: HDU - 4841
#include<iostream>
#include<vector>
#include<stdio.h>
#include<string>
using namespace std;
int main()
{
vector<int> table;
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
table.clear();
for(int i=0;i<2*n;++i)
{
table.push_back(i);
}
int pos = 0;
for(int i=0;i<n;++i)
{
pos = (pos+m-1)%table.size();
table.erase(table.begin()+pos);
}
int j=0;
for(int i=0;i<2*n;++i)
{
if(!(i%50)&&i)cout<<endl;
if(j<table.size()&&i==table[j])
{
++j;
cout<<"G";
}
else
{
cout<<"B";
}
}
cout<<endl<<endl;
}
return 0;
}
C - 简单计算器 HDU - 1237
好自闭啊,找了半天还是WA了,完全不知道WA在哪里了Orz,先存一个档:
#include<iostream>
#include<stack>
#include<stdio.h>
#include<string>
#include<map>
#define CHECK_NUM(ch) ('0'<=ch&&ch<='9')
#define CHECK_OP(ch) ('+'==ch ||'-'==ch||'*'==ch||'/'==ch ||'\0'==ch)
using namespace std;
map<char,int>dir;
stack<double> num;
stack<char> op;
stack<double> newnum;
stack<char> newop;
double OPT(double one,double two,char temp)
{
// printf("%f",two);
double three=0;
switch(temp)
{
case '+':three = two + one;break;
case '-':three = two - one;break;
case '*':three = two * one;break;
case '/':three = two / one;break;
}
// printf("%c %lf = %lf\n",temp,one,three);
return three;
}
void initi()
{
while(!num.empty())num.pop();
while(!op.empty())op.pop();
while(!newop.empty())newop.pop();
while(!newnum.empty())newnum.pop();
}
void STACK(char ch)
{
// cout<<ch<<endl;
if(!op.empty()&&num.size()>1)
{
char temp=op.top();
// cout<<"当前栈顶:"<<temp<<",当前扫描:"<<ch<<endl;
if(dir[temp]>=dir[ch])
{
op.pop();
double one=num.top();num.pop();
double two=num.top();num.pop();
num.push(OPT(one,two,temp));
// cout<<"入栈num:"<<num.top()<<endl;
}
}
op.push(ch);
}
int main()
{
char ch='\0';
double result=0;
dir['+']=1; dir['-']=1;
dir['*']=2; dir['/']=2;
dir['\0']=0;
while(1)
{
string a="";
getline(cin,a);
initi();
if(a=="0")break;
for(int i=0;i<=a.length();++i)
{
double nums=0;
if(CHECK_NUM(a[i]))
{
while(CHECK_NUM(a[i]))
{
nums+=a[i]-'0';
nums*=10;
i++;
}
nums/=10;
// cout<<"nums"<<nums<<endl;
num.push(nums);
// printf("%.2f\n",num.top());
}
if(CHECK_OP(a[i]))
{
STACK(a[i]); //如果是操作数就入操作数栈
}
}
while(!op.empty())
{
newop.push(op.top());
op.pop();
}
while(!num.empty())
{
newnum.push(num.top());
num.pop();
}
while(!newop.empty()&&newnum.size()>1)
{
char temp=newop.top();newop.pop();
// cout<<"当前弹出:"<<temp<<endl;
double one=newnum.top();newnum.pop();
double two=newnum.top();newnum.pop();
newnum.push(OPT(two,one,temp));
// cout<<"入栈num:"<<newnum.top()<<endl;
}
// printf(">>%.2lf\n",newnum.top());
// cout<<newnum.size();
printf("%.2lf\n",newnum.top());
}
return 0;
}
贴一个师傅的AC:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<iomanip>
using namespace std;
typedef long long LL;
#define rep(i,a,b) for(int i=a;i<b;++i)
const double eps=1e-9;
const int N=1010;
char str[N];
double stk1[N];//操作数栈
int p1;//操作数栈的栈顶指针
char stk2[N];//运算符栈
int p2;//运算符栈的栈顶指针
double op(double a,double b,char ch){
if(ch=='+')return a+b;
else if(ch=='-')return a-b;
else if(ch=='*')return a*b;
else return a/b;
}
//优先级比较函数加括号的话,只需要修改这里就好了
//ch1:扫描到的,ch2:栈顶的
int comp(char ch1,char ch2)
{
int a=0,b=0;
if(ch1=='*'||ch1=='/')a=1;
if(ch2=='*'||ch2=='/')b=1;
return a<=b;
}
int main()
{
while(gets(str)) //读入一行
{
p1=0,p2=0;
int len=strlen(str);
if(len==1&&str[0]=='0')break;
int i=0;
double sum=0;
while(i<len&&str[i]>='0'&&str[i]<='9')
{
sum=sum*10+str[i]-'0';
i++;
}
stk1[p1++]=sum;//得到操作数
while(i<len)
{
i++;//跳过空格
char ch=str[i];//读取操作数
i+=2;//跳过空格
double sum=0;//得到第二个操作数
while(i<len&&str[i]>='0'&&str[i]<='9')
{
sum=sum*10+str[i]-'0';
i++;
}
if(p2==0)//如果操作符栈为空,操作符就进栈
{
stk2[p2++]=ch;
}
else
{
//当前读到的操作数的优先级小于等于栈顶的优先级
//并且运算符栈不为空
while(p2>0&&comp(ch,stk2[p2-1]))
{
double ans=op(stk1[p1-2],stk1[p1-1],stk2[p2-1]);//计算新的值
p2-=1;
p1-=2;
stk1[p1++]=ans;//新的值入操作数栈
}
stk2[p2++]=ch;
}
stk1[p1++]=sum;
}
while(p2!=0)//当操作符栈不为空
{
double ans=op(stk1[p1-2],stk1[p1-1],stk2[p2-1]);
p1-=2;
stk1[p1++]=ans;
p2--;
}
printf("%.2f\n",stk1[0]);
}
return 0;
}
D - ACboy needs your help again! HDU - 1702
#include<iostream>
#include<string>
#include<queue>
#include<stack>
using namespace std;
int main()
{
int t,n,temp;
cin>>t;
while(t--)
{
string str,str1;
queue<int>Q;
stack<int>S;
cin>>n>>str;
for(int i=0;i<n;++i)
{
if(str=="FIFO")
{
cin>>str1;
if(str1=="IN")
{
cin>>temp;
Q.push(temp);
}
if(str1=="OUT")
{
if(Q.empty()) cout<<"None"<<endl;
else
{
cout<<Q.front()<<endl;
Q.pop();
}
}
}
else
{
cin>>str1;
if(str1=="IN")
{
cin>>temp;S.push(temp);
}
if(str1=="OUT")
{
if(S.empty()) cout<<"None"<<endl;
else
{
cout<<S.top()<<endl;
S.pop();
}
}
}
}
}
return 0;
}
E - 看病要排队 HDU - 1873
#include<iostream>
#include<string>
#include<queue>
#include<stack>
using namespace std;
int id;
typedef struct p
{
int id;
int pr;
}people;
bool operator < (const people &a,const people &b)
{
//当a,b的优先级一样的时候,比较a,b的编号
if(a.pr== b.pr) return (a.id > b.id);//当优先级一样的时候,先来的人先看病
return (a.pr < b.pr);
}
priority_queue<people> A;
priority_queue<people> B;
priority_queue<people> C;
int intital()
{
while(!A.empty()) A.pop();
while(!B.empty()) B.pop();
while(!C.empty()) C.pop();
id=0;
return 0;
}
int main()
{
int num=1;
while(scanf("%d",&num)!=EOF)
{
intital();
// cout<<"输入的n:"<<num<<endl;
for(int i=0;i<num;++i)
{
string str;
people person;
int doctor;
cin>>str>>doctor;
// cout<<"str,doctor:"<<str<<","<<doctor<<endl;
if(str=="IN")
{
++id;//人数自增
person.id=id;
cin>>person.pr;
// cout<<",person.pr"<<person.pr<<endl;
switch(doctor)
{
case 1:
{
A.push(person);
break;
}
case 2:
{
B.push(person);
break;
}
case 3:
{
C.push(person);
break;
}
}
}
else
{
switch(doctor)
{
case 1:
{
if(A.empty())
{
cout<<"EMPTY"<<endl;
}
else
{
cout<<A.top().id<<endl;
A.pop();
}
break;
}
case 2:
{
if(B.empty())
{
cout<<"EMPTY"<<endl;
}
else
{
cout<<B.top().id<<endl;
B.pop();
}
break;
}
case 3:
{
if(C.empty())
{
cout<<"EMPTY"<<endl;
}
else
{
cout<<C.top().id<<endl;
C.pop();
}
break;
}
}
}
}
}
return 0;
}
士兵队列训练问题 HDU - 1276
#include<iostream> #include<map> #include<string> #include<list> using namespace std; int main() { int t,n; cin>>t; while(t--) { cin>>n; int k=2; list<int>mylist; list<int>::iterator it; for(int i=1;i<=n;++i)mylist.push_back(i); while(mylist.size()>3) { int num=1; for(it=mylist.begin();it!=mylist.end();) { if(num++%k==0) { it = mylist.erase(it); } else { ++it; } } k==2?k=3:k=2; } for(it=mylist.begin();it!=mylist.end();++it) { if(it!=mylist.begin())cout<<" "; cout<<*it; } cout<<endl; } return 0; }
G - 产生冠军 HDU - 2094
#include<iostream>
#include<set>
#include<string>
using namespace std;
int main()
{
set<string>A,B;
string s1,s2;
int n;
while(cin>>n&&n)
{
for(int i=0;i<n;++i)
{
cin>>s1>>s2;
A.insert(s1);A.insert(s2);
B.insert(s2);
}
if(A.size()-B.size()==1)
{
cout<<"Yes"<<endl;
}
else
{
cout<<"No"<<endl;
}
A.clear();
B.clear();
}
return 0;
}
H - Shopping HDU - 2648 :map的遍历
#include<iostream> #include<map> #include<string> using namespace std; int main() { int n,m,p; map<string,int>shop; while(cin>>n) { string s; for(int i=1;i<=n;++i)cin>>s; cin>>m; while(m--) { for(int i=1;i<=n;++i) { cin>>p>>s; shop[s]+=p; } int rank =1; map<string,int>::iterator it; for(it=shop.begin();it!=shop.end();it++) { if(it->second>shop["memory"]) { rank++; } } cout<<rank<<endl; } shop.clear(); } return 0; }
I - Ignatius and the Princess II HDU - 1027:next_permutation
#include<iostream> #include<map> #include<string> #include<list> #include<algorithm> using namespace std; int a[1001]; int main() { int m,n; while(cin>>n>>m) { for(int i=1;i<=n;++i) a[i]=i; int b=1; do { if(b==m)break; b++; }while(next_permutation(a+1,a+n+1)); for(int i=1;i<n;++i) { cout<<a[i]<<" "; } cout<<a[n]<<endl; } return 0; }
J - 排列2 HDU - 1716
#include<iostream> #include<map> #include<string> #include<list> #include<vector> #include<algorithm> #include<set> #include<string.h> using namespace std; int main() { while(1) { int num[4]={}; vector<int> m[100]; int index[10]; memset(index,0,sizeof(int)*10); int count=0; map<int,bool> visit;//标记是否重复出现 set<int>s; s.clear(); for(int i=0;i<4;++i) { cin>>num[i]; if(num[i]==0)++count; //计算0的个数 index[num[i]]=1; } if(count==4)break;//如果有4个0就结束掉 sort(num,num+4); do { int str=0; if(num[0]==0)continue; for(int i=0;i<4;++i) { str+=num[i]; str*=10; } str/=10; // cout<<">>"<<str<<","<<visit[str]<<" "; if(!visit[str]) { m[num[0]].push_back(str); // cout<<"输入到导出表的:"<<"m["<<num[0]<<"]="<<str<<endl; visit[str]=1; } }while(next_permutation(num,num+4)); int i=1,k=0; for(int j=0;j<10;++j)if(index[j])++k; for(;i<=9;++i) { if(index[i]) { // cout<<"第一组"<<i<<endl; index[i]=0; cout<<m[i][0]; for(int j=1;j<m[i].size();++j) { cout<<" "<<m[i][j]; } k--; cout<<endl; } if(k==1)break; } for(;i<=9;++i) { if(index[i]) { index[i]=0; cout<<m[i][0]; for(int j=1;j<m[i].size();++j) { cout<<" "<<m[i][j]; } } } cout<<endl; } return 0; }
贴一份AC:https://blog.csdn.net/leo6033/article/details/79249163
#include<stdio.h> int a[4],i; void sum(int num[],int x1, int x2, int x3, int x4) { num[i++] = a[x1] * 1000 + a[x2] * 100 + a[x3] * 10 + a[x4]; num[i++] = a[x1] * 1000 + a[x2] * 100 + a[x4] * 10 + a[x3]; num[i++] = a[x1] * 1000 + a[x3] * 100 + a[x2] * 10 + a[x4]; num[i++] = a[x1] * 1000 + a[x3] * 100 + a[x4] * 10 + a[x2]; num[i++] = a[x1] * 1000 + a[x4] * 100 + a[x2] * 10 + a[x3]; num[i++] = a[x1] * 1000 + a[x4] * 100 + a[x3] * 10 + a[x2]; } int main() { int p = 0; while (~scanf("%d %d %d %d", &a[0], &a[1], &a[2], &a[3])) { int num[24] = { 0 }; if (!a[0] && !a[1] && !a[2] && !a[3]) { break; } if (p == 0)p = 1; else printf("\n"); i = 0; sum(num, 0, 1, 2, 3); sum(num, 1, 2, 3, 0); sum(num, 2, 3, 0, 1); sum(num, 3, 0, 1, 2); int temp,j; for (i = 23; i >0; i--) { for (j = 0; j < i-1; j++) { if (num[j] > num[j + 1]) { temp = num[j]; num[j] = num[j + 1]; num[j + 1] = temp; } } } for (i = 0; i < 23; i++) { //printf("%d ",num[i]); if (num[i] != num[i + 1]&&num[i]>1000) { printf("%d", num[i]); if (num[i] / 1000 != num[i + 1] / 1000) { printf("\n"); } else { printf(" "); } } } printf("%d\n", num[i]); } return 0; }