我在这项任务中看不到任何问题 [英] I can't see any problems in this task

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

问题描述

在这个任务中,我们有一个游戏中的玩家,他已经有k(0≤k≤10^ 8)分数,最高等级是n(1≤n≤100)。我们得到了一个数组(大小= n-1),其中包含了玩家必须获得的分数才能达到下一级别。

输出 - 当前玩家的等级。

In this task, we have a player in game, who already has k (0 ≤ k ≤ 10^8) scores and the maximum level is n (1 ≤ n ≤ 100). We`ve got an array (size = n-1) which contains amount of scores that player has to get additionally to k to get to next level.
Output - current player`s level.

#include <iostream>
#include <vector>

using namespace std;

int main()
{
	int k = 0, n = 0;
	cin >> k >> n;
	vector <int> v(n-1);
	for (auto i = 0; i < n - 1; i++)
		cin >> v[i];
	int s = 0, l = 1;
	for (auto i = 0; i < n - 1; i++)
		if (s + v[i] <= k)
		{
			s += v[i];
			l++;
		}
	cout << l << endl;
}



示例:

输入:

500 5

100 200 300 400

输出:

3

-

输入

600 6

100 100 200 200 300

输出

5

--------- -

在我的委托中,我们有一个特殊的网站,它编译和测试我们的程序。我已经在很多例子中自己测试了这个程序,并尝试重写几次,但是它一直在测试8上。我不能得到这个测试的输入或输出值。我小组的每个人都完成了这项任务。他们使用的是C,但我使用的是C ++,因为我不知道C.



我尝试了什么:



重写程序

多次测试

在线查找相同的任务


Example:
input:
500 5
100 200 300 400
Output:
3
--
input
600 6
100 100 200 200 300
output
5
-----------
In my coledge we`ve got a special website, which compiles and tests our programes. I`ve tested this program myself on many examples and tried to rewrite it several times, but it keeps crushing on test 8. I can`t get input or output values of this test. Everyone from my group did this task exept me. They`re using C, but I use C++ `cause I don`t know C.

What I have tried:

to rewrite program
tested it many times
looked for same tasks online

推荐答案

(如果我找到你的话)从
(If I got you) change from
引用:

for(auto i = 0; i< n - 1; i ++)

if(s + v [i]< = k)

{

s + = v [i];

l ++;

}

for (auto i = 0; i < n - 1; i++)
if (s + v[i] <= k)
{
s += v[i];
l++;
}



to

for (auto i = 0; i < n - 1; i++)
  if (s + v[i] > k)
    break;
  else
  {
    s += v[i];
    l++;
  }


如果您了解C ++,那么您就知道C - 它是C ++的一个子集。



我们不知道测试8的作用,我们也不知道输出应该是什么或为什么。

仔细阅读你的作业问题,然后尝试运行你的代码在调试器中使用您自己的测试数据,看看到底发生了什么。



我们不能为你做这件事。
If you "know C++" then you know C - it's a subset of C++.

We have no idea what "test 8" does, and we also have no idea what the output should be or why.
Read your homework question carefully, and try running your code in the debugger using your own test data and see exactly what is happening.

We can't do that for you.


你的问题是知道在哪里停止。

一旦你知道你无法达到的水平,你可以停下来,你有答案。



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

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

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



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



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

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

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

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
Your problem is about knowing where to stop.
As soon as you know a level you can't reach, you can stop, you have the answer.

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天全站免登陆