A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

看看:D:D
回复 使用道具 举报
kankantimu
回复 使用道具 举报
挑战无极限
回复 使用道具 举报
看看!!!
回复 使用道具 举报
瞧瞧,看看.哈哈
回复 使用道具 举报
什么题嘛?我来见见世面!
回复 使用道具 举报
让我来看看!
回复 使用道具 举报
看看,很给力
回复 使用道具 举报

RE: =========!!!!!!===敢挑战吗?===!!!!!!=======

只为看题
回复 使用道具 举报
如果是 Java 的,我现在来挑战。算法的也可以。
回复 使用道具 举报
回复看题~
回复 使用道具 举报
看看是啥。。
回复 使用道具 举报
本帖最后由 崔宏奎 于 2013-5-1 00:44 编辑

真心没学过矩阵。。。

  1. static void Main(string[] args)
  2.         {
  3.             //计数:
  4.             int start = 1;
  5.             //总数
  6.             int num;
  7.             string str;
  8.             do
  9.             {
  10.                 Console.Write("请输入一个整数:");
  11.                 str = Console.ReadLine();
  12.             } while (!int.TryParse(str, out num) || num < 1);
  13.             //num=num+2;

  14.             string[,] arr = new string[num + 2, num + 2]; //创建数组
  15.             //起始位置:
  16.             int col = (num +1) / 2, row = (num + 1) / 2;  //pos=1;

  17.             //第一圈,特殊
  18.             arr[col, row] = start++.ToString();
  19.             arr[col, ++row] = start++.ToString();
  20.             arr[++col, row] = start++.ToString();
  21.             arr[col, --row] = start++.ToString();
  22.             //第二圈
  23.             for (int i = 1; i < num - 1; ++i)
  24.             {
  25.                 if (i % 2 == 1)   //单数
  26.                 {
  27.                     arr[col, --row] = start++.ToString();
  28.                     //上移
  29.                     for (int j = 0; j <=  i; ++j)
  30.                     {
  31.                         arr[--col, row] = start++.ToString();
  32.                     }
  33.                     //右移
  34.                     for (int j = 0; j <=  i; ++j)
  35.                     {
  36.                         arr[col, ++row] = start++.ToString();
  37.                     }
  38.                 }
  39.                 else   //双数
  40.                 {
  41.                     arr[col, ++row] = start++.ToString();
  42.                     //下移
  43.                     for (int j = 0; j <= i; ++j)
  44.                     {
  45.                         arr[++col, row] = start++.ToString();
  46.                     }
  47.                     //左移
  48.                     for (int j = 0; j <=  i ; ++j)
  49.                     {
  50.                         arr[col, --row] = start++.ToString();
  51.                     }

  52.                 }

  53.             }

  54.             for (int i = 0; i < num + 2;++i )
  55.             {
  56.                 arr[0, i] = "*";
  57.                 arr[i, 0] = "*";
  58.                 arr[i, num+1] = "*";
  59.                 arr[num + 1, i] = "*";
  60.             }
  61.             PrintArr(arr);

  62.             Console.ReadKey();

  63.         }
  64.         public static void PrintArr(string[,] arr)
  65.         {
  66.             int len = arr.GetLength(0);
  67.             for (int i = 0; i < len; ++i)
  68.             {

  69.                 for (int j = 0; j < len; ++j)
  70.                 {
  71.                     Console.Write("{0}\t", arr[i, j]);
  72.                 }
  73.                 Console.WriteLine();
  74.             }
  75.         }
复制代码
回复 使用道具 举报
看看。。。
回复 使用道具 举报
本帖最后由 张世钦 于 2013-5-1 01:33 编辑

