我真的不知道如何进一步提高这一点 [英] I seriously donno HOW to IMPROVE THIS FURTHER

查看:138
本文介绍了我真的不知道如何进一步提高这一点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比赛页面| CodeChef [ ^ ]



我的尝试:



Contest Page | CodeChef[^]

What I have tried:

#include<stdio.h>
int main()
{
int c2[10000],c1[10000];
int i,k,j,T;
char s[20][100000];
scanf("%d",&T);
for(i=0;i<T;i++)
{c1[i]=0;c2[i]=0;
}
for(i=0;i<T;i++)
{
  scanf("%s",s[i]);
}
for(i=0;;i++)
{ 
  for(j=0;s[i][j]!='\0';j++)
   { 
     if(s[i][j]=='A')
       c1[i]++;
     else if(s[i][j]=='B')
       c2[i]++;
     else if(s[i][j]=='.')
       {
          for(k=j;;k++)
          {
            if(s[i][k]=='A')
              {
                c1[i]=c1[i]+(k-j);
                break;
              }  
            else if(s[i][k]=='B')
              {
                c2[i]=c2[i]+(k-j);
                break;
              }
            
          }
       }
   }     

  if(i==T-1)
   break;
}
for(i=0;i<T;i++)
{
printf("%d %d",c1[i],c2[i]);
}
}

推荐答案

首先,我看到代码中存在缺陷或错误。如果T大于20,则程序崩溃。或者检查T< 20.(如果需要比使用malloc和免费工作更大的T)



所以你应该通过一些输出更好地改善用户交互(请输入起始值小于20)以及结果是什么。



我建议在所有for循环中的分解标准,如(i< T-1)主循环,也为一些变量使用更好的名称,如T。
At first I see a weakness or a bug in your code. If T gets greater than 20 than your program crashes. Or check T < 20. (If need greater T than than work with malloc and free)

So you should make the user interaction better by some output what is asked ("Please input the start value less than 20") and what the results are.

I would recommand for a breakup criteria in all for loops like (i<T-1) in the main loop and also use better names for some variables like T.


引用:

我认真地不知道如何进一步提高

I seriously donno HOW to IMPROVE THIS FURTHER



改进是什么意思?

据我所知,代码不起作用。

这是一个挑战,你的任务是制作正确的代码。



你可以减少足迹:

按照约束:1≤T≤20,所以


What fo you mean by 'improve' ?
As far as I understand this, the code don't work.
The is a challenge, your task is to make correct code.

You can reduce foot print:
As per constraint: 1 ≤ T ≤ 20, so

int c2[10000],c1[10000];



可以替换为


can be replaced with

int c2[20],c1[20];



此外,您不需要存储所有村庄模式,只需一个:


and further, you don't need to store all villages patterns, just one:

#include<stdio.h>
int main()
{
  int c2[20],c1[20];
  int i,k,j,T;
  char s[100000];
  scanf("%d",&T);
  for(i=0;i<T;i++)
  {
    c1[i]=0;c2[i]=0;
    
    scanf("%s",s);
    
    for(j=0;s[j]!='\0';j++)
    {
      if(s[j]=='A')
        c1[i]++;
      else if(s[j]=='B')
        c2[i]++;
      else if(s[j]=='.')
      {
        for(k=j;;k++)
        {
          if(s[k]=='A')
          {
            c1[i]=c1[i]+(k-j);
            break;
          }
          else if(s[k]=='B')
          {
            c2[i]=c2[i]+(k-j);
            break;
          }
        }
      }
    }
  }
  for(i=0;i<T;i++)
  {
    printf("%d %d",c1[i],c2[i]);
  }
}





您的代码应该在'..A ..'和'..B上崩溃..B..B ..'并在其他人身上给出错误的结果。

我认为完全重写是有序的。调试器将帮助您了解代码中的错误。



有一个工具可以让您查看代码正在做什么,其名称是调试的。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与什么进行比较应该这样做。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。



Your code should crash on '..A..' and '..B..B..B..' and give wrong results on the others.
I think a complete rewrite is in order. The debugger will help you to understand what is wrong in your code.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于我真的不知道如何进一步提高这一点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