本文介绍了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



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

08-01 22:12