不错的题目,帮顶了~~~
回复 使用道具 举报
看看是什么样子的东西。。。
回复 使用道具 举报
这个题目的可心就是模拟赋值的路线,根据 n 分别取3,4,5时候,观察数组的赋值路线,摸索出一般的规律
下面是我的代码,鉴于对Java的输入输出还不太熟悉(又不想用字符串数组),下面的输出有点别扭
  1. package cn.xml;

  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.io.OutputStreamWriter;
  8. import java.io.PrintStream;

  9. import org.junit.Test;

  10. public class StreamResult {
  11.        
  12.          
  13.            /**
  14.              *   
  15.              * last :记录当前变法的坐标,这也是整个题目实现的核心
  16.              * step :走的圈数
  17.              * x,y  :代表每圈  的其实位置
  18.              * cnt  :计数统计
  19.              * len  :步长,记录每一圈的步长
  20.              *
  21.              *
  22.             **/
  23.         public   static void main(String args[]) throws Exception
  24.         {
  25.                 int n = 0;
  26.                 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  27.                 String num;
  28.                 while( (num = in.readLine())!=null)
  29.                 {
  30.                           try
  31.                           {
  32.                                   n = Integer.parseInt(num);
  33.                           }
  34.                           catch(Exception e)
  35.                           {
  36.                                   System.out.println("您的输入有误哦,请你重新输入");
  37.                                   continue;
  38.                           }
  39.                      
  40.                           if(n<1)
  41.                           {
  42.                                   System.out.println("您的输入有误哦,请你重新输入");
  43.                                   continue;
  44.                           }
  45.                           
  46.                       int map[][] = new int[n+2][n+2];
  47.                      
  48.                           int ct= n/2;
  49.                           if(n%2==1)
  50.                             ct ++;
  51.                           
  52.                           int x,y,last,cnt=1;
  53.                            x = y = last = ct;
  54.                          
  55.                           for(int step = 1; step<=ct; step++)
  56.                           {
  57.                                   int len = step*2 -1;
  58.                                   /** 向上走 **/
  59.                                   
  60.                                   for(int i=0;i<len;i++)
  61.                                   {
  62.                                           last = x-i;
  63.                                           map[last][y] = cnt++;
  64.                                   }
  65.                                   
  66.                                
  67.                                   /** 向右走 **/
  68.                                   x = last;
  69.                                   y = y+1;
  70.                                   for(int i=0;i<len;i++)
  71.                                   {
  72.                                           last = y+i;
  73.                                           map[x][last] = cnt++;
  74.                                   }
  75.                                  
  76.                                   
  77.                                   /** 向下走 **/
  78.                                   
  79.                                   x = x+1;
  80.                                   y = last;
  81.                                   for(int i=0;i<len;i++)
  82.                                   {
  83.                                           last = x +i;
  84.                                           map[last][y] = cnt++;
  85.                                   }
  86.                                   
  87.                                   /** 向左走 **/
  88.                                   
  89.                                   x = last;
  90.                                   y = y-1;
  91.                                   for(int i=0;i<len;i++)
  92.                                   {
  93.                                           last = y-i;
  94.                                           map[x][last] = cnt++;
  95.                                   }
  96.                                   y = last-1;
  97.                                   
  98.                           }
  99.                           
  100.                   
  101.                   /** 输出结果,表示很别扭 **/
  102.                  
  103.                   for(int i=0;i<=n+1;i++)
  104.                   {
  105.                         if(i==0 || i==n+1)
  106.                                 {
  107.                                   for(int j=0;j<=n+1;j++)
  108.                                           System.out.printf("%-5c",'*');
  109.                                 }
  110.                                
  111.                         else
  112.                         {
  113.                                 for(int j=0;j<=n+1;j++)
  114.                                 {
  115.                                         if(j==0 || j==n+1)
  116.                                                 System.out.printf("%-5c",'*');
  117.                                         else
  118.                                         {
  119.                                                 System.out.printf("%-5d", map[i][j]);
  120.                                         }
  121.                                        
  122.                                 }
  123.                         }
  124.                        
  125.                         System.out.println("\n");
  126.                        
  127.                   }
  128.                 }
  129.                
  130.         }

  131.    
  132. }
复制代码
运行结果:
1
*    *    *   

*    1    *   

*    *    *   

3
*    *    *    *    *   

*    7    8    9    *   

*    6    1    2    *   

*    5    4    3    *   

*    *    *    *    *   

4
*    *    *    *    *    *   

*    7    8    9    10   *   

*    6    1    2    11   *   

*    5    4    3    12   *   

*    16   15   14   13   *   

*    *    *    *    *    *   

5
*    *    *    *    *    *    *   

*    21   22   23   24   25   *   

*    20   7    8    9    10   *   

*    19   6    1    2    11   *   

*    18   5    4    3    12   *   

*    17   16   15   14   13   *   

*    *    *    *    *    *    *   

0
您的输入有误哦,请你重新输入

评分

