いくつかの筆記試験のコード

10673 ワード

1、非再帰的に最小公倍数と最大公約数を求める
 
#include<stdio.h>
void main()
{
int a,b,num1,num2,temp;
printf("please input num1 and num2 
"); scanf("%d%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(" %d
%d
",a,(num1 * num2) / a); }
 
 
2、再帰的に最大公約数を求める
 
//         
#include<stdio.h>
int gcd(int m,int n);
int main()
{
  int m,n;
 printf("Input m,n:
"); scanf("%d%d",&m,&n); printf("%d
",gcd(m,n)); } int gcd(int m,int n) { if(m>n)// "<" ">" , return gcd(m-n,n); else if(m<n) return gcd(m,n-m); else if(m==n) return m; }
 
3、文字列単語逆置き問題
 
//     
#include<string.h>
#include<stdio.h>
void Reversion(char *str)//   ,  string.h   
{
    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
",temp); } void turn(char *str)// , { 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
",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
",str); } void main() { char str[30]="hello world future"; Reversion(str); char str1[]="ni hao a"; turn(str1); }
 
 
4、低い住所か高い住所かを優先する
 
#include<stdlib.h>
#include<stdio.h>
void main()
{
    int a=10;
    short b;
    memcpy(&b,&a,2);// a        b
    printf("%d
",b); }
 
 
5、文字列の反転
 
#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
",str); return 0; }
 
6、判断システムの大端小端記憶
 
#include<stdio.h>
union s{
int i;
char ch;
}c;
int check()
{
c.i=1;
return (c.ch);
}
void main()
{
if (check())
{
printf("little
"); } else printf("big
"); }
 
7、一つの確率を求める問題、すなわち一つの辺の長さが10の正方形と一つの半径が10の円が重なる部分の答えは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、配列a[N]は、1からN-1の数を格納し、そのうちある数を1回繰り返し、繰り返した数を探し出す
 
 
#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、2つのスレッドで1-100の出力を実現する
 
package lzf.thread;
//       1-100   
public class Synchronized {
	// state==1    1    ,state==2    2    
	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() {
			@Override
			public void run() {
				while (num1 < 95){
					//       t     ,                 
					synchronized (t) {
						//   state!=1,           1  ,   1   t wait()  ,        
						if (state != 1) {
							try {
								t.wait();
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
						//  state=1 ,     1  5   
						for (int j = 0; j < 5; j++) {
							System.out.println("num1:"+num1);
							num1 += 1;
							num2 = num1;
						}
						//   1     ,  state   2,           2  
						state = 2;
						// notifyAll()     t wait   2,     1        ,   t 
						t.notifyAll();
					}
				}
			}
		}).start();

		new Thread(new Runnable() {
			@Override
			public 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.notifyAll();
					}
				}
			}
		}).start();
	}
}
linuxの下のスレッドの例
gcc -o pthread_testpthread_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
"); 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
"); buffer_has_item=0; } pthread_mutex_unlock(&mutex); } }
 
スレッドシミュレーション列車の切符販売
 
package lzf.thread;

class TicketSystem {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SellThread st = new SellThread();
		new Thread(st).start();
		try{
			Thread.sleep(10);
		}
		catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		new Thread(st).start();
		st.b = true;
		
	}

}

class SellThread implements Runnable{
	int tickets = 100;
	Object obj = new Object();
	boolean b = false;
	@Override
	public void run() {
		// TODO Auto-generated method stub
		if(b==false){
			while(true){
				sell();
			}
		}
		while (true) {
			synchronized (this) {
				if(tickets>0){
					try{
						Thread.sleep(1);
					}
					catch (Exception e) {
						// TODO: handle exception
						e.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 exception
				e.printStackTrace();
			}
			System.out.println("sell"+Thread.currentThread().getName()+" sell tickets:"+tickets);
			tickets--;
		}
	}
}
 
10、マクロ定義は二つの数字を交換する
 
//   
#define SWAP(x,y) ((x)=(x)+(y),(y)=(x)-(y),(x)=(x)-(y))
//   
#define SWAP(x,y) ((x)=(x)^(y),(y)=(x)^(y),(x)=(x)^(y))//      ,            
#define swap(x, y)/
//    
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);
}