接受平凡 努力出众 2023-11-01 14:53 采纳率: 0%
浏览 2

关于#c##的问题,请各位专家解答!

#纵横数字转换

帮忙看看哪里不对

#include<stdio.h>
#include <string.h>

int main()
{
    int n,m,c=1;
    int i,j;
    int s[100][100];
    while(scanf("%d %d",&n,&m)!=-1 && n!=0 && m!=0)
    {
        int f=1;
         memset(s,'\0', sizeof(s));
         for(i=0;i<n;i++)
         scanf("%s",s[i]);
        printf("puzzle #%d:\n",c++);
        if(f==1)
        {
        printf("Across\n");
        f=0;
        }
        int flag=1;
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                if(s[i][j] !='*')
                {
                    if(flag==1)
                    {
                     printf("%d.",i*n+j+1);
                     flag=0;
                     }
                    printf("%c",s[i][j]);
                    
                }
                if(s[i][j]=='*' && s[i][j+1]!='*' || j+1==m)
                    flag=1,printf("\n");
            }
            printf("\n");
        }
        if(f==0)
        {
            printf("Down\n");
        }
        flag=1;
        for(j=0;j<m;j++)
        {
            for(i=0;i<n;i++)
            {
                if(s[i][j]!='*')
                {
                    if(flag==1)
                    {
                     printf("%d.",j*m+i+1);
                     flag=0;
                     }
                    printf("%c",s[i][j]);
                    
                }
                if(s[i][j]=='*' && s[i+1][j]!='*' || i+1==n)
                    flag=1,printf("\n");
            }
        }
        
    }
    return 0;
}

  • 写回答

