current position:Home>69. Sqrt (x) (c + + problem solving version with vs runnable source program)
69. Sqrt (x) (c + + problem solving version with vs runnable source program)
2022-01-27 04:37:35 【Bufan】
69. Sqrt(x)(C++ The problem solving version contains VS Can run the source program )
1. Answer key
1.1 violence
- Direct traversal , Note that the value of the stored square is long long.
- Method 2 is also traversal , However, the search space has been reduced a lot , So the speed is also very fast .
1.2 Dichotomy search
- For a nonnegative number n, Its square root will not be greater than (n/2+1);
- stay [0, n/2+1] Within this range, binary search can be carried out , Find out n The square root of .
2. Power button C++ Source code
2.1 violence
class Solution {
public:
int mySqrt(int x) {
for (long long int i = 0; i <= x; i++) {
long long int s = i * i;
if (s == x)return i;
else if (s > x)return i - 1;
}
return 0;
}
};
2.2 Dichotomy
class Solution {
public:
int mySqrt(int x) {
long long int left = 0;
long long int right = x / 2 + 1;
long long int mid = 0;
long long int mul = 0;
while (left < right) {
mid = (left + right) / 2;
mul = mid * mid;
if (mul < x)left = mid + 1;
else if (mul > x)right = mid - 1;
else return mid;
}
return left * left <= x ? left : left - 1;
}
};
3.VS Can run the source program
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<limits>
#include<algorithm>
#include<math.h>
#pragma warning(disable:4996)
using namespace std;
class Solution {
public:
int mySqrt(int x) {
// Traverse ( violence ) use long long
/*for (long long int i = 0; i <= x; i++) { long long int s = i * i; if (s == x)return i; else if (s > x)return i - 1; } return 0;*/
// Two points search
long long int left = 0;
long long int right = x / 2 + 1;
long long int mid = 0;
long long int mul = 0;
while (left < right) {
mid = (left + right) / 2;
mul = mid * mid;
if (mul < x)left = mid + 1;
else if (mul > x)right = mid - 1;
else return mid;
}
return left * left <= x ? left : left - 1;
}
};
int main()
{
printf(" Enter a non negative integer :");
int x;
scanf("%d", &x);
Solution test;
int ans = test.mySqrt(x);
printf(" Its arithmetic square root ( Integral part ) by :%d", ans);
printf("\n");
system("pause");
return 0;
}
copyright notice
author[Bufan],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270437295876.html
The sidebar is recommended
- Spring IOC container loading process
- [thinking] the difference between singleton mode and static method - object-oriented programming
- Git tool
- Force deduction algorithm - 92 Reverse linked list II
- What is the sub problem of dynamic programming?
- C / C + +: static keyword summary
- Idea does not have the artifacts option when configuring Tomcat
- I don't know how to start this
- MySQL slow log optimization
- [Vue] as the window is stretched (larger, smaller, wider and higher), the text will not be displayed
guess what you like
-
Popular Linux distributions for embedded computing
-
The 72 year old uncle became popular. Wu Jing and Guo fan made his story into a film, which made countless dreamers blush
-
Two pronged approach, Tsinghua Professor Pro code JDK and hotspot source code notes, one-time learning to understand
-
C + + recursive knapsack problem
-
The use of GIT and GitHub and the latest git tutorial are easy to understand -- Video notes of crazy God speaking
-
Ignition database test
-
Bootstrap switch switch control user's guide, springcloud actual combat video
-
A list that contains only strings. What other search methods can be used except sequential search
-
[matlab path planning] multi ant colony algorithm grid map path planning [including GUI source code 650]
-
[matlab path planning] improved genetic algorithm grid map path planning [including source code phase 525]
Random recommended
- Appium settings app is not running after 5000ms
- Reactnative foundation - 07 (background image, status bar, statusbar)
- Reactnative foundation - 04 (custom rpx)
- Linux checks where the software is installed and what files are installed
- SQL statement fuzzy query and time interval filtering
- Java project: OA management system (java + SSM + bootstrap + MySQL + JSP)
- Configuration and use of private image warehouse of microservice architect docker
- Relearn JavaScript events
- How does Java dynamically obtain what type of data is passed? It is used to judge whether the data is the same, dynamic data type
- [data structure] chain structure of binary tree (pre order traversal) (middle order traversal) (post order traversal) (sequence traversal)
- Detailed explanation of red and black trees
- redhat7. 9 install database 19C
- Blue Bridge Cup notes: (the given elements are not repeated) complete arrangement (arrangement cannot be repeated, arrangement can be repeated)
- Detailed explanation of k8s management tool kubectl