为什么它显示错误的答案? [英] Why is it showing wrong answer?

查看:66
本文介绍了为什么它显示错误的答案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SPOJ.com - 问题SMPSEQ3 [ ^ ]



我尝试过:



SPOJ.com - Problem SMPSEQ3[^]

What I have tried:

#include <iostream>
using namespace std;

int main() {
	int n,m,a[100],b[100],i,j ;
	cin>>n;
	for(i=0;i<n;i++)
	cin>>a[i];
	cin>>m;
	for(j=0;j<n;j++)
	cin>>b[j];
	for(i=0;i<n;i++)
	{
	    for(j=0;j<m;j++)
	    {
	        if(a[i]==b[j])
	        break;
	        else if(a[i]!=b[j]&& j==m-1)
	        {
	            cout<<a[i]<<" ";
	        }
	    }
	}
	return 0;
}

推荐答案

你在内循环中写了错误的限制。

You have written the wrong limit in the inner loop.
for(j=0;j<n;j++)
	cin>>b[j];
	for(i=0;i<n;i++)//<<=== OOOOOPS! Should be 'm'
	{





尝试



Try

#include <iostream>
using namespace std;

int main()
{
  int n,m,a[100],b[100],i,j ;
  cin>>n;
  for(i=0;i<n;i++)
    cin>>a[i];
  cin>>m;
  for(j=0;j<m;j++)
    cin>>b[j];
  for(i=0;i<n;i++)
  {
    for(j=0;j<m;j++)
    {
      if(a[i]==b[j])
        break;
    }
    if (j == m)
      cout << a[i] << " ";
  }
  cout << endl;
  return 0;
}









or

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
  size_t size;

  vector<int> v, w;

  cin >> size;
  v.resize(size);

  for (auto & x: v)
    cin >> x;

  cin >> size;
  w.resize(size);

  for (auto & x: w)
    cin >> x;


  for (auto  x: v)
  {
    auto it = find( w.begin(), w.end(), x);
    if ( it == w.end())
      cout << x << " ";
  }
  cout << endl;
}


尝试替换

Try to replace
for(j=0;j<n;j++)



with the br />


with

for(j=0;j<m;j++)



您的代码行为不符合预期,或者您不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里向你展示你的代码在做什么和你的任务是与应该做的事情进行比较。

调试器中没有魔法,它不知道你的代码应该做什么,它没有找到bug,它只是帮助你向你展示发生了什么。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

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



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

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

1.11 - 调试程序(步进和断点)|学习C ++ [ ^ ]

调试器仅显示您的代码正在执行的操作,并且您的任务是与应该执行的操作进行比较。


Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
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 know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
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[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于为什么它显示错误的答案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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