我不是很理解字符串提取方面的知识

楼主
我不是很理解字符串提取方面的知识
[P][SIZE=3][FACE=Times New Roman]有五个数字[/FACE][FACE=Times New Roman](1,2,3,4,5),[/SIZE][FACE=Times New Roman]从[/FACE][FACE=Times New Roman]1[/FACE][FACE=Times New Roman]开始数起,数到第三个数字就把那个数字输出来[/FACE][FACE=Times New Roman].[/FACE][FACE=Times New Roman]输出结果是[/FACE][FACE=Times New Roman]3 1  5  2  4[/FACE][/FACE][/P][P][FACE=Times New Roman][SIZE=3][/SIZE][/FACE] [/P][P][FACE=Times New Roman][SIZE=3]这种题目应该怎么写啊,那些提取字符串的语法 我就是套用不上来[/SIZE][/FACE][/P]
1楼
这个问题,其实和提取字符串没有任何关系。

我写了个C#的例子,你自己把它翻译成Java的吧。算法是通用的。

[CODE]//---------------------------------------------------------
// <copyright file="Program.cs" company="Yaping Xin">
//     Copyright (c) Yaping Xin. All rights reserved.
// </copyright>
// <Description>Demo.</Description>
//---------------------------------------------------------
namespace Demo100816A01
{
    using System;
    using System.Text;

    /// <summary>
    /// Application entry class.
    /// </summary>
    internal class Program
    {
       /// <summary>Const definition of how many number as a loop</summary>
       private const int LoopCount = 3;
       
       /// <summary>
       /// Application entry point.
       /// </summary>
       /// <param name="args">Command line parameters</param>
       internal static void Main(string[] args)
       {
           Do(new int[] { 1, 2, 3, 4, 5 });
           Do("12345");
           Do("abcde");
       }

       /// <summary>
       /// Rearrange the int array and print out result
       /// </summary>
       /// <param name="src">the int array</param>
       private static void Do(string src)
       {
           string seprator = "";
           Do<char>(src.ToCharArray(), seprator);
       }

       /// <summary>
       /// Rearrange the int array and print out result
       /// </summary>
       /// <param name="src">the int array</param>
       private static void Do(int[] src)
       {
           string seprator = ", ";
           Do<int>(src, seprator);
       }

       /// <summary>
       /// Rearrange the array and print out result
       /// </summary>
       /// <typeparam name="T">Element data type of the array</typeparam>
       /// <param name="src">the array to be print</param>
       /// <param name="seprator">seprator between each element in print string</param>
       private static void Do<T>(T[] src, string seprator)
       {
           int[] dimensions = RearrangeDimension(src.Length);
           string message = string.Format(
               "Source: {0}\tResult: {1}",
               PrintStringOfArray<T>(src, seprator),
               PrintStringOfRearrangedArray<T>(src, dimensions, seprator));
           System.Console.WriteLine(message);
       }
       
       /// <summary>
       /// Rearrange the dimension of the array.
       /// </summary>
       /// <param name="capacity">capacity of the array</param>
       /// <returns>Rearranged dimension</returns>
       private static int[] RearrangeDimension(int capacity)
       {
           if (capacity < 1)
           {
               throw new ArgumentException("capacity should at least be greater than 0.");
           }

           if (capacity == 1)
           {
               return new int[1] { 0 };
           }

           bool[] src = new bool[capacity];
           for (int i = 0; i < capacity; i++)
           {
               src[i] = true;
           }

           int[] dimensions = new int[capacity];
           int currentCapacity = 0;
           int position = -1;
           int count = 0;
           while (currentCapacity < capacity)
           {
               while (count < LoopCount)
               {
                   position = position < capacity - 1 ? ++position : 0;
                   if (src[position])
                   {
                       count++;
                   }
               }

               count = 0;
               src[position] = false;
               dimensions[currentCapacity++] = position;
           }

           return dimensions;
       }
       
       /// <summary>
       /// Gets the print string of array
       /// </summary>
       /// <typeparam name="T">Element data type of the array</typeparam>
       /// <param name="src">the array to be print</param>
       /// <param name="seprator">seprator between each element in print string</param>
       /// <returns>the print string</returns>
       private static string PrintStringOfArray<T>(T[] src, string seprator)
       {
           StringBuilder sb = new StringBuilder();
           for (int i = 0; i < src.Length; i++)
           {
               sb.Append(src[i].ToString());
               if (i < src.Length - 1)
               {
                   sb.Append(seprator);
               }
           }

           return string.Format("[{0}]", sb.ToString());
       }

       /// <summary>
       /// Gets the print string of rearranged array
       /// </summary>
       /// <typeparam name="T">Element data type of the array</typeparam>
       /// <param name="src">the array to be rearranged</param>
       /// <param name="dimensions">rearranged dimensions</param>
       /// <param name="seprator">seprator between each element in print string</param>
       /// <returns>the print string</returns>
       private static string PrintStringOfRearrangedArray<T>(T[] src, int[] dimensions, string seprator)
       {
           StringBuilder sb = new StringBuilder();
           int position = 0;
           foreach (int dimension in dimensions)
           {
               sb.Append(src[dimension].ToString());
               position++;
               if (position < src.Length)
               {
                   sb.Append(seprator);
               }
           }

           return string.Format("[{0}]", sb.ToString());
       }
    }
}
[/CODE]

运行结果:

