A. 真的是Hello world!
Description
我们学习一门编程语言时,第一个程序都是请你编程序输出“Hello world!”,今天就请你编程序输出“Hello world!”,只不过根据不同情况,有时是正着输出,有时是反着输出。当输入是0时正着输出,当输入是1时反着输出。
Input
本问题有多组测试数据,对于每组测试数据,输入只有一行,一个整数0或者1。
Output
对于每组测试数据,输出也只有一行,分别是正着输出还是反着输出的“Hello world!”。
Sample Input
0
0
1
0
Sample Output
Hello world!
Hello world!
!dlrow olleH
Hello world!
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
if(n==0)
{
cout<<"Hello world!"<<endl;
}
if(n==1)
{
cout<<"!dlrow olleH"<<endl;
}
}
return 0;
}
有趣的箭头
Description
Input
本问题有多组测试数据,每组测试数据只有一行,分别是用空格隔开的一个正整数n和两个字符,第一个字符表示朝向,一定是L、R、U、D之一,后一个字符是填充的字符。
Output
对于每一组测试数据,输出符合要求的箭头图案。
Sample Input
5 R $
Sample Output
$
$$
$$$
$$$$
$$$$$
$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$
$$$$$
$$$$
$$$
$$
$
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<cmath>
#include<cstring>
#include<stack>
using namespace std;
int main()
{
int n,i,j;
char a,b;
while(cin>>n>>a>>b)
{
if(a=='L')
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n*2-i;j++)
{
cout<<" ";
}
for(j=1;j<=i;j++)
{
cout<<b;
}
for(j=1;j<=4*n-1-(i+n*2-i);j++)
{
cout<<" ";
}
cout<<endl;
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
cout<<" ";
}
for(j=1;j<=n*4-(n-i)-1;j++)
{
cout<<b;
}
cout<<endl;
}
for(i=1;i<=n-1;i++)
{
for(j=1;j<=i;j++)
{
cout<<" ";
}
for(j=1;j<=4*n-i-1;j++)
{
cout<<b;
}
for(j=1;j<=4*n-1-(4*n-i-1+i);j++)
{
cout<<" ";
}
cout<<endl;
}
for(i=1;i<=n;i++)
{
for(j=1;j<=2*n-(n+1-i);j++)
{
cout<<" ";
}
for(j=1;j<=n+1-i;j++)
{
cout<<b;
}
for(j=1;j<=4*n-1-(n+1-i+2*n-(n+1-i));j++)
{
cout<<" ";
}
cout<<endl;
}
}
else
{
if(a=='R')
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n*2-1;j++)
{
cout<<" ";
}
for(j=1;j<=i;j++)
{
cout<<b;
}
for(j=1;j<=4*n-1-(i+n*2-1);j++)
{
cout<<" ";
}
cout<<endl;
}
for(i=1;i<=n;i++)
{
for(j=1;j<=3*n+i-1;j++)
{
cout<<b;
}
for(j=1;j<=4*n-1-(3*n+i-1);j++)
{
cout<<" ";
}
cout<<endl;
}
for(i=1;i<=n-1;i++)
{
for(j=1;j<=4*n-i-1;j++)
{
cout<<b;
}
for(j=1;j<=4*n-1-(4*n-i-1);j++)
{
cout<<" ";
}
cout<<endl;
}
for(i=1;i<=n;i++)
{
for(j=1;j<=2*n-1;j++)
{
cout<<" ";
}
for(j=1;j<=n-i+1;j++)
{
cout<<b;
}
for(j=1;j<=4*n-1-(n-i+1+2*n-1);j++)
{
cout<<" ";
}
cout<<endl;
}
}
else
{
if(a=='U')
{
for(i=1;i<=2*n;i++)
{
for(j=1;j<=2*n-i;j++)
{
cout<<" ";
}
for(j=1;j<=2*i-1;j++)
{
cout<<b;
}
for(j=1;j<=4*n-1-(2*i-1+2*n-i);j++)
{
cout<<" ";
}
cout<<endl;
}
for(i=1;i<=2*n-1;i++)
{
for(j=1;j<=n;j++)
{
cout<<" ";
}
for(j=1;j<=2*n-1;j++)
{
cout<<b;
}
for(j=1;j<=n;j++)
{
cout<<" ";
}
cout<<endl;
}
}
else
{
for(i=1;i<=2*n-1;i++)
{
for(j=1;j<=n;j++)
{
cout<<" ";
}
for(j=1;j<=2*n-1;j++)
{
cout<<b;
}
for(j=1;j<=n;j++)
{
cout<<" ";
}
cout<<endl;
}
for(i=1;i<=2*n;i++)
{
for(j=1;j<=i-1;j++)
{
cout<<" ";
}
for(j=1;j<=4*n-(i-1)*2-1;j++)
{
cout<<b;
}
for(j=1;j<=4*n-1-(i-1+4*n-(i-1)*2-1);j++)
{
cout<<" ";
}
cout<<endl;
}
}
}
}
}
return 0;
}
构造最小数
Description
设有n(n<=1000)个正整数,将它们拼接在一起,组成一个很多位的正整数,每个数能且只能在拼接好后的数中出现一次,各个数出现的次序随意,请你编程序找出最小的数。
Input
本问题有多组测试数据,对于每组测试数据,输入有两行,第一行为正整数的个数n,第二行为用空格隔开的n个正整数。
Output
对于每组测试数据,输出只有一行,即能够组成的最小的数。
Sample Input
3
12 34 56
4
13 22 567 3333
Sample Output
123456
13223333567
#include<bits/stdc++.h>
using namespace std;
string s[1001];
bool bj(const string &a,const string &b)
{
return a+b>b+a;
}
int main()
{
int n,i;
while(cin>>n)
{
for(i=1;i<=n;i++)
{
cin>>s[i];
}
sort(s+1,s+n+1,bj);
for(i=n;i>=1;i--)
{
cout<<s[i];
}
cout<<endl;
}
return 0;
}
国王的试酒员
Description
从前有一个国王,他拥有n瓶葡萄酒,国王将在明天(也就是24小时之后)举办一个大型的庆祝活动,并需要这n瓶葡萄酒用于宴请,但从间谍处得到消息,这n瓶酒中的其中一瓶,被人下了毒,而这种毒为慢性毒药,只要碰到一滴就会中毒,中毒者当场不会有任何表现,而是在10-20小时之间才会表现出中毒样并毒发身亡。国王拥有成千上万的奴隶正在建造新宫殿,他可以从中选取一定数量的奴隶来充当试酒的人,而因为选中的人会耽误工程,所以他希望能够选最少的奴隶试酒并找出那瓶毒酒。假如你就是那个国王,假如你拥有足够多用于试酒的杯子,你最少需要多少名奴隶才能在24小时之内找出哪一瓶为毒酒?请你编程序求解之。
Input
本问题有多组测试数据,对于每组测试数据,输入只有一行,即酒的瓶数n(1<=n<=1000000000)。
Output
对于每组测试数据,输出只有一行,即需要奴隶的最少人数。
Sample Input
2
Sample Output
1
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
string s;
while(cin>>n)
{
n--;
s="";
while(n!=0)
{
s=(char)(n%2+'0')+s;
n/=2;
}
cout<<s.size()<<endl;
}
return 0;
}
蚂蚁国的交通问题
Description
有一根长度为L厘米的细木杆,现有n只蚂蚁在细木杆上,这n只蚂蚁的初始位置分别距离细木杆的左端第a1、a2、a3、……、an厘米处,木杆很细,不能同时通过两只蚂蚁。开始时,蚂蚁的头朝左还是朝右是任意的,它们只会朝前走或调头,但不会后退,当任意两只蚂蚁碰头时,两只蚂蚁会同时调头朝反方向走,在这里我们假定两只蚂蚁在碰头之前各自是不会自己掉头的,现在假设蚂蚁们每秒钟可以走一厘米的距离。请你编写程序,计算一下所有蚂蚁都离开木杆的最小时间和最大时间。
Input
本问题有多组测试数据,对于每组测试数据,输入有两行,第一行是用空格隔开的两个正整数L(1<=L<=1000000)和n(1<=n<=100000);第二行是n个正整数a1、a2、a3、……、an(0<=a1,a2,a3,…,an<=L),相邻两数之间用空格隔开。
Output
对于每组测试数据,输出只有一行,用空格隔开的两个正整数,分别表示所有蚂蚁都离开木杆的最小时间和最大时间。
Sample Input
3 1
1
Sample Output
1 2
#include<bits/stdc++.h>
using namespace std;
int a[100001];
int main()
{
int l,n,i,mx,mi,dd,xx;
while(cin>>l>>n)
{
mx=-999999999;
mi=-999999999;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
dd=max(l-a[i],a[i]);
xx=min(l-a[i],a[i]);
mx=max(mx,dd);
mi=max(mi,xx);
}
printf("%d %d\n",mi,mx);
}
return 0;
}
带权的最大子段和问题
Description
在短学期和算法导论课期间,我们一共讨论了四种求解最大子段和问题的方法,上一届的期末考试也考了交替出现的子段和问题,今天我们再拓展一下,研究一下最大的带权子段和问题,这个问题我们已经找到了很有意思的应用背景。例如已知三个权系数分别为1、-2、1,考虑从i开始的序列,带权的累加和是:a[i]-2a[i+1]+a[i+2]+a[i+3]-2a[i+4]+a[i+5]+…,你的任务是编程序计算最大的带权子段和。
Input
本问题有多组测试数据,对于每组测试数据,输入有三行,第一行是两个用空格隔开的n和m,其中1<=m<=100,m<=n<=100000;第二行是用空格隔开的m个整数,表示带权的系数;第三行是n个用空格隔开的整数,表示一个有n个元素的序列。输入保证中间运算过程不会超过int范围。
Output
对于每组测试数据,输出只有一行,即最大的子段和的值。
Sample Input
10 3
1 -2 1
10 -9 11 -2 -3 12 8 7 -10 6
Sample Output
63
#include<bits/stdc++.h>
using namespace std;
int a[100001],b[100001],n,m,ans;
#define INF 2100000000
int main()
{
int i,j,s,k;
while(cin>>n>>m)
{
for(i=1;i<=m;i++)
{
scanf("%d",b+i);
}
for(i=1;i<=n;i++)
{
scanf("%d",a+i);
}
ans=-INF;
for(i=1;i<=m;i++)
{
s=0;
for(j=i;j<=n;j++)
{
k=(j-i)%m;
if(s<0&&k==0)
{
s=b[1]*a[j];
}
else
{
s+=b[k+1]*a[j];
}
ans=max(ans,s);
}
}
cout<<ans<<endl;
}
return 0;
}