Skip to main content
 首页 » 编程设计

c#之文字轰炸

2024年02月05日12thcjp

这是我正在尝试编写的程序:

Write a program that reads a text and line width from the console. The program should distribute the text so that it fits in a table with a specific line width. Each cell should contain only 1 character. It should then read a line with numbers, holding the columns that should be bombarded.

我的代码是这样的:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
 
namespace _04.Text_Bombardment 
{ 
    class Program 
    { 
        static void Main(string[] args) 
        { 
            var sentence = Console.ReadLine(); 
            var bombing = int.Parse(Console.ReadLine()); 
            var selected = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); 
            Dictionary<int, bool> bombDict = new Dictionary<int, bool>(); 
            var newSentence = sentence + new string(' ', bombing - sentence.Length % bombing); // whole row minus words left 
 
            for (int i = 0; i < selected.Length; i++) 
            { 
                bombDict.Add(selected[i], true); 
            } 
            var rows = newSentence.Length / bombing; 
            var cols = bombing; 
 
            var count = 0; 
 
            var arrSent = newSentence.ToCharArray(); 
            for (int i = 0; i < rows; i++) 
            { 
                for (int j = 0; j < cols; j++) 
                { 
                    if (bombDict.ContainsKey(j) && bombDict[j] == true && arrSent[count] != ' ') 
                    { 
                        arrSent[count] = ' '; 
                        try 
                        { 
                            if (arrSent[count + bombing] == ' ') 
                            { 
                                bombDict[j] = false; 
                            } 
                            else 
                            { 
                                bombDict[j] = true; 
                            } 
 
                        } 
                        catch (IndexOutOfRangeException) 
                        { 
                            continue; 
                        } 
                   } 
                   count++; 
                } 
            } 
            var finalSent = string.Join("", arrSent).TrimEnd(); 
            Console.WriteLine(finalSent); 
       } 
   } 
} 

它在这句话上中断了:

Vazov received his elementary education in hisnative town of Sopoandat Plovdiv. The son of a conservative, well-to-do merchant. 
20 
1 6 17 2 5 0 15 

当前输出:

ov  eceived  i  e   en  ry educa i n    hi  ative to n of  opo  dat Plov iv. T e s   of a co serva ive  well-to- o mer ha t. 

预期输出:

ov  eceived  i  e   en  ry educa i n    hi  ative to n of  opo  dat Plov iv. T e s   of a co serva ive  well-to- o mer han .  

所以它只在最后不起作用。 有人能帮我吗? 有什么建议吗?


补充说明:

例如,我们阅读文本“Well this problem is gonna be a ride.”和线宽 10。我们将文本分布在 4 行 10 列中。我们读取数字“1 3 7 9”并在表中的这些列上投下炸弹。 炸弹摧毁了它们落在的角色及其下方的所有相邻角色。
注意:被摧毁角色下方的空白区域可以阻止炸弹(见第 7 栏)。 最后,我们在控制台上打印轰炸文本:“W l th s o lem i o na be a r de。” 注意:不应打印表格中文本后的空单元格。

请您参考如下方法:

您的解决方案很难理解,请保持简单,为您可以轻松理解的变量命名。

我修改了你的代码,希望对你有帮助:

static void Main() 
{ 
    string sentence = "Well this problem is gonna be a ride."; 
    int numberOfColumns = int.Parse("10"); 
    List<int> bombs = "1 3 7 9".Split(' ').Select(int.Parse).ToList(); 
 
 
    // we need to convert to decimal, otherwise C# will ignore decimal part.  
    //example: 127/20 = 6.35, so we need 7 rows. if we don't convert to decimal we have 6 
    // the Ceiling says, always round up. so even 6.1 will be rounded to 7 
    int numberOfRows = (int)Math.Ceiling(sentence.Length / Convert.ToDecimal(numberOfColumns)); 
 
    char[,] array = new char[numberOfRows, numberOfColumns]; 
 
    int sentencePointer = 0; 
    for (int rowIndex = 0; rowIndex < numberOfRows; rowIndex++) 
    { 
        for (int colIndex = 0; colIndex < numberOfColumns; colIndex++) 
        { 
            // if you want to print the grid with the full text, just comment the 3 lines below, 
            //and keep only "array[rowIndex, colIndex] = sentence[sentencePointer];" 
            if (bombs.Contains(colIndex)) 
            { 
                if (sentence[sentencePointer] == ' ') // bomb is deactivated 
                { 
                    bombs.Remove(colIndex); 
                    array[rowIndex, colIndex] = sentence[sentencePointer]; 
                } 
                else 
                    array[rowIndex, colIndex] = '*'; // * represents a bomb 
            } 
            else 
                array[rowIndex, colIndex] = sentence[sentencePointer]; 
 
            sentencePointer++; // move next character 
            if (sentencePointer >= sentence.Length) 
                break; // we reach the end of the sentence. 
        } 
    } 
 
    PrintGrid(array, numberOfRows, numberOfColumns); 
 
    // just give some space to print the final sentence 
    Console.WriteLine(""); 
    Console.WriteLine(""); 
    Console.WriteLine(""); 
 
    for (int rowIndex = 0; rowIndex < numberOfRows; rowIndex++) 
    { 
        for (int colIndex = 0; colIndex < numberOfColumns; colIndex++) 
        { 
            Console.Write(array[rowIndex, colIndex]); 
        } 
    } 
 
    Console.ReadKey(); 
} 
 
private static void PrintGrid(char[,] array, int numberOfRows, int numberOfColumns) 
{ 
    Console.WriteLine(new string('-', numberOfColumns * 2)); 
 
    for (int rowIndex = 0; rowIndex < numberOfRows; rowIndex++) 
    { 
        Console.Write("|"); 
        for (int colIndex = 0; colIndex < numberOfColumns; colIndex++) 
        { 
            Console.Write(array[rowIndex, colIndex]); 
            Console.Write("|"); 
        } 
        Console.WriteLine(""); 
    } 
}