(Google interview question) There are four threads 1, 2, 3, and 4. The function of thread 1 is to output 1, the function of thread 2 is to output 2, and so on… There are now four files ABCD. Initially they are empty.

Now we want the four files to be in the following format: A:1 2 3 4 1 2…. B:2 3 4 1 2 3…. C:3 4 1 2 3 4…. D:4 1 2 3 4 1…. Please design a program. The following is an example of A. For B, C, and D, you only need to modify the initialization value of the global variable n: 1 #include 2 #include 3 #include 4 using namespace std; 5 6 pthread_mutex_t myloack=PTHREAD_MUTEX_INITIALIZER; 7 pthread_cond_t mycOnd=PTHREAD_COND_INITIALIZER; 8 int n=0; 9 void *ThreadFunc(void *arg) 10 { 11 int num=(int )arg; 12 for (int i = 0; i <10 ; ++i) 13 { 14 pthread_mutex_lock(&myloack); 15 while (n!=num) 16 pthread_cond_wait(&mycond,&myloack); 17 18 if (num==0) 19 cout<<“1”; 20 else if(num==1) 21 cout<<“2”; 22 else if(num==2) 23 cout<<“3”; 24 else 25 cout<<“4″<<endl; 26 n=(n+1)%4; 27 pthread_mutex_unlock(&myloack); 28 pthread_cond_broadcast(&mycond); 29 } 30 return (void *)0; 31 } 32 33 int main(int argc, char const *argv[ ]) 34 { 35 36 pthread_t id[4]; 37 for (int i = 0; i <4 ; ++i) 38 { 39 int err=pthread_create(&id[i],NULL,ThreadFunc,(void *)i); 40 if (err!=0) 41 { 42 cout<<“create err:”<<endl; 43 exit(-1); 44 } 45 46 } 47 48 for (int i =…

Java-2-Learning Process 2: Basic Knowledge 1, 2, 3 documents, full version video resources, e-book download

 Java learning process: basic knowledge 1, 2, 3 documents, full version video resources, e-books 1. Basic knowledge 1, 2 and 3 can be downloaded from the following address: http://download.csdn.net/detail/iot_li/8868361 2. Full version video resources: 3. Electronic books: Special note: Since the capacity of learning videos and e-books is too large, those who want it can select some of the materials, leave an email, and I will send them to you. Of course, everyone is welcome to choose, study hard and make progress every day!

java11235_Use java to calculate the sum of the 120th digit of 1,1,2,3,5,8…

Use java to calculate the sum of the numbers 1, 1, 2, 3, 5, 8… 1-20th digits Attention: 254 Answer: 4 mip version Solution time 2021-01-27 00:01 The questioner is sorry and sorry 2021-01-26 16:29 Use java to calculate 1,1,2,3,5 ,8…The sum of the 1-20th digits Best answer Second-level knowledge expert Takaki Nono 2021-01-26 16:54 public class Test1 { public static void main(String args[]) { int a[] = new int[20]; a[0] = a[1] = 1; int sum = a[0] + a[1]; for (int i = 2; i <20; i++) { a[i] = a[i – 2] + a[i – 1]; sum += a[i]; } System.out.println(” The sum of the first 20 digits is :” + sum); } } The result is :17710 Answer all 1F Liuli Zhishi 2021-01-26 19:27 If you observe the rules of numbers, you will know that except for the first number And the second number is unexpected, the other numbers are equal to the first 2 numbers, and I wrote a function to see if you want to see it, but I didn’t pay much attention to the function name (I am a novice) //———– ——–Function class——————————— package com.yaojian.com; public class hanshu { public int feibulaji(int n){…

JavaScript essential reading notes (1,2)

Chapter 1 Highlights Some of Javascript’s features are more trouble than they’re worth. Among them, some features are because the specification is very imperfect, which may cause portability problems; some features lead to the generation of code that is difficult to understand and modify; some features make my coding style overly complex and error-prone; and some features are just Wrong design Chapter 1 Essence Some features of Javascript are more trouble than they are worth. Among them, some features are because the specification is very imperfect, which may lead to portability problems; some features lead to the generation of code that is difficult to understand and modify; some features make my coding style overly complex and error-prone; and some features are just Design errors. Sometimes language designers make mistakes. Most programming languages ​​have good parts and bad parts. I’ve found that I can become a better programmer if I only use the good parts and avoid the fluff. After all, how can you build something good out of crappy parts? It is almost impossible for a standards committee to remove defective parts of a language because doing so would undermine all the bad programs that depend on those weak parts.…

JavaScript essential reading notes (1,2)

Chapter 1 The Essence Some of the features of Javascript cause far more trouble than they are worth. Among them, some features are because the specification is very imperfect, which may lead to portability problems; some features lead to the generation of code that is difficult to understand and modify; some features make my coding style overly complex and error-prone; and some features are just Design errors. Sometimes language designers make mistakes. Most programming languages ​​have essential parts and weak parts. I’ve found that I can become a better programmer if I only use the good parts and avoid the fluff. After all, how can you build something good out of crappy parts? It is almost impossible for a standards committee to remove defective parts of a language because doing so would undermine all the bad programs that depend on those useless parts. There’s usually nothing they can do except pile more features on top of a pile of existing flaws. And new and old features don’t always coexist harmoniously, possibly resulting in more useless parts. However, you have the power to define your own subsets. You can write better programs based on the best parts. The proportion of useless…

