http://bailian.openjudge.cn/xly2019/
A.数与字符串
#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int main(){
int a;
while(scanf("%d",&a)!=EOF&&a){
int sum=0;
int temp=a;
while(temp){
sum++;
temp=temp/10;
}
if(a>=pow(10,sum)-10){
cout<<a<<endl;
}else {
cout<<pow(10,sum-1)-1<<endl;
}
}
}
B:打印月历
肯定是按行打印
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string.h>
#include<cstring>
#include<string>
#include<algorithm>
#include<math.h>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<map>
using namespace std;
int year,month;
int day1[2][13]={
{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31}
};
int isRunYear(int y){
if((y%400==0)||(y%4==0&&y%100!=0)) {
return 366;
}else {
return 365;
}
}
int main(){
cin>>year>>month;
int sum=0;
for(int i=1900;i<year;i++){
sum+=isRunYear(i);
}
int x;
if(isRunYear(year)){
x=1;
for(int i=1;i<month;i++){
sum+=day1[1][i];
}
}else {
x=0;
for(int i=1;i<month;i++){
sum+=day1[0][i];
}
}
sum=(sum+1)%7;
cout<<"Sun Mon Tue Wed Thu Fri Sat"<<endl;
int f=0;
for(int i=1;i<sum;i++){
printf(" ");
f++;
}
for(int i=1;i<=day1[x][month];i++){
printf("%3d ",i);
f++;
if(f==7){
cout<<endl;
f=0;
}
}
return 0;
}
C:Hopscotch
记录路径的BFS
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <cstring>
using namespace std;
const int maxn=1007;
bool mp[maxn];
int n,m;
struct node{
int index;
int step;
vector<char> s;
};
void BFS(){
memset(mp,false,sizeof(mp));
queue<node> q;
node temp;
temp.index=n;
temp.step=0;
q.push(temp);
while(!q.empty()){
temp=q.front();q.pop();
for(int i=0;i<2;i++){
node now=temp;
if(i==0) {
now.index=temp.index*3;
if(!mp[now.index]) {
now.s.push_back('H');
q.push(now);
mp[now.index]=true;
}
}else {
now.index=temp.index/2;
if(!mp[now.index]){
now.s.push_back('O');
q.push(now);
mp[now.index]=true;
}
}
now.step++;
if(now.index==m){
int len=now.s.size();
cout << len << endl;
for(int i=0;i<len;i++){
cout<<now.s[i];
}
cout<<endl;
return;
}
}
}
}
int main(){
while(cin>>n>>m){
if(n==0&&m==0) break;
BFS();
}
return 0;
}
D:上楼梯
裸动态规划
每次可以走1 - K层楼梯(不包括含有4的层数),那么第n层楼梯,要么是从第n - 1层直接到达,要么是从 n - 2 层, n -3 层 … n - k层楼梯直接一步到达,所以有到达n的方法数 F[n] = F[n - 1] + F[n - 2] + … + F[i - k]注意不能从含有4的步数 j 一步到达。
#include<cstdio>
#include<iostream>
#include<string.h>
using namespace std;
int F[51]={0};
int main(){
int n,k;
while(scanf("%d%d",&n,&k)!=EOF){
memset(F,0,sizeof(F));
if(n==0&&k==0) break;
F[0]=1;
F[1]=1;
for(int i=2;i<=n;i++){
for(int j=1;j<=k&&j<=i;j++){
int p=j;
int flag=1;
while(p!=0) {
int r=p%10;
if(r==4) {
flag=0;
break;
}
p/=10;
}
if(flag) F[i]+=F[i-j];
}
}
printf("%d\n",F[n]);
}
}
E:Life Line
搜索
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn=17;
int n,c;
int zero;
bool vis[maxn][maxn];
int board[maxn][maxn];
int dir[6][2]={ {-1,0},{-1,-1},{0,-1},{0,1},{1,0},{1,1} };
int dfs(int x,int y,int c1){
if(board[x][y]==0) {
zero=1;
return 0;
}
vis[x][y]=1;
int cnt=1;
for(int d=0;d<6;d++){
int tx=x,ty=y;
tx+=dir[d][0],ty+=dir[d][1];
if(tx<=0||tx>n||ty<= 0||ty>tx||vis[tx][ty])
continue;
if(board[tx][ty]==0) {
zero=1;
return 0;
}
if(board[tx][ty]==c1)
cnt+=dfs(tx,ty,c1);
}
return cnt;
}
int solve(){
memset(vis,0,sizeof(vis));
int ans=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
if(board[i][j]!=0&&!vis[i][j]) {
zero=0;
int temp=dfs(i,j,board[i][j]);
if(!zero)
ans+=board[i][j]==c?-temp:temp;
}
}
}
return ans;
}
int main(){
while(scanf("%d %d",&n,&c)){
if(n==0&&c==0) break;
memset(board,0,sizeof(board));
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
scanf("%d",&board[i][j]);
}
}
int ans=-123456;
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
if(board[i][j]==0) {
board[i][j]=c;
ans=max(ans,solve());
board[i][j]=0;
}
}
}
cout<<ans<<endl;
}
}
F:跳蛙
模拟
G:Falling Leaves
二叉搜索树
https://blog.csdn.net/fish_your_god/article/details/106591321
H:昂贵的聘礼
最短路