Abs()不工作.... [英] Abs () not working ....

查看:115
本文介绍了Abs()不工作....的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用abs()查找差异但不能.....



我尝试过:



using abs () to find difference but cann't .....

What I have tried:

#include <iostream>

#include<cmath>
#include<limits.h>
#include<stdio.h>
#include<algorithm>
#include<stdlib.h>


using namespace std;

int main()
{

    long int n=0;
    long int a[n];
    long int value;
    cin>>n;
    for(int i=0; i<n;>    {
        cin>>a[i];
    }
    cin>>value;
    cout<<endl;
    int temp=0,temp2=0;
    float diff=0.0,diff2=0.0;
    for(int i=0; i<n;>    {
        temp=a[i];

        for(int j=i+1; j<n;>        {
            if(i==0)
            {
                if(temp+a[j]==value)
                {
                    temp2=a[j];
                    diff=temp2-temp;
                         diff=abs(diff);
                    cout<<"temp="<<temp<<"temp2="<<temp2<<"diff="<<diff<<endl;
                }
            }


            else
            {
                if(temp+a[j]==value)
                {
                    temp2=a[j];
                    diff2=temp2-temp;
                    diff2=abs(diff2);


                    if(diff2<diff)>
                        diff=diff2;


                    cout<<"temp="<<temp<<"temp2="<<temp2<<"diff="<<diff<<endl;
                }
            }

        }
    }
    cout<<"diff="<<diff<<" value="<<value<<endl;
    int aa=(value+diff)/2;
    int b=(value-diff)/2;
    cout<<"Peter should buy books whose prices are "<<b<<" and "<<aa<<endl;</stdlib.h></algorithm></stdio.h></limits.h></cmath></iostream>

推荐答案

#include <stdlib.h>





Abs在stdlib.h文件中定义你可以通过简单地查看Abs的帮助来找到它。



如果你发现你需要浮点版本它叫做fabs并在math.h中定义



Abs is defined in the stdlib.h file you can find that out by simply looking at the help for Abs.

If you find you need the float version it is called fabs and is defined in math.h


我认为你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到有一点它会停止你所期望的。

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

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]
I think you should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]


这篇关于Abs()不工作....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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