算法学习日记(一)

递归之分图形


一个光弹幕的组成是p

两个则是p   p
      p
    p    p
其它则为
            B(n-1)   B(n-1)
         B(n-1)
     B(n-1)     B(n-1)


  • INPUT:
    输入多组数据,每组一个整数n(n<=7),表示宇宙的尺度,最后一行以-1结束。
  • OUTPUT:
    sample
    1
    2
    3
    -1

     p


      p   p
      p
    p    p


      p   p      p   p
      p          p
    p    p      p   p
           p   p
           p
         p    p
      p   p      p   p
      p             p
    p    p      p   p


#include<iostream>
#include<stdlib.h>
#include<cmath>
#include<algorithm>
#include<vector>
#include<string.h>
#include<string>
#include<stdio.h>
using namespace std;
int map[2187][2187];
void dfs(int n,int x,int y)
{
	int size;
	if(n==1)
	{
		map[x][y]='p';
		return ;
		
	} 
	size=pow(3,n-2);
	dfs(n-1,x,y);
	dfs(n-1,x,y+2*size);
	dfs(n-1,x+size,y+size);
	dfs(n-1,x+2*size,y); 
	dfs(n-1,x+2*size,y+2*size);
	
	
	
	
}
int main()
{
int n,size;
while(scanf("%d",&n)==1) 
{
	if(n==-1)
	{
		break;
	}
	
	size=pow(3,n-1);
	for(int i=1;i<=size;i++)
	{
		for(int j=1;j<=size;j++)
		{
			map[i][j]=' ';
		}
	}
	
	dfs(n,1,1);
	
	for(int i=1;i<=size;i++)
	{
		for(int j=1;j<=size;j++)
		{
			printf("%c",map[i][j]);
		}
		printf("\n");
	}
	
		printf("-\n");
}


return 0;
}


基本排列算法(一)

描述

给定一个只包含小写字母’a’-'z’的字符串 S ,你需要将 S 中的字符重新排序,使得任意两个相同的字符不连在一起。

如果有多个重排后字符串满足条件,输出字典序最小的一个。

如果不存在满足条件的字符串,输出INVALID。

输入

字符串S。(1 ≤ |S| ≤ 100000)

输出

输出字典序最小的答案或者INVALID。
样例输入aaabc
样例输出abaca

#include<iostream>
#include<stdlib.h>
#include<cmath>
#include<algorithm>
#include<vector>
#include<string.h>
#include<string>
#include<stdio.h>
#define swap(i,j){int t=a[i];a[i]=a[j];a[j]=t;	}
using namespace std;
char *a=new char[10000] ;
int ok=1;
int title=0;
void print()
{
	
	
	for(int i=0;i<strlen(a)-1;i++)
{
			if(a[i]==a[i+1]	)
		{
			ok=0;	
			break; 
		}
	
}


if(ok&&!title)
{

	for(int i=0;i<strlen(a);i++)
	{
		
		printf("%c",a[i]);
		title++;
	}
	
}
ok=1;	
	
}
void per(int st,int end)
{
	

if (st==end)
{
print();
}	
	else
	 {	 	
	 	for(int i=st;i<=end;i++)
		 {
		 	swap(i,st);
		 	per(st+1,end);
		 	swap(i,st);
							 	
		 } 
	 	
	 	
	 	
	 }
	
} 
int main()
{


cin>>a;
per(0,strlen(a)-1);
if(title==0)
{
	cout<<"INVALID"<<endl;
}
return 0;
}

基本排列算法(二)

描述

如果一个1~N的排列P=[P1, P2, … PN]中的任意元素Pi都满足|Pi-i| ≤ 1,我们就称P是1-偏差排列。

给定一个N,请你计算一共有少个不同的排列是1-偏差排列。

例如对于N=3,有3个1-偏差排列:[1, 2, 3], [1, 3, 2], [2, 1, 3]。

输入

一个整数N。

对于50%的数据,1 ≤ N ≤ 15

对于100%的数据,1 ≤ N ≤ 50

输出

一个整数表示答案
样例输入3
样例输出3

#include<iostream>
#include<stdlib.h>
#include<cmath>
#include<algorithm>
#include<vector>
#include<string.h>
#include<string>
#include<stdio.h>
#define swap(i,j){int t=a[i];a[i]=a[j];a[j]=t;	}
using namespace std;
int a[50];
int n;
int num;
void per(int st,int end)
{
	if(st==end)
	{
	int ok=1; 
		for(int i=0;i<n;i++)
		{
		
		if(abs(a[i]-i-1)>1)
		{
			ok=0;
			break;
		}
		
		
		}
	if(ok)
	{
		
		num++;
		
	}
		
	}else
	{
		for(int i=st;i<=end;i++)
		{
			swap(i,st);		
			per(st+1,end);
			swap(i,st);
			
		}
	
	}
	
	
	
}
int main()
{
	

cin>>n;
for(int i=0;i<n;i++)
{
	a[i]=i+1;
}
per(0,n-1);
cout<<num<<endl;

return 0;
}

`

`

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值