一些笔试的代码
来源:互联网 发布:2017网络项目 编辑:程序博客网 时间:2024/04/30 08:26
1、非递归求最小公倍数和最大公约数
#include<stdio.h>void main(){int a,b,num1,num2,temp;printf("please input num1 and num2 \n");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\n最小公倍数是%d\n",a,(num1 * num2) / a);}
2、递归求最大公约数
//用递归求最大公约数#include<stdio.h>int gcd(int m,int n);int main(){ int m,n; printf("Input m,n:\n"); scanf("%d%d",&m,&n); printf("%d\n",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\n",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\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、判断低地址还是高地址优先
#include<stdlib.h>#include<stdio.h>void main(){ int a=10; short b; memcpy(&b,&a,2);//将a的低两字节赋值给b printf("%d\n",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\n",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\n");}elseprintf("big\n");}
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个数,其中某个数重复一次,找出重复的那个数
#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、用两个线程实现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() {@Overridepublic 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() {@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.notifyAll();}}}}).start();}}
linux下线程示例
gcc -o pthread_test 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); }}
线程模拟火车售票
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、宏定义交换两个数字
//第一种#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);}
- 一些笔试的代码
- 整理一些笔试题(要求手写代码的)
- 笔试的一些题目 .
- 笔试的一些题目
- 笔试的一些题目
- 整理一些笔试题(要求手写代码的) 2011年以前的
- 一些公司的笔试题
- 一些公司的笔试题
- Microsoft的一些笔试题
- C的一些笔试题
- 一些笔试题的复习
- 遇到的一些笔试题
- Unity的一些笔试题
- Unity的一些笔试题
- 一些c++的笔试题
- Unity的一些笔试题
- 整理的一些笔试题
- 笔试的一些小总结
- [Python]计算100以内的素数--一行代码filter方式
- ubuntu 安装中文输入法
- 生成组合和排列
- tomcat 解析包含中文的cookie 抛警告源码分析
- 关于安卓高版本访问网络出现.NetworkOnMainThreadException异常的问题
- 一些笔试的代码
- android back键处理
- 蛇形线走线注意要点
- Discuz论坛防止广告机的方法
- 关于设置X-UA-Compatible来设置IE浏览器兼容模式时无效问题
- Win7上配置Cocos2dx开发环境
- spring+springmvc+openjpa的逆向工程整合出错
- Android中Back键与Home键的区别
- 第五周上级项目三