1. Non-recursive least common multiple and maximum Common Divisor
# Include <stdio. h> void main () {int A, B, num1, num2, temp; printf ("Please input num1 and num2 \ n"); scanf ("% d ", & num1, & num2); If (num1> num2) {A = num1; B = num2;} else {A = num2; B = num1;} while (B> 0) {temp = A % B; A = B; B = temp;} printf ("the maximum common divisor is % d \ n, and the minimum common multiple is % d \ n",, (num1 * num2)/);}
2. recursively calculate the maximum common approx.
// Recursively calculate the maximum common divisor # include <stdio. h> int gcd (int m, int N); int main () {int M, N; printf ("input M, N: \ n "); scanf ("% d", & M, & N); printf ("% d \ n", gcd (m, n);} int gcd (INT m, int N) {If (M> N) // if it is greater than or less than "<" or ">", two return gcd (m-n, n) are not required ); else if (M <n) return gcd (m, n-m); else if (M = N) return m ;}
3. String word Inversion
// String transpose # include <string. h> # include <stdio. h> void reversion (char * Str) // method 1, using string. h library function {int n = strlen (STR)-1; // char * temp = (char *) malloc (n + 1); char temp [30] = ""; while (n> 0) {If (STR [N]! = '') & (STR [n-1] ='') {strcat (temp, STR + N-1); STR [N] = '\ 0 ';} N --;} strcat (temp, STR + n); printf ("% s \ n", temp);} void turn (char * Str) // method 2, use the {char temp; Int J = strlen (STR)-1, I = 0, begin, end; while (j> I) {temp = STR [I]; STR [I] = STR [J]; STR [J] = temp; j --; I ++;} printf ("% s \ n", STR ); I = 0; while (STR [I]) {If (STR [I]! = '') {Begin = I; while (STR [I] & STR [I]! = '') I ++; end = I-1;} while (end> begin) {temp = STR [begin]; STR [begin] = STR [end]; STR [end] = temp; end --; begin ++;} I ++;} printf ("% s \ n", STR) ;} void main () {char STR [30] = "Hello World Future"; reversion (STR); char str1 [] = "Ni hao a"; turn (str1 );}
4. Determine whether the address is low or high.
# Include <stdlib. h> # include <stdio. h> void main () {int A = 10; short B; memcpy (& B, & A, 2 ); // assign the lower two bytes of A to B printf ("% d \ n", B );}
5. String flip
#include <stdio.h>#include <string.h>void rotate(char *start, char *end){ while(start != NULL && end !=NULL && start<end) { char temp=*start; *start=*end; *end=temp; start++; end--; }}void leftrotate(char *p,int m){ if(p==NULL) return ; int len=strlen(p); if(m>0&&m<=len) { char *xfirst,*xend; char *yfirst,*yend; xfirst=p; xend=p+m-1; yfirst=p+m; yend=p+len-1; rotate(xfirst,xend); rotate(yfirst,yend); rotate(p,p+len-1); }}int main(void){ char str[]="abcdefghij"; leftrotate(str,3); printf("%s\n",str); return 0;}
6. Determine the system's large-end and small-end Storage
#include<stdio.h>union s{int i;char ch;}c;int check(){c.i=1;return (c.ch);}void main(){if (check()){printf("little\n");}elseprintf("big\n");}
7. The question of probability is that a square with a side length of 10 overlaps with a circle with a radius of 10 and the answer is about 25 π.
#include<stdio.h>#include<time.h>void main(){ int count=0,i=100; srand(time(0)); while(i>0) { int a=rand()%10; int b=rand()%10; if((a*a+b*b)<=100) count++; i--; } printf("%d",count);}
8, array a [n], stored 1 to the number of N-1, where a Number repeat once, find the number of repeated
#include<iostream> using namespace std; void do_dup(int a[] , int n) { int *b = new int[n]; for( int i = 0 ; i != n ; ++i ) { b[i] = -1 ; } for( int j = 0 ; j != n ; ++j ) { if( b[a[j]] == -1 ){ b[a[j]] = a[j] ; } else { cout << b[ a[j] ] << endl ; break ; } } } int main(){ int a[]={ 1 , 2 , 3 , 4 , 3 } ; do_dup( a , 5 ) ; }
9. Use two threads to implement 1-Output
Package lzf. thread; // use two threads to implement 1-output public class synchronized {// state = 1 indicates thread 1 starts printing, state = 2 indicates that thread 2 starts printing Private Static int state = 1; Private Static int num1 = 1; Private Static int num2 = 1; public static void main (string [] ARGs) {final synchronized T = new synchronized (); New thread (New runnable () {@ overridepublic void run () {While (num1 <95) {// both threads use the t object as the lock to ensure that only one thread prints synchronized (t) during each alternation {// If stat E! = 1, indicating that it is not the turn of thread 1 to print at this time. Thread 1 will call T's wait () method until it is awakened next time if (State! = 1) {try {T. wait ();} catch (interruptedexception e) {e. printstacktrace () ;}// when the State is 1, it is the turn of thread 1 to print the number five times for (Int J = 0; j <5; j ++) {system. out. println ("num1:" + num1); num1 + = 1; num2 = num1;} // After thread 1 is printed, assign the state value to 2, the next step is to print state = 2 in thread 2; // The notifyall () method is used to wake up thread 2 in wait on T. At the same time, thread 1 will exit the synchronization code block and release t lock T. policyall ();}}}}). start (); New thread (New runnable () {@ overridepublic void run () {While (num2 <100) {synchronized (T) {If (State! = 2) {try {T. wait ();} catch (interruptedexception e) {e. printstacktrace () ;}}for (Int J = 0; j <5; j ++) {system. out. println ("num2:" + num2); num2 + = 1; num1 = num2;} state = 1; T. policyall ();}}}}). start ();}}
Linux thread example
Gcc-O pthread_test. C-lpthread
#include<stddef.h>#include<stdio.h>#include<unistd.h>#include"pthread.h"void reader_function(void);void writer_function(void);char buffer;int buffer_has_item=0;pthread_mutex_t mutex;main(){ pthread_t reader; pthread_mutex_init(&mutex,NULL); pthread_create(&reader,NULL,(void*)&reader_function,NULL); writer_function();}void writer_function(void){ while(1) { pthread_mutex_lock(&mutex); if(buffer_has_item==0) { buffer='a'; printf("make a new item\n"); buffer_has_item=1; } pthread_mutex_unlock(&mutex); }}void reader_function(void){ while(1) { pthread_mutex_lock(&mutex); if(buffer_has_item==1) { buffer='\0'; printf("consume item\n"); buffer_has_item=0; } pthread_mutex_unlock(&mutex); }}
Thread simulation train ticket sales
package lzf.thread;class TicketSystem {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubSellThread st = new SellThread();new Thread(st).start();try{Thread.sleep(10);}catch (Exception e) {// TODO: handle exceptione.printStackTrace();}new Thread(st).start();st.b = true;}}class SellThread implements Runnable{int tickets = 100;Object obj = new Object();boolean b = false;@Overridepublic void run() {// TODO Auto-generated method stubif(b==false){while(true){sell();}}while (true) {synchronized (this) {if(tickets>0){try{Thread.sleep(1);}catch (Exception e) {// TODO: handle exceptione.printStackTrace();}System.out.println("obj"+Thread.currentThread().getName()+" sell tickets:"+tickets);tickets--;}}}}public synchronized void sell(){if(tickets>0){try{Thread.sleep(10);}catch (Exception e) {// TODO: handle exceptione.printStackTrace();}System.out.println("sell"+Thread.currentThread().getName()+" sell tickets:"+tickets);tickets--;}}}
10. macro definition interchange two numbers
// Type 1 # define swap (x, y) (x) = (x) + (y), (y) = (x)-(y), (X) = (x)-(y) // The second type # define swap (x, y) (x) = (x) ^ (y), (y) = (x) ^ (y), (x) = (x) ^ (y) // better than the previous one, no overflow problem with large numbers # define swap (x, y) // with line breaks x = x + y;/y = x-y;/x = x-y; # define swap (x, y)/x ^ = y;/y ^ = x;/x ^ = y; void main () {int x = 3, y = 4; swap (x, y); printf ("% d, % d", x, y );}