【问题标题】:Undesired Output不需要的输出
【发布时间】:2014-02-11 06:16:03
【问题描述】:
#include <iostream>
#include <cstring> 
using namespace std;

int main() {
    std::string a, b;
    int number_cases=0,i,j,count=0;
    cin>>number_cases;
    while(number_cases!=0)
        {
            cin>>a;
            cin>>b;
            for(i=0;i<a.size();i++) {
                for(j=0;j<b.size();j++) {
                    if(a[i]==b[j]); {
                        count++;
                        b[j]='#';
                        break;
                    }
                }
            }
            cout<<count<<endl;
            count=0;
            --number_cases;
        }
}

http://www.codechef.com/FEB14/problems/LCPESY 我在提交时遇到 TLE 错误,建议一些优化输出的方法。

【问题讨论】:

  • 请不要从您的代码中删除错误,如果它们在下面的答案中进行了讨论。这会让其他人更难从你的错误中吸取教训。
  • 这个问题似乎离题了,因为它缺乏最基本的理解。

标签: c++ string string-length


【解决方案1】:

唯一的错误是

if(a[i]==b[j]);

删除“;”来自if语句

if(a[i]==b[j])

一切正常

【讨论】:

  • 我注意到了,修复 TLE 问题
【解决方案2】:
#include <iostream>
#include <cstring> 
using namespace std;

int main() {
    std::string a, b;

由于您使用命名空间 std 编写,因此“std::”不是必需的。

但是您的程序无法运行的原因是因为您有一个“;”在你的 if 语句中。

if(a[i]==b[j]);

应该是

if(a[i]==b[j]) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-04
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多