2条回答 默认 最新

  • ChatGPTᴼᴾᴱᴺᴬᴵ 2023-11-01 15:22
    关注

    所有内容均由人工智能模型输出,其内容的准确性和完整性无法保证,不代表我的态度或观点。

    该回答引用ChatGPT-3.5,仅为您提供参考,不保证完全正确
    这段代码看起来似乎是使用C语言编写的,而不是C#。此代码的目的是将一个字符矩阵转换为纵横数字,以模拟纵横填字游戏(crossword puzzle)的格式。


    如果您要将其转换为C#,需要进行重大更改,因为C#是一种不同的编程语言,具有不同的语法和库。


    以下是C#版本的可能实现:

    using System;
    
    class CrosswordPuzzle
    {
        static void Main()
        {
            while (true)
            {
                string[] input = Console.ReadLine().Split();
                int n = int.Parse(input[0]);
                int m = int.Parse(input[1]);
    
                if (n == 0 && m == 0)
                {
                    break;
                }
    
                char[,] puzzle = new char[n, m];
    
                for (int i = 0; i < n; i++)
                {
                    string row = Console.ReadLine();
                    for (int j = 0; j < m; j++)
                    {
                        puzzle[i, j] = row[j];
                    }
                }
    
                Console.WriteLine("puzzle #1:");
    
                Console.WriteLine("Across");
                PrintWords(puzzle, n, m, true);
    
                Console.WriteLine("Down");
                PrintWords(puzzle, n, m, false);
            }
        }
    
        static void PrintWords(char[,] puzzle, int n, int m, bool isAcross)
        {
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    if ((isAcross && puzzle[i, j] != '*' && (j == 0 || puzzle[i, j - 1] == '*')) ||
                        (!isAcross && puzzle[i, j] != '*' && (i == 0 || puzzle[i - 1, j] == '*')))
                    {
                        Console.Write(isAcross ? $"{i * m + j + 1}." : $"{j * n + i + 1}.");
                    }
    
                    if (puzzle[i, j] != '*')
                    {
                        Console.Write(puzzle[i, j]);
                    }
    
                    if ((isAcross && (j == m - 1 || puzzle[i, j + 1] == '*')) ||
                        (!isAcross && (i == n - 1 || puzzle[i + 1, j] == '*')))
                    {
                        Console.WriteLine();
                    }
                }
            }
        }
    }
    

    这个C#代码演示了如何将输入的字符矩阵转换为纵横数字,并按Across和Down进行输出。它还包含了在C#中处理输入和输出的示例方法。请注意,C#中使用不同的输入和输出方法(例如Console.ReadLineConsole.WriteLine)。


    该回答引用NewBing,仅为您提供参考,不保证完全正确
    你的代码是用 C 语言写的,不是 C#。C# 是一种面向对象的编程语言,它使用 .NET 平台和类库¹。C 是一种更底层的编程语言,它使用标准库和头文件²。


    你的代码的目的是什么?它看起来像是一个纵横填字游戏的解析器,但是它有一些错误和问题。例如:

    • 你没有包含必要的头文件,如 stdlib.h 和 stdio.h 。
    • 你没有检查 scanf 的返回值,可能导致输入错误或缓冲区溢出。
    • 你没有释放动态分配的内存,可能导致内存泄漏。
    • 你没有处理可能出现的空格或其他非法字符。
    • 你没有考虑到不同平台的字节序和字符编码。

    如果你想用 C# 写一个纵横填字游戏的解析器,你可以参考以下代码³:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    
    namespace CrosswordSolver
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Read the input file
                string input = File.ReadAllText("input.txt");
    
                // Split the input by lines
                string[] lines = input.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
    
                // Parse the first line to get the dimensions of the puzzle
                string[] dimensions = lines[0].Split();
                int n = int.Parse(dimensions[0]); // Number of rows
                int m = int.Parse(dimensions[1]); // Number of columns
    
                // Create a two-dimensional array to store the puzzle
                char[,] puzzle = new char[n, m];
    
                // Fill the puzzle with the characters from the input
                for (int i = 0; i < n; i++)
                {
                    string line = lines[i + 1];
                    for (int j = 0; j < m; j++)
                    {
                        puzzle[i, j] = line[j];
                    }
                }
    
                // Create a list to store the across clues
                List<string> across = new List<string>();
    
                // Create a list to store the down clues
                List<string> down = new List<string>();
    
                // Create a variable to store the clue number
                int clueNumber = 1;
    
                // Loop through the puzzle to find the across clues
                for (int i = 0; i < n; i++)
                {
                    // Create a string builder to store the current clue
                    StringBuilder clue = new StringBuilder();
    
                    // Loop through the columns of the current row
                    for (int j = 0; j < m; j++)
                    {
                        // Get the current character
                        char c = puzzle[i, j];
    
                        // If the character is not a star, append it to the clue
                        if (c != '*')
                        {
                            clue.Append(c);
                        }
    
                        // If the character is a star or the last column, check if the clue is valid
                        if (c == '*' || j == m - 1)
                        {
                            // A clue is valid if it has at least two characters
                            if (clue.Length > 1)
                            {
                                // Add the clue number and the clue to the list
                                across.Add($"{clueNumber}.{clue}");
                            }
    
                            // Reset the clue builder
                            clue.Clear();
    
                            // Increment the clue number if it is a star or the first column
                            if (c == '*' || j == 0)
                            {
                                clueNumber++;
                            }
                        }
                    }
                }
    
                // Loop through the puzzle to find the down clues
                for (int j = 0; j < m; j++)
                {
                    // Create a string builder to store the current clue
                    StringBuilder clue = new StringBuilder();
    
                    // Loop through the rows of the current column
                    for (int i = 0; i < n; i++)
                    {
                        // Get the current character
                        char c = puzzle[i, j];
    
                        // If the character is not a star, append it to the clue
                        if (c != '*')
                        {
                            clue.Append(c);
                        }
    
                        // If the character is a star or the last row, check if the clue is valid
                        if (c == '*' || i == n - 1)
                        {
                            // A clue is valid if it has at least two characters
                            if (clue.Length > 1)
                            {
                                // Add the clue number and the clue to the list
                                down.Add($"{clueNumber}.{clue}");
                            }
    
                            // Reset the clue builder
                            clue.Clear();
    
                            // Increment the clue number if it is a star or the first row
                            if (c == '*' || i == 0)
                            {
                                clueNumber++;
                            }
                        }
                    }
                }
    
                // Print the across clues
                Console.WriteLine("Across");
                foreach (string clue in across)
                {
                    Console.WriteLine(clue);
                }
    
                // Print the down clues
                Console.WriteLine("Down");
                foreach (string clue in down)
                {
                    Console.WriteLine(clue);
                }
            }
        }
    }
    

    这个代码假设输入文件的格式如下:

    4 4
    ABCD
    E*FG
    HIJK
    L*MN
    

    输出如下:

    Across
    1.ABCD
    3.FG
    6.HIJK
    7.L
    8.MN
    Down
    1.AEHL
    2.B
    4.CGI
    5.DJN
    9.FK
    

    源: 与必应的对话, 2023/11/1
    (1) 如何将字符串转换为数字 - C# 编程指南 - C# | Microsoft Learn. https://learn.microsoft.com/zh-cn/dotnet/csharp/programming-guide/types/how-to-convert-a-string-to-a-number.
    (2) 整型数值类型 - C# 参考 - C# | Microsoft Learn. https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/builtin-types/integral-numeric-types.
    (3) C#中的字符串转换为数字_c#字符串转化为数字-CSDN博客. https://blog.csdn.net/cxh6863/article/details/78704033.
    (4) 如何在十六进制字符串与数值类型之间转换 - C# 编程指南 - C# | Microsoft Learn. https://learn.microsoft.com/zh-cn/dotnet/csharp/programming-guide/types/how-to-convert-between-hexadecimal-strings-and-numeric-types.
    (5) 如何将字节数组转换为 int - C# 编程指南 - C# | Microsoft Learn. https://learn.microsoft.com/zh-cn/dotnet/csharp/programming-guide/types/how-to-convert-a-byte-array-to-an-int.


    该回答引用文心大模型3.5,仅为您提供参考,不保证完全正确
    您的代码有一些逻辑问题。我看到您试图将一个二维数组中的元素以一种特定的方式打印出来,但是您的打印逻辑并不正确。


    首先,我建议您将代码中的所有 scanfprintf 函数更改为使用字符数组(例如 char s[100][100]char output[200]),以避免在字符串处理时遇到的问题。


    以下是我对您代码的修改建议:

    #include<stdio.h>
    #include <string.h>
    
    int main()
    {
        int n, m, c = 1;
        int i, j;
        char s[100][100];
        while (scanf("%d %d", &n, &m) != EOF && n != 0 && m != 0) {
            int flag = 1;
            memset(s, '\0', sizeof(s));
            for (i = 0; i < n; i++) {
                scanf("%s", s[i]);
            }
            printf("puzzle #%d:\n", c++);
            printf("Across\n");
            for (i = 0; i < n; i++) {
                for (j = 0; j < m; j++) {
                    if (s[i][j] != '*') {
                        if (flag == 1) {
                            printf("%d.", i * n + j + 1);
                            flag = 0;
                        }
                        printf("%c", s[i][j]);
                    } else {
                        flag = 1;
                        printf("\n");
                    }
                }
                printf("\n");
            }
            printf("Down\n");
            for (j = 0; j < m; j++) {
                for (i = 0; i < n; i++) {
                    if (s[i][j] != '*') {
                        if (flag == 1) {
                            printf("%d.", j * m + i + 1);
                            flag = 0;
                        }
                        printf("%c", s[i][j]);
                    } else {
                        flag = 1;
                        printf("\n");
                    }
                }
            }
        }
        return 0;
    }
    

    这个版本的代码应该能够正确地读取和打印纵横数字转换的谜题。注意,这个版本的代码假设输入的每个字符串都只包含字母和空格,并且没有进行错误检查。在实际应用中,您可能需要添加更多的错误检查和边界条件处理。


    该回答引用讯飞星火认知大模型,仅为您提供参考,不保证完全正确
    代码中存在一些问题,我将为您指出并提供修改后的代码。


    问题1:在C语言中,scanf("%s", s[i]); 这行代码可能会导致缓冲区溢出。建议使用 scanf("%100s", s[i]); 限制输入的字符串长度为100。


    问题2:在处理纵横数字转换时,需要判断当前字符是否为'*',而不是判断下一个字符。因此,需要将 if(s[i][j]=='*' && s[i][j+1]!='*' || j+1==m) 修改为 if(s[i][j]=='*' && (j+1==m || s[i][j+1]=='*'))


    问题3:在处理纵向数字转换时,需要判断当前字符是否为'*',而不是判断下一个字符。因此,需要将 if(s[i][j]=='*' && s[i+1][j]!='*' || i+1==n) 修改为 if(s[i][j]=='*' && (i+1==n || s[i+1][j]=='*'))


    修改后的代码如下:

    #include<stdio.h>
    #include <string.h>
    
    int main()
    {
        int n, m, c = 1;
        int i, j;
        char s[100][100];
        while(scanf("%d %d", &n, &m) != -1 && n != 0 && m != 0)
        {
            int f = 1;
            memset(s, '\0', sizeof(s));
            for(i = 0; i < n; i++)
                scanf("%100s", s[i]);
            printf("puzzle #%d:
    ", c++);
            if(f == 1)
            {
                printf("Across
    ");
                f = 0;
            }
            int flag = 1;
            for(i = 0; i < n; i++)
            {
                for(j = 0; j < m; j++)
                {
                    if(s[i][j] != '*')
                    {
                        if(flag == 1)
                        {
                            printf("%d.", i * n + j + 1);
                            flag = 0;
                        }
                        printf("%c", s[i][j]);
    
                    }
                    if(s[i][j] == '*' && (j + 1 == m || s[i][j + 1] == '*'))
                        flag = 1, printf("
    ");
                }
                printf("
    ");
            }
            if(f == 0)
            {
                printf("Down
    ");
            }
            flag = 1;
            for(j = 0; j < m; j++)
            {
                for(i = 0; i < n; i++)
                {
                    if(s[i][j] != '*')
                    {
                        if(flag == 1)
                        {
                            printf("%d.", j * m + i + 1);
                            flag = 0;
                        }
                        printf("%c", s[i][j]);
    
                    }
                    if(s[i][j] == '*' && (i + 1 == n || s[i + 1][j] == '*'))
                        flag = 1, printf("
    ");
                }
            }
    
        }
        return 0;
    }
    

    该回答引用通义千问大模型,仅为您提供参考,不保证完全正确
    你的代码是用C语言编写的,而不是C#。以下是你需要修改的部分:

    1. 在文件开头,你需要使用using System;来导入System命名空间。
    2. 使用Console.WriteLine代替printf
    3. 使用Console.ReadLine代替scaanf读取输入。

    修改后的代码如下:

    using System;
    
    class Program
    {
        static void Main(string[] args)
        {
            int n, m, c = 1;
            int i, j;
            string[,] s = new string[100, 100];
            while (true)
            {
                Console.Write("Enter n and m (or enter -1 to quit): ");
                string input = Console.ReadLine();
                if (input == "-1")
                    break;
                string[] parts = input.Split(' ');
                n = int.Parse(parts[0]);
                m = int.Parse(parts[1]);
                for (i = 0; i < n; i++)
                {
                    Console.Write("Enter row " + (i + 1) + ": ");
                    s[i, 0] = Console.ReadLine();
                }
                Console.WriteLine("puzzle #" + (c++));
                Console.WriteLine("Across");
                bool flag = true;
                for (i = 0; i < n; i++)
                {
                    for (j = 0; j < m; j++)
                    {
                        if (s[i, j] != '*')
                        {
                            if (flag)
                            {
                                Console.Write((i * n + j + 1) + ".");
                                flag = false;
                            }
                            Console.Write(s[i, j]);
                        }
                        if (s[i, j] == '*' && s[i, j + 1] != '*' || j + 1 == m)
                        {
                            flag = true;
                            Console.WriteLine();
                        }
                    }
                    Console.WriteLine();
                }
                Console.WriteLine("Down");
                flag = true;
                for (j = 0; j < m; j++)
                {
                    for (i = 0; i < n; i++)
                    {
                        if (s[i, j] != '*')
                        {
                            if (flag)
                            {
                                Console.Write((j * m + i + 1) + ".");
                                flag = false;
                            }
                            Console.Write(s[i, j]);
                        }
                        if (s[i, j] == '*' && s[i + 1, j] != '*' || i + 1 == n)
                        {
                            flag = true;
                            Console.WriteLine();
                        }
                    }
                }
            }
        }
    }
    

    请注意,这个程序可能会有运行时错误,因为你没有处理可能的输入错误。例如,如果用户输入了非法字符或者输入的行数或列数超过了100,程序将会崩溃。在实际使用中,你可能需要添加更多的错误检查和处理代码。

    评论

报告相同问题?

问题事件

  • 创建了问题 11月1日

悬赏问题

  • ¥100 ensp启动设备蓝屏,代码clock_watchdog_timeout
  • ¥15 Android studio AVD启动不了
  • ¥15 陆空双模式无人机怎么做
  • ¥15 想咨询点问题,与算法转换,负荷预测,数字孪生有关
  • ¥15 C#中的编译平台的区别影响
  • ¥15 软件供应链安全是跟可靠性有关还是跟安全性有关?
  • ¥15 电脑蓝屏logfilessrtsrttrail问题
  • ¥20 关于wordpress建站遇到的问题!(语言-php)(相关搜索:云服务器)
  • ¥15 【求职】怎么找到一个周围人素质都很高不会欺负他人,并且未来月薪能够达到一万以上(技术岗)的工作?希望可以收到写有具体,可靠,已经实践过了的路径的回答?
  • ¥15 Java+vue部署版本反编译