Not all the resultsare listed in the sample. There are just some lines. The ellipsis expresseswhat you should write.
这是一道简单的按要求输出的题目,就是输出一个N,
从3X3到10X10
在本上画一画,不难发现,可以用循环打出表格,
比如4X4的:
①先从0,0→1,0→2,0→3,0
②从3,0(之前打过,不需要打)→2,1→1,2→0,3(给后面③打)
③从0,3→1,3→2,3→3,3
所以4X4的 ①,③循环四次,而②只需要循环2次,因为第一个和第四个都在①、③循环中出现
这样,这道题就搞定了,
你可以建立一个3维数组,来存储然后打出来,也可以像我这样做一个,打出来一个。
要注意一点,字母从a到z,z后面还是a,一直循环,每个N之间没有空行。
恩,代码:
// Children‘s day
#include <iostream>
#include <string.h>
using namespace std;
char map[11][11];
int main()
{
char a=‘a‘;
int i,j,k,x,y;
for(i=3;i<=10;++i)
{
// 初始化表格
memset(map,0,sizeof(map));
// 第一个循环
for(j=0;j<i;++j)
{
if(a>‘z‘) a-=26;
map[j][0]=a++;
}
//第二个循环
x=i-1,y=0;
for(j=1;j<=i-2;++j)
{
if(a>‘z‘) a-=26;
map[x+(-1)*j][y+1*j]=a++;
}
//第三个循环
for(j=0;j<i;++j)
{
if(a>‘z‘) a-=26;
map[j][i-1]=a++;
}
// 打印出来表格
for(j=0;j<i;++j)
{
for(k=0;k<i;++k)
{
if(map[j][k]==0) cout<<" ";
else cout<<map[j][k];
}
cout<<endl;
}
}
return 0;
}我没写,只是在网上找了一个,感觉真心强大,7个printf搞定了。。。
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int main()
{
printf("a e\nbdf\nc g\n");
printf("h n\ni mo\njl p\nk q\n");
printf("r z\ns ya\nt x b\nuw c\nv d\n");
printf("e o\nf np\ng m q\nh l r\nik s\nj t\n");
printf("u g\nv fh\nw e i\nx d j\ny c k\nzb l\na m\n");
printf("n b\no ac\np z d\nq y e\nr x f\ns w g\ntv h\nu i\n");
printf("j z\nk ya\nl x b\nm w c\nn v d\no u e\np t f\nqs g\nr h\n");
printf("i a\nj zb\nk y c\nl x d\nm w e\nn v f\no u g\np t h\nqs i\nr j\n");
return 0;
}
ACM-简单题之Children's Day——hdu4706,布布扣,bubuko.com
ACM-简单题之Children's Day——hdu4706
原文:http://blog.csdn.net/lttree/article/details/21008631