对于某些不同的测试用例,结果将成为错误的答案。 [英] Result is coming out to to be a wrong answer for some different test case.

查看:71
本文介绍了对于某些不同的测试用例,结果将成为错误的答案。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

You are given a sorted sequence of n integers S = s1, s2, ..., sn and a sorted sequence of m integers Q = q1, q2, ..., qm. Please, print in the ascending order all such si that belongs to Q. 





我尝试了什么:





What I have tried:

#include <iostream>
using namespace std;

int main() {
	int n,m,i,j,a[101],b[101],ctr=0,c[100];
	cin>>n;
	for(i=0;i<n;i++)
	cin>>a[i];
	cin>>m;
	for(i=0;i<m;i++)
	cin>>b[i];
	for(i=0;i<n;i++)
	{
	    for(j=1;j<m;j++)
	    {
	        if(a[i]==b[j])
	        {
	            c[ctr]=a[i];
	            ctr++;
	        }
	    }
	}
	for(i=0;i<ctr;i++)
	{
	cout<<c[i];
	}
	return 0;
}

推荐答案

在屏幕上进行更多输出以进行所有交互和比较。你的algorhitm不是最优的,因为你的两个数组是排序的,所以不需要从索引0开始,但最后一次使用。



为了更好的可读性,我会使用S和Q为数组名称。



为什么用1开始j?编写一些为您填写数据的测试函数,并学习如何使用调试器。
Make some more output on the screen for all interactions and comparisons. Your algorhitm isnt optimal because your two arrays are sorted, so dont need to start at index 0, but the last used.

For better readability I would use the S and Q for the arrays names.

Why starts j with 1? Write some test functions which are filling the data for you and learn to use the debugger.


FOR each number in si
    FOR each number in Q
        IF the number in si equals the number in Q
        BEGIN
            PRINT the number
            BREAK from the inner loop
        END
    END FOR
END FOR


这篇关于对于某些不同的测试用例,结果将成为错误的答案。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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