队列问题
1. List item
NEFU 1632
Description
假设在周末舞会上,男士们和女士们进入舞厅时,各自排成一队。跳舞开始时,依次从男队和女队的队头上各出一人配成舞伴。规定每个舞曲只能有一对跳舞者。若两队初始人数不相同,则较长的那一队中未配对者等待下一轮舞曲。现要求写一个程序,模拟上述舞伴配对问题。
Input
第 1 行两个正整数,表示男士人数 m 和女士人数 n,1≤m,n≤1000;
第 2 行一个正整数,表示舞曲的数目 k,k≤1000。
Sample Input
2 4
6
Output
共 k 行,每行两个数,之间用一个空格隔开,表示配对舞伴的序号,男士在前,女士在后。
Sample Output
1 1
2 2
1 3
2 4
1 1
2 2
#include <bits/stdc++.h>
using namespace std;
//创建两个队列
queue<int>vis1,vis2;
int main()
{
int n,m,s1,s2,k;
cin>>n>>m;
cin>>k;
//将男女依次入队
//例 男1 2
// 女1 2 3 4
for(int i=1;i<=n;i++)
vis1.push(i);
for(int i=1;i<=m;i++)
vis2.push(i);
for(int i=1;i<=k;i++)
{//使S1,S2为男女队列首元素
s1=vis1.front();
vis1.pop();//首元素出列
s2=vis2.front();
vis2.pop();//首元素出列
cout<<s1<<" "<<s2<<endl;
vis1.push(s1);//重新入队
vis2.push(s2);
}
return 0;
}
**
动态规划
1.街道问题
**
OJ.20
一个城市的街道布局如下:从最左下方走到最右上方,每次只能往上或往右走,一共有多少种走法?
Input
输入很多行行数,每行1个数字代表n的值,当n=0时结束(2<=n<=15)
Sample Input
1
2
10
5
0
Output
输出对应每行n值的走法.
Sample Output
2
6
184756
252
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,j,k;
while(scanf("%d",&n)!=EOF&&n!=0)
{
long long data[n+1][n+1];
for(int i=0;i<=n;i++)
{
data [i][0]=1;data[0][i]=1;
}
for(int j=1;j<=n;j++)
for(int k=1;k<=n;k++)
data[j][k]=data[j-1][k]+data[j][k-1];
cout << data[n][n]<< endl;}
return 0;
}
其他OJ题解
#include <bits/stdc++.h>
using namespace std;
int gcd (int x, int y) {
return y == 0?x:gcd(y, x%y);
}
int main () {
int n;
int a, b, c, d, e, f;
int ans1, ans2;
cin >> n;
for (int i = 0; i < n; i ++) {
cin >> a >> b >> c >> d;
e = a*d + b*c;
f = b*d;
ans1 = e/gcd(e,f);
ans2 = f/gcd(e,f);
cout << ans1 << ' ' << ans2 << endl;
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main ()
{ int n,k;
while(scanf("%d",&n)!=EOF)
{k=1;
for(int i=1;i<=n;i++)
k=k*i;
cout <<k<< endl;
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
double f(double x)
{ double f;f=3*x*x+2*x+4;
return(f);
}
int main ()
{ double x,y,h;
while(scanf("%lf%lf",&x,&h)!=EOF)
{ y=(f(x+h)-f(x))/h;
printf("%.2lf",y);
cout<<endl;}
return 0;
}