小编Aru*_*dey的帖子

在不使用sqrt函数的情况下查找平方根?

我发现了不使用sqrt函数找出平方根的算法,然后尝试进入编程.我最终使用C++中的这个工作代码

    #include <iostream>
    using namespace std;

    double SqrtNumber(double num)
    {
             double lower_bound=0; 
             double upper_bound=num;
             double temp=0;                    /* ek edited this line */

             int nCount = 50;

        while(nCount != 0)
        {
               temp=(lower_bound+upper_bound)/2;
               if(temp*temp==num) 
               {
                       return temp;
               }
               else if(temp*temp > num)

               {
                       upper_bound = temp;
               }
               else
               {
                       lower_bound = temp;
               }
        nCount--;
     }
        return temp;
     }

     int main()
     {
     double num;
     cout<<"Enter the number\n";
     cin>>num;

     if(num < 0)
     {
     cout<<"Error: Negative number!";
     return 0;
     }

     cout<<"Square roots are: +"<<sqrtnum(num) and …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm math sqrt

17
推荐指数
2
解决办法
9万
查看次数

使用c ++在数组中出现的大多数元素?

我曾尝试过以下代码来获取数组中最常出现的元素.它运行良好但唯一的问题是当有两个或多个元素具有相同的出现次数并且等于最多出现的元素时,它只显示扫描的第一个元素.请帮我解决这个问题.

#include <iostream>
using namespace std;
int main()
{
    int i,j,a[5];
    int popular = a[0];
    int temp=0, tempCount, count=1;
    cout << "Enter the elements: " << endl;
    for(i=0;i<5;i++)
        cin >> a[i];
    for (i=0;i<5;i++)
    {
        tempCount = 0;
        temp=a[i];
        tempCount++;
        for(j=i+1;j<5;j++)
        {
            if(a[j] == temp)
            {
                tempCount++;
                if(tempCount > count)
                {
                    popular = temp;
                    count = tempCount;
                }
            }
        }
    }
    cout << "Most occured element is: " <<  popular;
}
Run Code Online (Sandbox Code Playgroud)

c++ arrays algorithm

4
推荐指数
1
解决办法
3万
查看次数

错误:运算符“!” 不能应用于“int”类型的操作数

我是 C 语言编程的新手,正在编写程序以确定数字是否为 2 的幂。但是作为操作员'!'出现错误 不能应用于 int 类型的操作数。认为相同的程序在 C++ 中运行良好。这是代码:

    public static void Main(String[] args)
    {
        int x;

        Console.WriteLine("Enter the number: ");

        x = Convert.ToInt32(Console.ReadLine());


        if((x != 0) && (!(x & (x - 1))))

            Console.WriteLine("The given number "+x+" is a power of 2");
    }
Run Code Online (Sandbox Code Playgroud)

c# boolean-logic operators

3
推荐指数
1
解决办法
8802
查看次数

标签 统计

algorithm ×2

c++ ×2

arrays ×1

boolean-logic ×1

c# ×1

math ×1

operators ×1

sqrt ×1