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");
}
else
printf("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() {
@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\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 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);
}