This topic has not been done since there are too many corner case to consider.1. Divisor equals 0.2. Dividend equals 0.3. Is the result negative?4. When dividend equals Integer.min_value and divisor equals-1, the result would overflow. Convert result to long and then to Integer.5. Divided by divisor * 2 ^ n to avoid exceeding time limit6. There is a to convert divisor and
Hundred discretionary system development "13710813997 Mr. Zheng" hundred-product system development, hundred discretionary still product system app development, hundred discretionary still product model app development, hundred discretionary still product app development source code, hundred discretionary still product software development source code, hundred discretionary still product software app development, Hundred discretionary still product platform software development, hundred discreti
funct Ion DateDiff ($date 1, $date 2, $unit = "") {//Time comparison function, return two dates in seconds, minutes, hours or days
switch ($unit) {
C ASE ' s ':
$dividend = 1;
break;
case ' I ':
$dividend =//osphp.com.cn
break;
case ' h ':
$dividend = 3600;
break;
case ' d ':
1. Question
2. Answer 2.1. method 1
The question requires that multiplication, division, and Division operations cannot be used, but we canDivision is transferred to the logarithm Field.
$ \ Frac {A} {B} = e ^ {\ frac {LNA} {LNB} = e ^ {LNA-LNB} $
In this way, it is converted to exponential, logarithm, and subtraction operations. Because we can only take the logarithm of a positive integer, we must first take the absolute values of both numbers, and then add the symbol.
At the same time, the que
dividend * divisor */Private Static void calc4division (INT dividend, int divisor) {int result = 0; If (divisor = 0) {Throw new arithmeticexception ("the divisor is 0 and does not comply with human computing rules! ") ;}Result = dividend/divisor; system. Out. println (" Calculation Result: "+ result );}
It can be seen that throw arithmeticexception is abnorma
[0]);}return false;}function Check_time ($time) {//Check time is legal time
Cloud Habitat Community Network
$timearr = Explode (":", $time);if (is_numeric ($timearr [0]) is_numeric ($timearr [1]) is_numeric ($timearr [2])) {
//
if ($timearr [0] >= 0 $timearr [0]
Php
return true;Elsereturn false;}return false;}
function DateDiff ($date 1, $date 2, $unit = "") {
A time comparison function that returns two dates, a few seconds, minutes, hours, or days.//Switch ($unit) {Case ' s
Exception handling:Java exception handling keywords are: Try Catch finally throw throwsThe error is not an exception, but a problem that is out of the programmer's control, and all exception classes are subclasses that inherit from the Java.lang.Exception class.The cause of the exception usually includes:The user has entered illegal dataThe file you want to open does not existThe connection is interrupted during network communication, or the JVM memory overflows.Syntax for the TRY Catch structur
']));
}
function Getweekday ($date) {//Calculate the date given is the day of the week
$DATEARR = Explode ("-", $date);
Return Date ("W", Mktime (0,0,0, $DATEARR [1], $DATEARR [2], $DATEARR [0]));
}
Copy Code code as follows:
function Check_date ($date) {//check date is legal date
$DATEARR = Explode ("-", $date);
if (is_numeric ($dateArr [0]) is_numeric ($DATEARR [1]) is_numeric ($DATEARR [2])) {
return Checkdate ($DATEARR [1], $DATEARR [2], $DATEARR [0]);
}
return
Objective: to correctly understand the Exception Handling Mechanism of C ++Learning the definition and execution process of Exception Handling
Content: defines a cexception class, with a member function reason (), used to display the exception type, trigger an exception in the subfunction, and handle the exception in the main program, observe the execution process of the program.
3. Experiment task: Use cout to display the exception type in the member function reason () of the cexception class,
Divide-integers without using multiplication, division and mod operator.If It is overflow, return2147483647Given dividend = 100 and divisor = 9 , return 11 . Public classSolution {/** * @paramDividend The dividend *@paramdivisor the divisor *@returnThe result*/ Public intDivideintDividend,intdivisor) { //Write Your code here if(Divisor = = 0) return2147483647; if(
Divide-integers without using multiplication, division and mod operator.If It is overflow, return max_int.Solution:1 Public classSolution {2 Public intDivideintDividend,intdivisor) {3 if(divisor==0)returnInteger.max_value;4 if(dividend==0)return0;5 if(divisor==1)returndividend;6 if(Dividend==integer.min_value Divisor==-1)returnInteger.max_value;7 8 BooleanNeg =
Check if date is legal
function Check_date ($date) {//check date is legal date
$DATEARR = Explode ("-", $date);
if (is_numeric ($dateArr [0]) is_numeric ($DATEARR [1]) is_numeric ($DATEARR [2])) {//Open source OSPhP.COM.CN
return Checkdate ($DATEARR [1], $DATEARR [2], $DATEARR [0]);
}
return false;
}
function Check_time ($time) {//Check whether the time is legitimate
OSPHP.COM.Cn Open Source
$TIMEARR = Explode (":", $time);
i
($date) {//calculates the date given is the day of the week
$DATEARR = Explode ("-", $date);
Return Date ("W", Mktime (0,0,0, $DATEARR [1], $DATEARR [2], $DATEARR [0]));
}
Copy CodeThe code is as follows:
function Check_date ($date) {//check date is legal date
$DATEARR = Explode ("-", $date);
if (is_numeric ($dateArr [0]) is_numeric ($DATEARR [1]) is_numeric ($DATEARR [2])) {
return Checkdate ($DATEARR [1], $DATEARR [2], $DATEARR [0]);
}
return false;
}
function Check_time ($time) {//Check
implementations. The above code can also be written as follows:
// 5.33 non-recursive function sumX2 (A, x) {var I = 0, j =. length-1; while (I
Recursion does not always mean inefficiency. In some scenarios, recursion is more efficient. For example, to calculate the m power of x, a conventional algorithm requires m multiplication. The following algorithm, however, the time complexity is reduced to O (logn)
// Calculate the m power of x (Recursive Implementation) // time complexity O (logn) fun
In the test interface, there will be such a scenario, input illegal parameters, check the return error code and error contentThe usual practice is to send a request, the wrong return result is taken out, and then the comparison match; but there is a problem that requires a further write error to return the parse function, which cannot be shared with the previously requested function.At this time, we can use the assertraises, Assertraisesregexp;python 2.7 in UnitTest called Assertraises,nose.tool
The main approach is: the first constant through the shift operation to find divisor closest to the dividend multiples, such as dividend is 100, the divisor is 3, this multiple is 32.
Then use 100 minus 3 of 32 times times, where 32 times times is generated by the left shift, and no multiplication is used.
At this point, then use a loop to find 3 times the most close to 100 and its 32 times times the differ
Index: [Leetcode] leetcode key index (C++/JAVA/PYTHON/SQL)Github:https://github.com/illuz/leetcode 029. Divide two integers (Medium) link :
Title: https://oj.leetcode.com/problems/divide-two-integers/Code (GitHub): Https://github.com/illuz/leetcode :
To achieve division, you cannot use multiplication, division, and modulo. Analysis :
You can't use multiplication, divide and take modulo, the rest, plus, minus and bitwise operations. The idea is to subtract from time to time, but it will time
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.