请编写程序输出前n个正整数的全排列(n<10),并通过9个测试用例(即n从1到9)观察n逐步增大时程序的运行时间。
输入格式:
输入给出正整数n(<10)。
输出格式:
输出1到n的全排列。每种排列占一行,数字间无空格。排列的输出顺序为字典序,即序列a1,a2,⋯,an排在序列b1,b2,⋯,bn之前,如果存在k使得a1=b1,⋯,ak=bk 并且 ak+1<bk+1。
#include <stdio.h>
#include <stdlib.h>
int a[11],b[11];
int n;
void find(int a[],int b[],int x)
{
if(x==n+1)
{
for(int i=1; i<=n; i++)
printf("%d",b[i]);
printf("\n");
}
for(int i=1; i<=n; i++)
{
if(a[i]==0)
{
a[i]=1;
b[x]=i;
find(a,b,x+1);
a[i]=0;
}
}
}
int main()
{
scanf("%d",&n);
find(a,b,1);
return 0;
}
Drizzle 前往山地统计大山的数目,现在收到这片区域的地图,地图中用0(平地)
和1(山峰)
绘制而成,请你帮忙计算其中的大山数目
山总是被平地四面包围着,每一座山只能在水平或垂直方向上连接相邻的山峰而形成。一座山峰四面被平地包围,这个山峰也算一个大山
另外,你可以假设地图的四面都被平地包围着。
要求:
输入:第一行输入M,N分别表示地图的行列,接下来M行每行输入N个数字表示地图
输出:输出一个整数表示大山的数
#include <stdio.h>
#include <stdlib.h>
int a[2100][2100];
int n,m;
int maxx;
int doit(int x,int y)
{
if(x>n||y>m||x<1||y<1||a[x][y]==0)
return 0;
a[x][y]=0;
doit(x+1,y);
doit(x,y+1);
doit(x-1,y);
doit(x,y-1);
return 1;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
scanf("%d",&a[i][j]);
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
if(doit(i,j)>0)
maxx++;
printf("%d",maxx);
return 0;
}
Drizzle 被困到一条充满数字的方块路中,假设这条路由一个非负的整数数组m
组成,Drizzle 最开始的位置在下标 start
处,当他位于下标i
位置时可以向前或者向后跳跃m[i]
步数,已知元素值为0
处的位置是出口,且只能通过出口出去,不可能数组越界,请你通过编程计算出Drizzle能否逃出这里。
#include <bits/stdc++.h>
using namespace std;
const int N = 50010;
bool flag[N];
int m[N];
int n,st;
void dfs(int x){
if(m[x] == 0){
printf("True");
exit(0);
}
if(flag[x]) return;
flag[x] = true;
if(x - m[x] >= 1) dfs(x - m[x]);
if(x + m[x] <= n) dfs(x + m[x]);
}
int main(){
cin >> n;
for (int i = 1;i <= n;i ++)
cin >> m[i];
cin >> st;
dfs(st + 1);
printf("False");
return 0;
}
小明在做丁达尔效应的实验。
他用胶体填满了一个有 N 行, M 列个格子的透明盒子。同时,为了使实验效果更好,他在盒子的某些格子中装入一些双面镜(用“/”和“\”表示)。
当光线找到双面镜的时候,光路会反射,方向会有90度的转换(如下图所示)。
小明在填充胶体的时候产生了失误,使得有些格子光线无法穿过(我们可以认为这些格子把光线都吸收了),这些格子用大写字母“C”来表示。
现在,小明已经制作好了实验装置,他将激光光源放在第sx行的第sy个格子上。激光光源可以朝向上下左右四个方向,分别用URDL四个字母表示(“U”-上, “R”-右, “D”-下, “L”-左)。
为了让实验效果更好,小明希望光的光路越酷越好。
对小明来说,最酷的光路就是包含环的光路,这种情况下,光不会射向盒子外面,而是一直在盒子内循环(数据保证光源处的点是光线可以通过,即起点一点是".")。
如果光源朝各个方向摆设,最终光线都会射向盒子外部,那么小明认为经过格子最多的光路是最酷的。
现在小明想知道光源朝哪个方向放置光路最酷。
如果光路是不包含环的,那么他还想知道光路经过的格子数目是多少。
注意
如果有多个方向是最优解,那么我们按照“U”-上, “R”-右, “D”-下, “L”-左的优先级来选择最终的答案,即如果向左和向右都是最优解,我们选择向右的方案。
#include <bits/stdc++.h>
using namespace std;
const int N = 510;
int dx[4] = {-1,0,1,0};
int dy[4] = {0,1,0,-1};//上、右、下、左
//0 1 2 3
int a[N][N];
int flag[N][N];
int flag1[N][N][5];
int ans;
int n,m;
char c2[4] = {'U','R','D','L'};
char c,c1;
int calc(){
int tmp = 0;
for(int i = 1;i <= n;i ++)
for(int j = 1;j <= m;j ++)
if(flag[i][j])
tmp += flag[i][j];
return tmp;
}
void dfs(int x,int y,int f){
int xx = x + dx[f];
int yy = y + dy[f];
if(xx <= 0 || xx > n || yy <= 0 || yy > m || a[xx][yy] == 0){
// cout << c << " " << calc() << endl;
if(calc() > ans){
ans = calc();
c1 = c;
}
return;
}
if(flag1[xx][yy][f + 1]){
printf("%c\nCOOL",c);
exit(0);
}
flag1[xx][yy][f + 1] = true;
flag[xx][yy] ++;
if(a[xx][yy] == 2 && f == 2) dfs(xx,yy,3);
else
if(a[xx][yy] == 2 && f == 1) dfs(xx,yy,0);
else
if(a[xx][yy] == 2 && f == 0) dfs(xx,yy,1);
else
if(a[xx][yy] == 2 && f == 3) dfs(xx,yy,2);
else
if(a[xx][yy] == 3 && f == 2) dfs(xx,yy,1);
else
if(a[xx][yy] == 3 && f == 1) dfs(xx,yy,2);
else
if(a[xx][yy] == 3 && f == 0) dfs(xx,yy,3);
else
if(a[xx][yy] == 3 && f == 3) dfs(xx,yy,0);
else dfs(xx,yy,f);
return;
}
int main(){
cin >> n >> m;
for(int i = 1;i <= n;i ++){
string s;
cin >> s;
for(int j = 1;j <= m;j ++){
if(s[j - 1] == '.') a[i][j] = 1;
if(s[j - 1] == '/') a[i][j] = 2;
if(s[j - 1] == '\\') a[i][j] = 3;
}
}
int sx,sy;
cin >> sx >> sy;
for(int i = 0;i < 4;i ++){
c = c2[i];
memset(flag,0,sizeof(flag));
memset(flag1,0,sizeof(flag1));
flag[sx][sy] = 1;
flag1[sx][sy][i + 1] = true;
dfs(sx,sy,i);
}
cout << c1 << endl;
cout << ans;
return 0;
}
我们称一个数是回文的,当且仅当它正着读和倒着读是相同的。
例如11或11455411是回文的,而10或1919810不是回文的。
现在给定一个数n,你需要求出区间[108,n]中所有的回文数。
输入格式:
一行一个整数n(108≤n<109)
输出格式:
输出一行一个数,表示题目所求区间中回文数的数量。
输入样例:
在这里给出一组输入。例如:
#include <bits/stdc++.h>
using namespace std;
int n,ans,cnt;
int power[10];
int tmp[100000];
void init(){
power[0] = 1;
for(int i = 1;i < 10;i ++)
power[i] = power[i - 1] * 10;
}
bool check(int x){
vector <int> v;
if(x == 0) return true;
while(x){
v.push_back(x % 10);
x = x / 10;
}
if((v.size() + 1) % 2 == 1) return false;
for(int i = 0;i < v.size();i ++){
if(v[i] != v[v.size() - i - 1]) return false;
}
return true;
}
int calc(int x){
if(x / 10 == 0)
return x * power[4];
if(x / 1000 == 0)
return x * power[3];
return x * power[2];
}
int main(){
init();
cin >> n;
for(int i = 0;i < 100000;i ++)
if(check(i))
tmp[++ cnt] = i;
for(int i = 1;i <= 99;i ++){
if(i % 10 == 0) continue;
int x = i % 10;
x = x * power[8] + (i / 10) * power[7] + i;
int tmp1 = x;
if(x > n) continue;
for(int j = 1;j <= cnt;j ++){
x = tmp1;
x = x + calc(tmp[j]);
// cout << x << endl;
if(x <= n) ans ++;
}
// cout << ans << endl;
}
cout << ans;
return 0;
}