解决方案:
事实证明代码本身(可能)"没有错"; 这只是效率低下.如果我的数学是正确的,如果我让它继续运行它将在2011年10月14日星期五之前完成.我会告诉你的!
警告:如果您尝试解决Project Euler#3,这可能包含剧透.
问题是这样的:
13195的主要因素是5,7,13和29.
600851475143的最大主要因素是什么?
这是我尝试解决它.我只是从Java和编程开始,我知道这不是最好或最有效的解决方案.
import java.util.ArrayList;
public class Improved {
public static void main(String[] args) {
long number = 600851475143L;
// long number = 13195L;
long check = number - 1;
boolean prime = true;
ArrayList<Number> allPrimes = new ArrayList<Number>();
do {
for (long i = check - 1; i > 2; i--) {
if (check % i == 0) {
prime = false;
}
}
if (prime == true && number % check …Run Code Online (Sandbox Code Playgroud) 我有字符串0000001.45,我想在字符串前面删除零并将字符串转换为long.是否可以无循环地进行投射.有java的一些功能.
我在表示对象id的文本文件中有一些字符串,有时id是long而另一个是Guid,我需要解析它们.
快速回答是显而易见的,例如尝试解析Guid,如果失败,尝试解析一个long,但是.有没有什么好方法(优雅的解决方案)来知道某个字符串是长字还是Guid?
ee02b525-9755-4a07-a46d-37bb5b060fd2 (Guid)
-48765 (long)
Run Code Online (Sandbox Code Playgroud)
提前致谢.
在这里没有得到我的问题的详细答案.我想我会从不同角度解决它.
有人能够解释用于确定C99固定宽度整数类型的基础类型的选择标准:
[u]int_fast[n]_t
[u]int_least[n]_t
[u]int[n]_t
Run Code Online (Sandbox Code Playgroud)
对于给定的处理器,如果'long'和'int'具有相同的大小(sizeof(int)== sizeof(long)),那么为什么'long'将被用于'int',反之亦然.
我试图在嵌入式C/C++环境中进行一些简单的变量操作.由于某种原因,我一直在得到零.
这是代码
void Heater::setPower( int newpower )
{
printf("1 new power %d\n",newpower);
if(newpower != power)
power = newpower;
else
return;
printf("2 new power %d\n",power);
// Set Duty
long unsigned int newduty = 0;
// Protect from divide by zero
if(power <= 0)
{
printf("2.5 zeroed\n");
newduty = 0;
power = 0;
}
else
newduty = period*(power/100);
printf("3 setduty %lu period %lu\n", newduty, period);
setDuty(newduty);
}
Run Code Online (Sandbox Code Playgroud)
这是我收到的输出
1 new power 76
2 new power 76
3 setduty 0 period 10000000
Run Code Online (Sandbox Code Playgroud)
所以我知道收到的号码是76.我知道它已经超过了第一个障碍,第二个障碍.但不知何故,在简单的数学方程式中,它变为零."period"也是一个长的unsigned …
我正在创建一个大的整数类。我读了两个long作为函数参数,我想做的是让long读入的每个数字都占用一个数组的索引。
通过将long转换为字符串,然后将其转换为char数组并将它们临时存储在某个地方,可以做到这一点。这可以正常工作,并且在打印出来时可以像读入的数字一样打印出来。现在,我要做的就是将它们现在添加到新数组中,并将其数据类型设置为long。我已经做了两个数组来处理这个问题。
问题是当我尝试将char转换为long时,它给了我完全不同的价值。似乎是将char转换为自己的数字?字符类似乎没有任何转换为long的方法。
最好的方法是什么?
编辑:看来,如果我将长数组更改为int数组,然后使用Character.getNumericValue(char ch),则可以正常将其添加到数组中。
由于我计划在此函数的结尾处返回一个long值,因此我应该确保这些数组对安全性很久吗?还是将它们存储为int数组?谢谢
public static long hugeMultiplication(long value1, long value2){
System.out.println("originalvalue: "+value1);
int lengthOfWordOne = String.valueOf(value1).length();
int lengthOfWordTwo = String.valueOf(value1).length();
System.out.println("length1: "+lengthOfWordOne);
System.out.println("length2: "+lengthOfWordTwo);
long[] numberOne = new long[lengthOfWordOne+1];
long[] numberTwo = new long[lengthOfWordTwo+1];
//make those longs into string to convert char array
char[] tempValueOne = String.valueOf(value1).toCharArray();
char[] tempValueTwo = String.valueOf(value2).toCharArray();
//copy each value of a char array to long array and change to long again
//this will set up the array having each …Run Code Online (Sandbox Code Playgroud) 第一行代码不会抛出异常,但是第二行(它指定long的最大值)会抛出RuntimeException.有人可以澄清这种混乱吗?谢谢.
long val1 = Long.MAX_VALUE;
long val2 = 9223372036854775807;
Run Code Online (Sandbox Code Playgroud) 所以我最近编写了以下代码:
import java.util.Scanner;
public class TrainTicket
{
public static void main (String args[])
{
Scanner money = new Scanner(System.in);
System.out.print("Please type in the type of ticket you would like to buy.\nA. Child B. Adult C. Elder.");
String type = money.next();
System.out.print("Now please type in the amount of tickets you would like to buy.");
int much = money.nextInt();
int price = 0;
switch (type)
{
case "A":
price = 10;
break;
case "B":
price = 60;
break;
case "C":
price = …Run Code Online (Sandbox Code Playgroud) 表达式之前(long)和(int)之后的语句有什么意义?
long years = (long) (minutes / minutesInYear);
int remainingDays = (int) (minutes / 60 / 24) % 365;
Run Code Online (Sandbox Code Playgroud) 我试图对Java中的两个64位Long变量执行XOR操作。问题是,当我在变量中添加超过16位时,它将失败。
例如,这有效并返回7:
Long h1 = Long.parseLong("1100001101001101");
Long h2 = Long.parseLong("1100001101000001");
System.out.println(Long.bitCount(h1 ^ h2));
Run Code Online (Sandbox Code Playgroud)
如果我将h1和h2的值增加到:
Long h1 = Long.parseLong("11000110000110100110101101001101");
Long h2 = Long.parseLong("11000011100001101001101101000001");
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
例外在线程“主” java.lang.NumberFormatException:对于输入字符串: “11000110000110100110101101001101”
在
java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
在java.lang.Long.parseLong(Long.java:592)
在java.lang.Long.parseLong(Long.java:631)
如果我加倍(64位要计算的),则相同:
Long h1 = Long.parseLong("1100011100011000011010011010110100110110000110100110101101001101");
Long h2 = Long.parseLong("1100001110001100001101001101011010011011100001101001101101000001");
Run Code Online (Sandbox Code Playgroud)
关于为什么为什么失败超过16位有帮助吗?
long-integer ×10
java ×7
int ×4
c ×2
.net ×1
arrays ×1
biginteger ×1
bitcount ×1
c# ×1
c99 ×1
embedded ×1
fixed-width ×1
guid ×1
largenumber ×1
parsing ×1
string ×1
types ×1
unsigned ×1
xor ×1