- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在做一道题,我必须找到数字
[4 + sqrt(11)]n
.
例如,当 n = 4, [4 + sqrt(11)]4 = 2865.78190... 时,答案是 65。其中 n 可以从 2 变化<= n <= 109。
我的解决方案 - 我尝试构建一个平方根函数来计算 11 的平方根精度等于用户输入的n值。
我用过BigDecimal
在 Java 中避免溢出问题。
public class MathGenius {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
long a = 0;
try {
a = reader.nextInt();
} catch (Exception e) {
System.out.println("Please enter a integer value");
System.exit(0);
}
// Setting precision for square root 0f 11. str contain string like 0.00001
StringBuffer str = new StringBuffer("0.");
for (long i = 1; i <= a; i++)
str.append('0');
str.append('1');
// Calculating square root of 11 having precision equal to number enter
// by the user.
BigDecimal num = new BigDecimal("11"), precision = new BigDecimal(
str.toString()), guess = num.divide(new BigDecimal("2")), change = num
.divide(new BigDecimal("4"));
BigDecimal TWO = new BigDecimal("2.0");
BigDecimal MinusOne = new BigDecimal("-1"), temp = guess
.multiply(guess);
while ((((temp).subtract(num)).compareTo(precision) > 0)
|| num.subtract(temp).compareTo(precision) > 0) {
guess = guess.add(((temp).compareTo(num) > 0) ? change
.multiply(MinusOne) : change);
change = change.divide(TWO);
temp = guess.multiply(guess);
}
// Calculating the (4+sqrt(11))^n
BigDecimal deci = BigDecimal.ONE;
BigDecimal num1 = guess.add(new BigDecimal("4.0"));
for (int i = 1; i <= a; i++)
deci = deci.multiply(num1);
// Calculating two digits before the decimal point
StringBuffer str1 = new StringBuffer(deci.toPlainString());
int index = 0;
while (str1.charAt(index) != '.')
index++;
// Printing output
System.out.print(str1.charAt(index - 2));
System.out.println(str1.charAt(index - 1));
}
}
此解决方案适用于 n = 200,但随后开始变慢。它在 n = 1000 时停止工作。
什么是处理问题的好方法?
2 -- 53
3 -- 91
4 65
5 67
6 13
7 71
8 05
9 87
10 73
11 51
12 45
13 07
14 33
15 31
16 85
17 27
18 93
19 11
20 25
21 47
22 53
23 91
24 65
25 67
最佳答案
在 n=22 时,结果似乎从 n=2 的位置开始重复。因此,将这 20 个值按照与列表中相同的顺序保存在数组中,例如nums[20]
。
然后当用户提供一个 n 时:
return nums[(n-2)%20]
现在有这种模式重复的证明 here .
或者,如果你坚持计算长度;因为您是通过循环乘法(而不是 BigDecimal pow(n))来计算幂的,所以您可以将前面使用的数字修剪到最后 2 位数字和小数部分。
关于java - 查找数字 (4+sqrt(11))^n 的小数点前的最后两位数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21964541/
前一段时间写过一篇文章《 实战,一个高扩展、可视化低代码前端,详实、完整 》,得到了很多朋友的关注。 其中的逻辑编排部分过于简略,不少朋友希望能写一些关于逻辑编排的内容,本文就详细讲述一下逻辑
我正在尝试以下 Java 片段: int[] testArray={10,20,30,40}; int i= 0; testArray[i++]= testArray[i++]+1; System.o
我想知道我是否可以通过某种方式在 C++ 中进行前/后函数调用。我有一个包含很多函数的包装器类,在每次调用包装器函数后,我应该调用另一个始终相同的函数。 所以我不想像这样对每个函数调用 postFun
我有一个像这样的头文件: #pragma once #include "gamestate.h" #include "ExitListener.h" class InitialGameState :
学习左值和右值。定义是任何可以是“地址”的东西都是左值,否则就是右值。 我检查了运算符的优先级,前缀和后缀增量都比“地址”运算符具有更高的优先级。 对于下面的两个例子,谁能解释一下为什么第一个“&++
在我的学习过程中,我遇到了前后迭代器,我想知道是否有办法让它们就地创建容器元素。从文档来看,容器似乎需要实现 push_back 函数才能与 back_iterator 一起使用。但是有没有一种方法可
我有两个关于 Java 中运算符优先级的类似问题。 第一个: int X = 10; System.out.println(X++ * ++X * X++); //it prints 1440 根据
请放轻松,不要对我开枪,因为我还是新手。 当我运行这段代码时,我完全糊涂了,终生无法弄清楚为什么: int y = 9; cout << "++y = " << ++y << "\n--y = " <
两种表达方式有区别吗: (*x)++ 和 ++(*x) 我可以看到这两个语句都替换了 *x 中 (*x+1) 的内容。但是它们之间有什么区别吗? 最佳答案 (*x)++ 计算为*x的值;作为副作用,*
我有一个如下所示的数据集: Date CONSUMER DISCR CONSUMER STAPLES ENERGY FINANCIALS HEALTH CARE
我希望检查名称字段中输入的前两个字符是否为字母 - 除此之外没有什么区别(空格、'、- 等都是公平的游戏)。这是我到目前为止所拥有的,但它不起作用。想法?谢谢! if (document.form01
我制作了一个简单的脚本,为像素和所有附近的像素着色为相同的颜色 Click foto
我需要编写一个循环,以下列格式输出从昨天算起的最近 30 天: 2014-02-02 2014-02-03 2014-02-04 ... 2014-03-04 我想我需要像这样使用循环: for ($
我正在做一些练习,但我对这个感到困惑: public static int f (int x, int y) { int b=y--; while (b>0) { if (x%2!=0
我需要一个 4 个字符的正则表达式。前 3 个字符必须是数字,最后 1 个字符必须是字母或数字。 我形成了这个,但它不起作用 ^([0-9]{3}+(([a-zA-Z]*)|([0-9]*)))?$
我需要编写一个循环,以下列格式输出从昨天算起的最近 30 天: 2014-02-02 2014-02-03 2014-02-04 ... 2014-03-04 我想我需要像这样使用循环: for ($
我有下面的程序,我试图找到前 1000 个素数的总和。在代码中,解决方案1和2有什么区别?为什么我不应该将 count 变量放在 if 条件之外?如果我把变量放在 if 之外,我显然没有得到我需要的答
这个问题在这里已经有了答案: Replace First N Occurrences in the String (7 个答案) 关闭 4 年前。 我有一个如下的字符串 const str = '_
我正在尝试测量以纳秒为单位的平均访问延迟,但在第一次迭代后我收到“段错误(核心转储)”。我错过了什么吗?我是否滥用了指针。这是导致错误的函数: #include #include #include
我有一个 SQL 问题 (MySQL)。我如何从下表创建一个新表(表名称:“well_master_prod_inj”)。 我需要按井名和日期聚合数据。我希望每个井名只有一行数据以及显示以下数据的列:
我是一名优秀的程序员,十分优秀!