題目:javascript
給定一個整數,將其轉換爲羅馬數字。html
輸入保證在1到3999的範圍內。java
答案:git
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title></title>
</head>scala
<body>
<script type="text/javascript">
function nums(num1,num2){
if(num2 == 0 || num1 == Number.MIN_VALUE && num2 == -1){
return Number.MAX_VALUE;
}
var r = 0;
var sign = (num1 > 0 && num2 > 0) || (num1 < 0 && num2 < 0) ? 1 : -1;
if(num1 > 0){
num1 = ~num1 + 1;
}
if(num2 > 0){
num2 = ~num2 + 1;
}
var digit = 0;
while(num1 - num2 <= num2){
num2 <<= 1;
digit++;
}
num2 >>= digit;
for(var i = digit; i>=0; i--){
if(num1 <= num2 << i){
r += 1 << i;
num1 -= num2 << i;
}
}
return sign > 0 ? r : -r;
}
nums(10,3);
console.log(nums(10,3));
</script>
</body>
</html>htm