我遇到了这个任务的问题 [英] I`ve got a problem with this task

查看:90
本文介绍了我遇到了这个任务的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个任务中,我们必须打印最长连续的偶数不是3的数字的长度,而不是7的数字。我们在向量中得到size_a(0< size_a< 1000)元素。每个元素(-1000


答案必须是一个整数。



 #include< iostream> 
#include< vector>使用namespace std

;

int main()
{
int size_a;
cin>> size_a;
int num = 1,num_max = 0;
vector< int> v(size_a);
for(auto i = 0; i< size_a; i ++)
cin>> v [I];
// ---------------------------------
for(auto i = 1; i< size_a; i ++)
{
if((v [i - 1]%7!= 0)&&(v [i]%7!= 0))
{
num ++;
if(num_max< num)
num_max = num;
}
else if((v [i - 1]%2 == 0)&&(v [i]%2 == 0)){
if((v [i - 1]%3!= 0)&&(v [i]%3!= 0))
{
num ++;
if(num_max< num)
num_max = num;
}
}
其他
num = 1;
}
cout<< num_max<< ENDL;
}





输入示例:



100 53 23 19 31

输出:

5

-

输入示例:

7

87 3 36 63 78 68 44

输出:

3

---- --------------

我的程序有一个错误,我找不到它 - 它适用于多个测试,但在我们的colledge的在线程序检查器中它没有通过测试7.我不知道输入示例。我小组中的几个人已经完成了这项任务,但是没有告诉我该怎么做。

我用它调试!!!不要提供解决方案!

我需要一段包含错误的代码和解决方法如何更改它以纠正错误。



我有什么尝试过:



-tried调试我的程序;

-asked我团队的其他一些学生,但他们不喜欢告诉我任何事情;

-在线查找这个问题

解决方案

你一直在问同样的问题:怎么了?我的代码?



如果你不想要答案使用调试器并不重要,因为这是正确的答案 - 除非你变得精通在使用调试器并使用这些琐碎的任务调试自己的代码时,你将永远无法在大型项目上工作。



所以:使用调试器。再次。


引用:

我使用了DEBUGGER!不要提供解决方案!!!



调试器只显示代码正在做什么,你的任务是检查它是否与你的算法匹配。如果您发现调试器没有问题,唯一的可能是算法错误。

分析样本输入,只检查7就足以得到答案了。

构建您自己的具有不同情况的测试输入,看看您是否得到了正确的答案。如果答案不正确,则是代码或算法中的错误。

首次测试输入长度为1

输入示例:
1
100
输出:
1





[更新]

Quote:

是的,这是我的问题 - 在我的算法中发现错误。我尝试了很多次,但我看不到它们,这就是我写这个网站的原因。



找到算法是问题的核心。

拿一张纸和一支铅笔练习样品。

从声明中,你必须回答这个问题:为什么数字是序列的一部分?

答案是你的算法。



学习分析方法可以提供帮助:

- EW Djikstra自上而下的方法是一个良好的开端。

https:// en.wikipedia.org/wiki/Top-down_and_bottom-up_design [ ^ ]

https://en.wikipedia.org/wiki/Structured_programming [ ^ ]

https://en.wikipedia.org/wiki/Edsger_W._Dijkstra [ ^ ]

https://www.cs.utexas.edu/users/EWD/ewd03xx/EWD316.PDF [ ^


In this task we have to print the length of the longest succession of even numbers not divided by 3, and numbers not divided by 7. We get size_a (0<size_a<1000) elements in vector. Each element (-1000 < Xi < 1000).

Answer must be one integer number.

#include <iostream>
#include <vector>

using namespace std;

int main()
{
	int size_a;
	cin >> size_a;
	int  num = 1, num_max = 0;
	vector <int> v (size_a);
	for (auto i = 0; i < size_a; i++)
		cin >> v[i];
	//---------------------------------
	for (auto i = 1; i < size_a; i++)
	{
		if ((v[i - 1] % 7 != 0) && (v[i] % 7 != 0))
		{
			num++;
			if (num_max < num)
				num_max = num;
		}
		else if ((v[i - 1] % 2 == 0) && (v[i] % 2 == 0)) {
			if ((v[i - 1] % 3 != 0) && (v[i] % 3 != 0))
			{
				num++;
				if (num_max < num)
					num_max = num;
			}
		}
		else
			num = 1;
	}
	cout << num_max << endl;
}



Example of input:
5
100 53 23 19 31
Output:
5
--
Example of input:
7
87 3 36 63 78 68 44
Output:
3
------------------
My program has a mistake, which I can`t find - it works on several tests, but in our colledge`s Online program checker it doesn`t pass test 7. I do not know input example. Several people in my group have done the task, but do not tell me how to do it.
I USED DEBUGGER ON IT!!! DO NOT OFFER THAT AS SOLUTION!!!
I need a piece of code that contains a mistake and solution how to change it to correct one.

What I have tried:

-tried to debug my program;
-asked some other students of my group, but they don`t tell me anything;
-looked up this problem online

解决方案

You are asking the same question all the time: "What's wrong with my code?"

And it doesn't matter if you don't want the answer "Use the debugger" because that is the correct answer - unless you become proficient at using the debugger and debugging your own code with these trivial tasks you will never get good enough to work on larger projects.

So: Use the debugger. Again.


Quote:

I USED DEBUGGER ON IT!!! DO NOT OFFER THAT AS SOLUTION!!!


the debugger only show what the code is doing, your task is to check if it match your algorithm. If you see no problem with debugger, the only possibility is that the algorithm is wrong.
Analyze the sample inputs, checking only the 7 is enough to get the answer.
Build your own test inputs that have different situations and see if you get the correct answers. if answer is not correct, it is a bug in code or in algorithm.
First test have input of length 1

Example of input:
1
100
Output:
1



[Update]

Quote:

Yes, that IS my problem - to find mistakes in my algorithm. I tried many times, but I can`t see them, and that`s why I am writing to this website.


Finding the algorithm is the heart of the problem.
Take a sheet of paper and a pencil and practice on samples.
From the statement, you have to answer the question: "Why number is part of a sequence or not?"
the answer is your algorithm.

Learning an analyze method can help:
- E.W. Djikstra top-Down method is a good start.
https://en.wikipedia.org/wiki/Top-down_and_bottom-up_design[^]
https://en.wikipedia.org/wiki/Structured_programming[^]
https://en.wikipedia.org/wiki/Edsger_W._Dijkstra[^]
https://www.cs.utexas.edu/users/EWD/ewd03xx/EWD316.PDF[^]


这篇关于我遇到了这个任务的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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