参与人数 1技术分 +2 收起 理由
黄玉昆 + 2

查看全部评分

回复 使用道具 举报
本帖最后由 杞文明 于 2013-5-1 02:44 编辑

弄了一哈! 哎!  好麻烦哦!


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 数组的题
{
class Program
{
static void Main(string[] args)
{

/*
思想:如果我们求的二维数组的行数是偶数,那么一个外围中大小一个是这样的
* 下边→右边→上边→左边 例如:行数是4的数的二维数组
* 7 8 9 10
* 6 1 2 11
* 5 4 3 12
* 16 15 14 13
* 他们从大到小是 16 15 14 → 13 12 11 → 10 9 8 → 7 6 5
* 用这种方式一层一层的给二位数组赋值
*
*
如果我们求的二维数组的行数是奇数数,那么一个外围中大小一个是这样的
* 上边→左边→下边→右边 例如:行数是3的数的二维数组
* 7 8 9
* 6 1 2
* 5 4 3
* 他们从大到小是 9 8 → 7 6 → 5 4 → 3 2
* 用这种方式一层一层的给二位数组赋值
*/

Console.WriteLine("输入一个数");
//转换为int型
int num =Convert.ToInt32( Console.ReadLine());
//定义数组
int[,]shuzu = new int[num,num];

int xxh = num-1; //用去记住我们要循环的开始位置和结束位置
int num1 = num; //用于求最大值
//循环的次数是行数的一半
for (int w = (num + 1) / 2; w >= 1; w--)
{
#region 偶数
if (num % 2 == 0)
{
//本次循环的最大值
int zuida = num1 * num1;

//下边
for (int j = num - 1 - xxh; j < xxh; j++)
{//下边:从左往右赋值 依次减小1
shuzu[xxh, j] = zuida;
zuida--;
}

//右边
for (int j = xxh; j > num - 1 - xxh; j--)
{//右边:从下往上赋值 依次减小1
shuzu[j, xxh] = zuida;
zuida--;
}

//上面
for (int j = xxh; j > num - 1 - xxh; j--)
{ //上边:从右往左赋值 依次减小1
shuzu[num - 1 - xxh, j] = zuida;
zuida--;
}

//左边
for (int j = num - 1 - xxh; j < xxh; j++)
{//左边:从上往下赋值 依次减小1
shuzu[j, num - 1 - xxh] = zuida;
zuida--;
}
xxh--; //开始和结束位置向中间靠拢
num1 = num1 - 2; //前一个和后一个偶数相差2
}
#endregion

#region 奇数
/*和偶数的循环同理 只是 他的大小事这样的: 上边→左边→下边→右边*/
if (num % 2 == 1)
{
int zuida = num1 * num1;
//上面一行
for (int j = xxh; j >= num - 1 - xxh; j--)
{
shuzu[num - 1 - xxh, j] = zuida;
zuida--;
}
//最左边
zuida++;
for (int j = num - 1 - xxh; j < xxh; j++)
{
shuzu[j, num - 1 - xxh] = zuida;
zuida--;
}

//最后一行

for (int j = num - 1 - xxh; j <= xxh; j++)
{
shuzu[xxh, j] = zuida;
zuida--;
}

//最右边这排
zuida++;
for (int j = xxh; j > num - 1 - xxh; j--)
{
shuzu[j, xxh] = zuida;
zuida--;
}

xxh--;
num1 = num1 - 2;

}
#endregion

}

//这个for是显示第一行的*号的
for (xxh = 0; xxh < num+2; xxh++)
{
Console.Write("*\t");
}

//这个for是显示数据和显示数据前面和后面的*
for (int k = 0; k < num; k++)
{
Console.WriteLine();
Console.WriteLine();
Console.Write("*\t");
for (int l = 0; l < num; l++)
{
Console.Write("{0}\t",shuzu[k, l]);
}
Console.Write("*");
}

//数据完成后,换一哈行
Console.WriteLine("\n\t");

//这个for是显示最后一行的*号的
for ( xxh = 0; xxh < num + 2; xxh++)
{
Console.Write("*\t");
}

Console.ReadKey();
}
}
}



评分

参与人数 1技术分 +4 黑马币 +3 收起 理由
黄玉昆 + 4 + 3

查看全部评分

回复 使用道具 举报
rhrh看看了。。
回复 使用道具 举报
看看,学习学习
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马