[IMG=0,absmiddle]http://xinsoft.org/pub/dailyreading/2010/0816/result-Demo100816A01.jpg[/IMG]
2楼
源程序下载:[URL=http://xinsoft.org/pub/dailyreading/2010/0816/Demo100816A01.zip]http://xinsoft.org/pub/dailyreading/2010/0816/Demo100816A01.zip[/URL]
3楼
说明:

1. 除了RearrangeDimension()这个函数外,其余的代码全都是结果输出。
2. 为了适应各种类型的数组,我应用了范型编程,所以代码稍微长了一点。

但是真正涉及到的算法只有下面这个函数RearrangeDimension(),不到三十行代码。你搞清楚这个函数,整个程序基本上就明白了。


[QUOTE]        /// <summary>
       /// Rearrange the dimension of the array.
       /// </summary>
       /// <param name="capacity">capacity of the array</param>
       /// <returns>Rearranged dimension</returns>
       private static int[] RearrangeDimension(int capacity)
       {
           if (capacity < 1)
           {
               throw new ArgumentException("capacity should at least be greater than 0.");
           }

           if (capacity == 1)
           {
               return new int[1] { 0 };
           }

           bool[] src = new bool[capacity];
           for (int i = 0; i < capacity; i++)
           {
               src[i] = true;
           [/i]}

           int[] dimensions = new int[capacity];
           int currentCapacity = 0;
           int position = -1;
           int count = 0;
           while (currentCapacity < capacity)
           {
               while (count < LoopCount)
               {
                   position = position < capacity - 1 ? ++position : 0;
                   if (src[position])
                   {
                       count++;
                   }
               }

               count = 0;
               src[position] = false;
               dimensions[currentCapacity++] = position;
           }

           return dimensions;
       }[/QUOTE][i]
[/i]
4楼
[QUOTE][b]下面引用由[u]ymjj[/u]发表的内容:[/b]

[P][b]我不是很理解字符串提取方面的知识[/b][/P][P][SIZE=14px][/SIZE][/P][P][/P][P][SIZE=14px][SIZE=3][FACE=Times New Roman]有五个数字[/SIZE][FACE=Times New Roman](1,2,3,4,5),[/FACE][FACE=Times New Roman]从[/FACE][FACE=Times New Roman]1[/FACE][FACE=Times New Roman]开始数起,数到第三个数字就把那个数字输出来[/FACE][FACE=Times New Roman].[/FACE][FACE=Times New Roman]输出结果是[/FACE][FACE=Times New Roman]3 1  5  2  4[/FACE][/SIZE][/FACE][/P][P][SIZE=14px] [/SIZE][/P][P][SIZE=14px][FACE=Times New Roman][SIZE=3]这种题目应该怎么写啊,那些提取字符串的语法 我就是套用不上来[/SIZE][/FACE][/SIZE][/P][SIZE=14px][/SIZE][P][/P][P][/P][/QUOTE]


再回过头看看你所问的问题,很奇怪你看到这个题目想到的居然是“提取字符串”,这说明你根本就没有对这个问题进行正确的抽象。你可能根本就没有考虑用什么样的数据结构来描述你的问题。假如题目不是数字,而是别的什么数据类型,请问你还能够用“提取字符串”来做吗?


如果我把这个问题稍微变换一下:

一个家庭里有5个成员站成一队,分别为:
爸爸王一、妈妈赵二、哥哥王三、妹妹王四、小猫咪咪

现在要你从第一个成员数起,数到三就站出来组成一个新的队列,最后新的队列是什么样的?

请问这回你还能用“字符串提取”来做吗?

现在请你再回过头看看我的程序,所采用的算法是针对任何数据类型的,无所谓你的数据类型是字符串、整数数组还是别的什么对象的数组。

现在我把上面的例子稍微改一下,来输出这样的一个队列。
完整的源代码下载:http://xinsoft.org/pub/dailyreading/2010/0816/Demo100816A02.zip

首先我增加一个叫做“Person”的对象定义:

[CODE]//---------------------------------------------------------
// <copyright file="Person.cs" company="Yaping Xin">
//     Copyright (c) Yaping Xin. All rights reserved.
// </copyright>
// <Description>Class definition of Person.</Description>
//---------------------------------------------------------
namespace Demo100816A01
{
    /// <summary>
    /// Class definition of Person
    /// </summary>
    public class Person
    {
       /// <summary>
       /// The person's name
       /// </summary>
       private string name = string.Empty;

       /// <summary>
       /// Initializes a new instance of the Person class
       /// </summary>
       /// <param name="name">The person's name</param>
       public Person(string name)
       {
           this.name = name;
       }

       /// <summary>
       /// Override the ToString method
       /// </summary>
       /// <returns>the result of ToString</returns>
       public override string ToString()
       {
           return this.name;
       }
    }
}
[/CODE]

然后为了方便我在Program类中增加这样一个函数:
(其实也可以不增加,直接用 private static void Do<T>(T[] src, string seprator))
[CODE]
       /// <summary>
       /// Rearrange the Person array and print out result
       /// </summary>
       /// <param name="src">the int array</param>
       private static void Do(Person[] src)
       {
           string seprator = ", ";
           Do<Person>(src, seprator);
       }
[/CODE]

然后我把在Program类的Main()改成这样的:

[CODE]
       /// <summary>
       /// Application entry point.
       /// </summary>
       /// <param name="args">Command line parameters</param>
       internal static void Main(string[] args)
       {
           Person[] family = new Person[5];
           family[0] = new Person("爸爸王一");
           family[1] = new Person("妈妈赵二");
           family[2] = new Person("哥哥王三");
           family[3] = new Person("妹妹王四");
           family[4] = new Person("小猫咪咪");

           Do(family);
       }
[/CODE]

完整的源代码下载:http://xinsoft.org/pub/dailyreading/2010/0816/Demo100816A02.zip

程序运行的结果如下:

[IMG=0,absmiddle]http://xinsoft.org/pub/dailyreading/2010/0816/result-Demo100816A02.jpg[/IMG]
5楼
[P] 打酱油[/P]

电脑版 Page created in 0.0234 seconds with 2 queries.