以下代码使用哪种算法/公式?
/**
* Computes the nth digit of Pi in base-16.
*
* If n < 0, return -1.
*
* @param n The digit of Pi to retrieve in base-16.
* @return The nth digit of Pi in base-16.
*/
public static int piDigit(int n) {
if (n < 0) return -1;
n -= 1;
double x = 4 * piTerm(1, n) - 2 * piTerm(4, n) -
piTerm(5, n) - piTerm(6, n);
x = x - Math.floor(x);
return …Run Code Online (Sandbox Code Playgroud) public class Person {
public String firstName, lastName;
public Person(String firstName,
String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFullName() {
return(firstName + " " + lastName);
}
}
Run Code Online (Sandbox Code Playgroud)
public class PersonTest {
public static void main(String[] args) {
Person[] people = new Person[20]; //this line .
for(int i=0; i<people.length; i++) {
people[i] =
new Person(NameUtils.randomFirstName(),
NameUtils.randomLastName()); //this line
}
for(Person person: people) {
System.out.println("Person's full name: " +
person.getFullName());
}
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我们使用了两次"new".这段代码是正确还是错误?第一个用于分配数组.但为什么第二个呢?这是来自讲义.
在网上搜索并阅读了stackoverflow上的问题答案后,我找不到我的问题的答案。
该等待在线程B中被调用,它解锁互斥锁,从而允许其他人访问条件变量(用于发出信号)。然后,当在线程A中发出条件变量的信号时,线程B唤醒,线程B再次锁定互斥锁。
当线程A通过信号通知条件变量唤醒线程B时,线程B被唤醒,请锁定互斥锁。
我从教程中了解到,每当发出线程B信号时,它就会立即锁定互斥量。但是,示例与此相反,当线程B被信号通知并唤醒后,线程A继续执行,有时线程B锁定互斥锁。
我的问题是线程B(在下面的示例中为消费者)何时锁定互斥锁?就在线程B接收信号的那一刻?还是取决于线程调度程序?
我没有找到任何解释。
我正在考虑以下示例的情况:
#include <pthread.h>
#include <stdio.h>
pthread_mutex_t mutex;
pthread_cond_t cond;
int buffer[100];
int loops = 5;
int length = 0;
void *producer(void *arg) {
int i,j;
for (i = 0; i < loops; i++) {
pthread_mutex_lock(&mutex);
buffer[length++] = i;
printf("producer length %d\n", length);
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
}
void *consumer(void *arg) {
int i;
for (i = 0; i < loops; i++) {
pthread_mutex_lock(&mutex);
while(length == 0) {
printf(" consumer waiting...\n");
pthread_cond_wait(&cond, &mutex);
}
int item …Run Code Online (Sandbox Code Playgroud) 我有一些函数(charfreq,wordfreq,charcount,wordcount,parerror),我想在dataStructure中使用给定的字符串.但我怎么能这样做?我正在尝试这些和许多方式,但我得到了所有的错误.谢谢你提前.
data StrStat = StrStat { charfreq :: [( Char , Int )]
, wordfreq :: [([ Char ] , Int )]
, charcount :: Int
, wordcount :: Int
, parerror::Maybe Int
}
analyze :: [Char] -> StrStat
analyze x = StrStat { charfreq = (charfreq x) {-this line gives error-}
, wordfreq = (wordfreq x)
, charcount = (charcount x)
, wordcount = (wordcount x)
, parerror = (parerror x)
}
Run Code Online (Sandbox Code Playgroud)
错误信息是: Syntax error in input (unexpected `=') …
Animal是Cat类的父类.
public static void main (String[] args)
{
Animal myCat1 = new Cat; //Allocation 1
Cat myCat2 = new Cat; //Allocation 2
}
Run Code Online (Sandbox Code Playgroud)
两个分配的差异是什么?每个都是真的,不是吗?
正如标题所述,我无法理解循环,并提出了一种简单的1到100总和的方法,但就像我说的那样,循环让我有些困惑.我想我有FOR循环.
这就是我想出来的.
int sum = 0;
for (int i = 1; i <= 100; i++) sum += i;
System.out.println("The sum is " + sum);
Run Code Online (Sandbox Code Playgroud)
我有一些奇怪的问题.我正在研究一个简单的程序,该程序应该通过网络将从stdin获取的数据发送到该程序的另一个实例,该程序应该将数据直接发送到stdout.但它没有按预期工作,只发送了一部分数据.在那之后,我设法找出了从stdin读取的问题:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define SIZE 1024
int main(void) {
char *buffer;
int read_bytes;
buffer = (char *) malloc(sizeof(char)*SIZE);
while ((read_bytes = read(STDIN_FILENO,buffer,SIZE)) == SIZE) {
write(STDOUT_FILENO,buffer,SIZE);
fprintf(stderr,"read %d bytes\n",read_bytes);
}
fprintf(stderr,"read %d bytes\n",read_bytes);
write(STDOUT_FILENO,buffer,read_bytes);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
(这只是为了简单地说明问题,而不是原始代码的实际部分)
它一切正常( - >将数据从stdin重新发送到stdout),直到我从花费更多时间的东西中获取,例如:
find / | ./the_programme > output.file
Run Code Online (Sandbox Code Playgroud)
输出在几千字节后停止,我真的很困惑为什么(它肯定没有完成).我尝试使用fcntl清除STDIN_FILENO上的O_NONBLOCK标志,但它根本没有帮助.我可能在这里遗漏了一些非常基本的东西,但无论是man page还是google都没有帮助我.
当我在ghci中编译我的代码时,没有问题.它可以正确编译.但是,如果我尝试在拥抱中编译它,我会收到错误"编译代码太复杂".我认为问题是由于许多|条件造成的.
如果我将其更改为使用if/else,则没有问题.我可以添加if/else语句100次,但这将非常烦人和恼人.而不是那样,我试图在20-30 |条件之后放置if/else语句,但是|如果语句如下所示我无法进行工作:
f x y z
| cond1 = e1
| cond2 = e2
...
if (1)
then
| cond30 = e30
| cond31 = e31
...
else
| cond61 = e61
| cond62 = e62
Run Code Online (Sandbox Code Playgroud)
如何以最少的努力修复代码?完整的代码在hpaste上,因为它比StackOverflow的问题大小限制更长.
在以下代码中,我创建了两个作业,然后取消了父作业。我希望子作业会被取消,但是,子作业不会被取消,它们仍然处于活动状态。为什么?
注意:我知道通过创建 Job() 对象,然后将作业对象提供给协程上下文,然后取消 Job() 对象,可以轻松解决此问题。我的问题是为什么下面的代码不起作用?如果我想取消父作业,是否必须使用显式父参数?
fun main() {
// CoroutineScope
val coroutineScope = CoroutineScope(Dispatchers.IO)
var child1 : Job? = null
var child2 : Job? = null
// Use
val currentJob = coroutineScope.launch {
child1 = coroutineScope.launch {
delay(500)
}
child2 = coroutineScope.launch {
delay(500)
}
}
Thread.sleep(300L)
currentJob.cancel()
println("Job 1 state: ${child1?.status()}")
println("Job 2 state: ${child2?.status()}")
println("Parent job is active: ${coroutineScope.isActive}")
println("Parent job is active: ${currentJob.isActive}")
Thread.sleep(2000L)
}
Run Code Online (Sandbox Code Playgroud)
输出 :
Job 1 state: Active
Job 2 state: Active …Run Code Online (Sandbox Code Playgroud) 我有一个int对列表,如:
[(1,2),(3,5),(0,1),(1,3),(3,0),(0,3)]
Run Code Online (Sandbox Code Playgroud)
我想订购从最小的一对到最大的这个列表.对于上面的例子,它应该是:
[(0,1),(0,3),(1,2),(1,3),(3,0),(3,5)].
Run Code Online (Sandbox Code Playgroud)
您可以假设给定输入中的列表中没有相同的对.我该怎么做?