【问题标题】:I am Getting Presentation Error in Online Program Challenge Compiler我在在线程序挑战编译器中遇到演示错误
【发布时间】:2014-03-30 19:22:20
【问题描述】:
#include <stdio.h>
#include <string.h>

int main() {
    int num, i, j, result, index;
    char name[100][10]; 
    char temp[10];                 
    scanf("%d\n", &num);
    for(i = 0; i < num; i++)
        scanf("%s\n", name[i]);
    for(i = 0; i < num; i++) { 
        index = i;
        for(j = i + 1; j < num; j++) {
            result = strcmp(name[index], name[j]);
            if(result > 0)                               
                index = j;
        }
        strcpy(temp, name[index]);
        strcpy(name[index], name[i]);                  
        strcpy(name[i], temp);
    }

    for(i = 0; i < num; i++) {
        printf("%s", name[i]);
        printf("\n");
    }
}

给你一组小写英文字母的单词。它们将按字母顺序排序。

字母顺序:它是单词在字典或电话簿中的组织顺序。

例如,

  • abc 位于 xab 之前。原因:第一个字母不同。因此,它们进行了比较。在英文字母中,a 在 x 之前。
  • aab 在 abc 之前。原因:第一个字母相同(aab abc)。因此,将比较接下来的 2 个字母 (aab abc)。
  • aab 在 aaba 之前。原因:aab 中的前 3 个字母与 aaba 中的前 3 个字母相同,并且 aab 中没有更多的字母可以与 aaba 进行比较。因此,长度较小的单词首先出现(aab)。

输入 - 第一行有“n”,即单词的数量,然后是 n 行,每行包含一个单词。

输出 - 按字母顺序排列的单词,每行一个。

约束:0

提示:了解二维数组如何提供帮助。

此程序恢复在线挑战中的演示错误

示例输入

2
world
hello

样本输出

hello
world

【问题讨论】:

  • “此程序还原演示文稿错误”是什么意思?您至少可以将printf("%s",name[i]); printf("\n"); 组合成printf("%s\n", name[i]);,但这是琐事。
  • nametemp 应该是 name[100][11]temp[11]。您忘记了结尾字符 '\0' 作为旁注
  • Reverts 表示我收到演示错误,但输出正确
  • scanf("%d\n", &amp;num); --> scanf("%d", &amp;num); , scanf("%s\n", name[i]); --> scanf("%s", name[i]);

标签: c


【解决方案1】:

Presentation error 仅表示您提交 o/p 的方式与机器人裁判的数据库不匹配。
只需通过在您的预期答案中犯一些错误来找出实际的o / p shold如何呈现,然后您就可以纠正它。

【讨论】:

  • I case 出现错误,因为我们的输出是正确的,但与机器人裁判的数据库不匹配。我会为此提交获得积分吗,因为在我的情况下,它为给定的测试用例提供了正确的输出,但在提交时给出了“演示错误”。
【解决方案2】:

更喜欢动态分配。因为,如果用户需要放超过 100 个字或者一个字超过 10 个字符,他就不能放。

提示:man 2 malloc

【讨论】:

    【解决方案3】:
    #include <stdio.h>
    #include <string.h>
    
    int main(){
    int num, i, j, result, index;
    
    char name[100][11]; 
    
    char temp[11];   
    
    scanf("%d", &num);
    
    for(i = 0; i < num; i++)
    
    scanf("%s", name[i]);
    
    for(i = 0; i < num; i++) 
    
    { 
    
    
    index = i;
    
    
    for(j = i + 1; j < num; j++)
    
    {
    
    
    result = strcmp(name[index], name[j]);
    
    
    if(result > 0)                               
    
    
    index = j;
    
    }
    
    
    strcpy(temp, name[index]);
    
    strcpy(name[index], name[i]);                  
    
    strcpy(name[i], temp);
    
    }
    
    enter code here
    
    for(i = 0; i < num-1; i++) 
    
    {
    
    
    printf("%s\n", name[i]);
    
    }
    
    printf("%s", name[i]);
    
    return 0;
    
    }
    

    【讨论】:

      猜你喜欢
      • 2021-11-13
      • 1970-01-01
      • 1970-01-01
      • 2021-03-11
      • 2015-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-24
      相关资源
      最近更新 更多