How many implementation codes that are different from each other and do not have repeated numbers can be composed by Java1,2,3,4

Question: There are 1, 2, 3, and 4 numbers. How many different three-digit numbers can be formed without repeated numbers? How many are they? Program analysis: The numbers that can be filled in the hundreds, tens, and ones places are all 1, 2, 3, and 4. After composing all the permutations, remove the permutations that do not meet the conditions. Programming: public class Wanshu { public static void main(String[] args) { int i=0; int j=0; int k=0; int t=0; for(i=1;i<=4;i++) for(j=1;j<=4;j++) for(k=1;k<=4;k++) if(i!=j && j!=k && i!=k) {t+=1; System.out.println(i*100+j*10+k); } System.out.println (t); } }

Java-2-Learning Process 2: Basic Knowledge 1, 2, 3 documents, full version video resources, e-book download

 Java learning process: basic knowledge 1, 2, 3 documents, full version video resources, e-books 1. Basic knowledge 1, 2 and 3 can be downloaded from the following address: http://download.csdn.net/detail/iot_li/8868361 2. Full version video resources: 3. Electronic books: Special note: Since the capacity of learning videos and e-books is too large, those who want it can select some of the materials, leave an email, and I will send them to you. Of course, everyone is welcome to choose, study hard and make progress every day!

java11235_Use java to calculate the sum of the 120th digit of 1,1,2,3,5,8…

Use java to calculate the sum of the numbers 1, 1, 2, 3, 5, 8… 1-20th digits Attention: 254 Answer: 4 mip version Solution time 2021-01-27 00:01 The questioner is sorry and sorry 2021-01-26 16:29 Use java to calculate 1,1,2,3,5 ,8…The sum of the 1-20th digits Best answer Second-level knowledge expert Takaki Nono 2021-01-26 16:54 public class Test1 { public static void main(String args[]) { int a[] = new int[20]; a[0] = a[1] = 1; int sum = a[0] + a[1]; for (int i = 2; i <20; i++) { a[i] = a[i – 2] + a[i – 1]; sum += a[i]; } System.out.println(” The sum of the first 20 digits is :” + sum); } } The result is :17710 Answer all 1F Liuli Zhishi 2021-01-26 19:27 If you observe the rules of numbers, you will know that except for the first number And the second number is unexpected, the other numbers are equal to the first 2 numbers, and I wrote a function to see if you want to see it, but I didn’t pay much attention to the function name (I am a novice) //———– ——–Function class——————————— package com.yaojian.com; public class hanshu { public int feibulaji(int n){…

Java loop exercise: There are four numbers 1, 2, 3, and 4. How many three-digit numbers without repeating numbers can be formed? How many are they?

package practiceGO; /** There are four numbers 1, 2, 3, and 4. How many three-digit numbers without repeating numbers can be formed? How many are they? */ public class Cto { public static void main(String[] args) { int count = 0; for(int i=1; i<=4; i++){ for(int j=1; j<=4; j++){ for(int k=1; k<=4; k++){ if (i!=j && i!=k && j!=k) { System.out.println(i*100+j*10+k); count++; } } } } System.out.println("can form "+count+" non-repeating three-digit numbers"); } } Run result: 123 124 132 134 142 143 213 214 231 234 241 243 312 314 321 324 341 342 412 413 421 423 431 432 Can form 24 non-repeating three-digit numbers Java loop exercise: There are four numbers 1, 2, 3, and 4. How many non-repeating numbers can be formed? three digits? How many are they?

What do the square brackets before numbers mean in Java? For example []89 or [1, 2, 3]89

I’m new to Java and haven’t found any explanation about syntax like [1, 2, 3, 4] 5. import java.util.*; class SumDigPower { static List list = new ArrayList(); public static List sumDigPow(long a, long b) { for(long i = a; i<=b; i++) { if(isEureka(i)) { list.add(i); } } return list; } public static boolean isEureka(long num) { //convert number to string to get length and then sum each digit to the nth power //return true or false depending on whether the number qualifies for the list String numString = Long.toString(num); long sum = 0; for(int i = 0; i <numString.length(); i++) { sum += Math.pow(Character.getNumericValue(numString.charAt(i)), i+1); } if(sum == num) {return true;} else {return false;} } } Expected: , but got: 1> yshavit..: JUnit uses this syntax to display the reason why a test failed, especially when the expected String does not match the actual value: String expected = “[1, 2, 3, 4, 5, 6, 7, 8, 9, 89]”; List actualLOngs= Arrays.asList( 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 89L); String actualString = actualLongs.toString(); assertEquals(expected, actualString); JUnit finds similarities and differences between expected and actual values ​​and highlights them…

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索