本部校赛 蛇形填数(二)problen1338
Description
如2*3矩阵:
2 7 11
3 5 13
再如3*4的矩阵
2 13 17 37
3 11 19 31
5 7 23 29
Input
第一行为一个正整数T,表示数据的组数,接下来T行,每行两个正整数n,m,表示矩阵有n行m列
Output
Sample Input
2 3
3 4
Sample Output
3 5 13
2 13 17 37
3 11 19 31
5 7 23 29
Hint
1<=n,m<=100
超时代码:哭晕
#include<stdio.h>
#include<string.h>
#include<math.h>
int zhi(int num)
{
int i,j,p,h=;
static int k=;
for(i=k;;i++)
{
int p=sqrt(i);
for(j=;j<=p;j++)
{
if(i%j==)
break;
}
if(j<=p)
continue;
k=i+;
return i;
}
}
int main()
{ int a[][];
int m,n,x,y;
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&m,&n);
{
memset(a,,sizeof(a));
int count=;
a[x=][y=]=;
while()
{
while(!a[x+][y]&&x+<m)
a[++x][y]=zhi(++count);
while(!a[x][y+]&&y+<n)
a[x][++y]=zhi(++count);
while(!a[x-][y]&&x->=)
a[--x][y]=zhi(++count);
while(!a[x][y-]&&y->=)
a[x][--y]=zhi(++count);
if(count==m*n-)
break;
}
for(x=;x<m;x++)
{
for(y=;y<n;y++)
printf("%6d",a[x][y]);
printf("\n");
}
}
} }
AC代码
#include<stdio.h>
#include<string.h>
#include<math.h>
int b[];
int z()//求素数
{
int i,j,count=;
for(i=;;i++)
{
int k=sqrt(i);
for(j=;j<=k;j++)
{
if(i%j==)
break;
}
if(j<=k)
continue; b[count]=i;
count++;
if(count==)
break;
}
}
int main()
{ int a[][];
int m,n,x,y;
int T;
scanf("%d",&T);
z();
while(T--)
{
scanf("%d%d",&m,&n); memset(a,,sizeof(a));
int count=;
a[x=][y=]=;
while(count<m*n-)
{
while(!a[x+][y]&&x+<m)
a[++x][y]=b[++count];
while(!a[x][y+]&&y+<n)
a[x][++y]=b[++count];
while(!a[x-][y]&&x->=)
a[--x][y]=b[++count];
while(!a[x][y-]&&y->=)
a[x][--y]=b[++count];
}
for(x=;x<m;x++)
{
for(y=;y<n;y++)
printf("%6d",a[x][y]);
printf("\n");
} } }
本部校赛 蛇形填数(二)problen1338的更多相关文章
- <蛇形填数>--算法竞赛 入门经典(第2版)- 3.1 数组 程序3-3 蛇形填数
蛇形填数: 在n×n方阵里填入1,2,....,n×n,要求填成蛇形.例如,n = 4 时方阵为: 10 11 12 1 9 16 13 2 8 15 14 3 7 ...
- 【OI】蛇形填数题的深入探究
题目:在 n×n 方阵里填入 1,2,...n×n, 要求蛇形填数.例如,n=4 时方阵为: 10 11 12 1 9 16 13 2 8 15 14 3 7 6 ...
- ACM 蛇形填数
蛇形填数 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 在n*n方陈里填入1,2,...,n*n,要求填成蛇形.例如n=4时方陈为:10 11 12 19 16 1 ...
- nyoj 33 蛇形填数
蛇形填数 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 在n*n方陈里填入1,2,...,n*n,要求填成蛇形.例如n=4时方陈为: 10 11 ...
- ny33 蛇形填数
蛇形填数 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 在n*n方陈里填入1,2,...,n*n,要求填成蛇形.例如n=4时方陈为: 10 11 12 1 9 16 1 ...
- 【ACM】蛇形填数 - 逻辑怪
蛇形填数 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 在n*n方陈里填入1,2,...,n*n,要求填成蛇形.例如n=4时方陈为:10 11 12 19 16 1 ...
- 梦工厂实验室 蛇形填数 dfs
问题 D: 蛇形填数 时间限制: 3 Sec 内存限制: 64 MB提交: 28 解决: 5[提交][状态][讨论版] 题目描述 在n*n方阵里填入1,2,...,n*n,要求填成蛇形.例如n=4 ...
- nyoj_33_蛇形填数_201308221636
蛇形填数时间限制:3000 ms | 内存限制:65535 KB 难度:3描述 在n*n方陈里填入1,2,...,n*n,要求填成蛇形.例如n=4时方陈为:10 11 12 19 16 13 28 ...
- nyoj33 蛇形填数
蛇形填数 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描写叙述 在n*n方陈里填入1,2,...,n*n,要求填成蛇形.比如n=4时方陈为: 10 11 12 1 9 16 ...
随机推荐
- ReactiveCocoa v2.5 源码解析 之 架构总览
ReactiveCocoa 是一个 iOS 中的函数式响应式编程框架,它受 Functional Reactive Programming 的启发,是 Justin Spahr-Summers 和 J ...
- 【转】ios开发之生成所缩略图方式
亲测:两种方式都有效 第一种方式:缩略成固定的尺寸大小 - (UIImage *)thumbnailWithImageWithoutScale:(UIImage *)image size:(CGSiz ...
- poj2505-A multplication game
题意:两个人轮流用2~9来乘n,使n不断扩大,n开始为1.当给一个固定值k,谁先使n超过k谁赢. 分析:能到达必败态的状态为必胜态,只能到达必胜态的状态为必败态.对于给定的k,n>=k时为必败态 ...
- format %x invalid or incompatible with argument问题解决方法
现在还有好多朋友在用Protel 99se来画图,可是在现在的双核或四核电脑上运行Protel出现错误并且弹出对话框:“format '%x' invalid or incompatible with ...
- PowerShell中的输出
1 输出重定向 > 或者>> 2 输出控制 out-* -paging#分页输出 get-process | out-host -paging more指令用于屏显 get-pro ...
- Java面试题之十
五十六.java中有几种类型的流?JDK 为每种类型的流提供了一些抽象类以供继承,请说出他们分别是哪些类? 主要有字节流,字符流.字节流继承于InputStream .OutputStream,字符流 ...
- 处理emacs-org模式TODO的一个脚本
处理前: 处理后: Table of Contents 1 前言 2 中文的处理 2.1 vim相关 2.2 perl 相关 3 时间相关 4 程序解析 1 前言 最近风帆问我一个问题,也就是处理or ...
- geoip 添加一列,add_field =>["[geoip][request_time]","%{request_time}"]
"message" => " 10.171.246.184 [11/Sep/2016:14:42:53 +0800] \"GET /wechat/home ...
- 热点块引发的cache buffers cahins latch
热点块引发的Cache buffer Chains latch: SQL语句即便适当进行了调优,有时也无法解决cache buffers cahins latch,若在编写SQL语句时的SQL工作方式 ...
- Axure设计分析作业-实例解析
本文转载自人人都谁产品经理,作者完全使用Axure做了这一个产品需求文档.文档地址:http://1passwordmanager.sinaapp.com/ 大家可以先睹为快.这个PRD完全使用axu ...