为什么我的C ++代码不起作用 [英] Why my C++ code is not working

查看:106
本文介绍了为什么我的C ++代码不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我的c ++代码无效

我的程序中没有编译错误。

我的代码的几行要求用户输入TA和B的值正在工作但输出结束



基本上我的代码是获取(A,B)的GCD



我使用过Debugger但找不到这个漏洞。

我能找到的只是反转(第27行)功能后出现了一些错误。



我尝试了什么:



  #include   <   iostream  >  
#include < span class =code-preprocessor> < vector >
#include < algorithm >
使用 命名空间 std ;
void 除数( int n,vector< int> vec)
{
for int i = 1 ; I< N;我++)>
{
if (n%i == 0
{
vec.push_back(i); // 它已经排序ha
}
}
}

int main()
{
vector< INT> VEC 1;
int T;
int A;
int B;
cin>>吨;
for int j = 0 ; J<吨; ++ j)的>
{
cin>> A>> B;

除数(A,vec1);

reverse(vec1.begin(),vec1.end()); // LINE 27

for int k = 0 ; K< vec1.size(); K ++)>
{
if (B%vec1.at(k)== 0
{
cout<< vec1.at(k)<< ENDL;
break ;
}
}
}
}

解决方案

有一些错误没有告诉我们 - 任何事情都可能发生,我们不知道是什么!说这就像你的车在不知名的地方发生故障,然后当你打电话给车库说它坏了!并结束通话。您认为在到达正确的部件之前您会等待多久?



因此请使用调试器。

设置断点函数的开始,在调试器中运行您的应用程序,并依次逐步执行每一行。使用调试器查看变量中的内容。

在执行每一行之前,先弄清楚你预期会发生什么,然后执行该行,看看发生了什么。这是你的期望吗?如果是这样,继续前进。如果不是......为什么不呢?有什么不同?发生了什么事,你没想到,或者没有发生过你的事情?



调试是一项技能:你只能通过使用它来开发它。在这样一个简单的代码示例上开发它比在100,000行完整项目上开发它要好得多!

试一试:看看你能找到什么信息!


计算GCD的方式很复杂,浪费资源。

收集一个数字的所有除数需要花费很多时间。



错误:你忘了n是它本身的除数!

  void 除数(< span class =code-keyword> int  n,vector< int> vec)
{
for int i = 1 ; i< n> +1 ; i ++)>
{
if (n%i == 0
{
vec.push_back(i); // 它已经排序ha
}
}
}< / n>< / int>



最大公约数 - 维基百科,免费百科全书 [ ^ ]



[UpDate]

你的程序问题是数字越大,越长需要得到答案。它是O(n)。

正确的方法是利用2个数字和GCD之间的数学关系。

这两个数字是倍数对于GCD,它意味着2个数字之间的差异也是GCD的倍数。

这意味着GCD(A,B)< => GCD(B,A%B)

这个

  int  GCD(  int  A, int  B)
{
int C;
C = A%B;
if (C == 0
返回 B;
return GCD(B,C);
}



是一个递归版本。


你将值向量传递给你的除数功能:

 void Divisor(int n,vector< int> vec)



调用函数时,会创建一个新的向量实例,并使用传递的向量的数据填充。但传递的向量的内容不会更改。



更改传递的<$ c的内容$ c> vector ,通过指针或引用传递:

 void Divisor(int n,vector< int>& vec)


Hello
My c++ code is not working
There are no compilation error in my program.
The few lines of my code which asks the user to put value of T A and B is working but there output coming

Basically My code is to get the GCD of (A,B)

I have used Debugger but can not find the flaw.
Only thing I can find is that after reverse(line 27) function there is some error.

What I have tried:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void Divisor(int n,vector<int>vec)
{
    for(int i=1;i<n;i++)>
    {
        if(n%i==0)
        {
            vec.push_back(i);// It will be already sorted ha
        }
    }
}

int main()
{
    vector<int>vec1;
    int T;
    int A;
    int B;
    cin >> T;
    for(int j=0;j<t;++j)>
    {
        cin >> A >> B;

        Divisor(A,vec1);

        reverse(vec1.begin(),vec1.end());//LINE 27

        for(int k=0;k<vec1.size();k++)>
        {
            if(B%vec1.at(k)==0)
            {
                cout << vec1.at(k) << endl;
                break;
            }
        }
    }
}

解决方案

"there is some error" tells us nothing - anything could be happening and we have no idea what! Saying that is like your car breaking down in the middle of nowhere, then when you call the garage just saying "it broke!" and ending the call. How long do you think you will be waiting before they arrive with the right parts?

So use the debugger.
Put a breakpoint at the start of the function, run your app in the debugger, and step through each line in turn. Use the debugger to look at what is in what variable.
Before you execute each line, work out what you expect to happen first, then execute the line and see what did happen. Was it what you expected? If so, move on. If not ... why not? What was different? What happened that you didn't expect, or didn't happen that you did?

Debugging is a skill: you only develop it by using it. And it's a lot better to develop it on a trivial code sample like this than on a 100,000 line complete project!
Give it a try: see what information you can find out!


The way you calc the GCD is complicated and a waste of resources.
Collecting all divisors of a number takes a lot of time.

Bug: You forgot that n is divisor of itself !

void Divisor(int n,vector<int>vec)
{
    for(int i=1; i<n>+1; i++)>
    {
        if(n%i==0)
        {
            vec.push_back(i);// It will be already sorted ha
        }
    }
}</n></int>


Greatest common divisor - Wikipedia, the free encyclopedia[^]

[UpDate]
The problem with your program is that the bigger the numbers, longer it takes to get the answer. It is O(n).
The right way to do it is by taking advantage of the mathematical relation between the 2 numbers and the GCD.
The 2 numbers are multiples of the GCD, it imply that the difference between the 2 numbers is also a multiple of the GCD.
This means that GCD(A,B) <=> GCD(B, A%B)
This

int GCD(int A, int B)
{
    int C;
    C= A % B;
    if (C == 0)
        return B;
    return GCD(B, C);
}


is a recursive version.


You are passing the vector by value to your Divisor function:

void Divisor(int n, vector<int> vec)


When calling the function, a new vector instance is created and filled with the data of the passed vector. But the content of the passed vector is not changed.

To change the content of the passed vector, pass it by pointer or reference:

void Divisor(int n, vector<int>& vec)


这篇关于为什么我的C ++代码不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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