/***************************************
* Function:
* 反置二进制数
* Lnstree by 2012
****************************************/
#include <stdio.h>
unsigned int reverse_bit(unsigned int a);
int main()
{
printf("%ld", reverse_bit(1));
}
unsigned int reverse_bit(unsigned int value)
{
unsigned int answer;
unsigned int i;
answer = 0;
for (i = 1; i != 0; i <<= 1)
{
answer <<= 1;
if (value & 1)
answer |= 1;
value >>= 1;
}
return value;
}
问题:
编写一个函数给定一个链表将表中最大数据移到链表尾
重点: 当数据位于表头时,要修改表头指针
/***************************************
* Function:
* 编写一个函数给定一个链表将表中最大
* 数据移到链表尾
* Lnstree by 2012
****************************************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct node
{
int data;
struct node* next;
}*link;
// 初始化10个节点链表
link initiate()
{
int i;
link cu,t,p = NULL;
srand(time(NULL));
for (i = 0; i < 10; i++)
{
t = (link)malloc(sizeof(link));
t -> data = rand() % 100 + 1;
if (p == NULL)
{
p = t;
p ->data = 100; // 用于测试当表头为最大值时
cu = t;
}
else
{
cu ->next = t;
cu = cu ->next;
cu ->next = NULL;
}
}
return p;
}
// 遍历一个链表
void printlink(link p)
{
link m = p;
while (m != NULL)
{
printf("%d ", m->data);
m = m->next;
}
}
// 销毁链表
void destroy(link p)
{
link c = p;
while (p -> next != NULL)
{
c = p ->next;
free(p);
p = c;
}
}
// 寻找链表中最大项移到链表尾
void findMax(link &p)
{
int temp;
link tm, tn;
tn = tm = p;
while (tn ->next != NULL)
{
if(tn ->next ->data > tm -> next ->data)
{
tm = tn;
}
tn = tn ->next;
}
// 假如最大值存在表头则修改表头,否则将tm指向最小的数据项移到表末
if (tm -> next ->data > p ->data)
{
tn ->next = tm->next;
tm ->next = tm->next->next;
tn = tn ->next;
tn ->next = NULL;
}
else
{
tm = p;
p = p ->next;
tn ->next = tm;
tm ->next = NULL;
}
}
// 寻找最大项节点移到该表最后
void changeMak (link p)
{
link tm,tn;
tm ->next = p;
}
int main()
{
link p = NULL;
p = initiate();
printlink(p);
findMax(p);
printf("\n");
printlink(p);
destroy(p);
return 0;
}
问题:编写一个函数给定一个链表将表中最大数据移到链表头
/***************************************
* Function:
* 将给定的链表最小项移到表头
* Lnstree by 2012
****************************************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct node
{
int data;
struct node* next;
}*link;
// 初始化10个节点链表
link initiate()
{
int i;
link cu,t,p = NULL;
srand(time(NULL));
for (i = 0; i < 10; i++)
{
t = (link)malloc(sizeof(link));
t -> data = rand() % 100 + 1;
if (p == NULL)
{
p = t;
cu = t;
}
else
{
cu ->next = t;
cu = cu ->next;
cu ->next = NULL;
}
}
return p;
}
// 遍历一个链表
void printlink(link p)
{
link m = p;
while (m != NULL)
{
printf("%d ", m->data);
m = m->next;
}
}
// 销毁链表
void destroy(link p)
{
link c = p;
while (p -> next != NULL)
{
c = p ->next;
free(p);
p = c;
}
}
// 寻找链表中最小项移到链表头
void findMin(link &p)
{
int temp;
link tm, tn;
tm = p;
tn = tm;
while (tn ->next != NULL)
{
if(tn ->next ->data < tm -> next ->data)
{
tm = tn;
}
tn = tn ->next;
}
if (tm -> next ->data < p ->data)
{
tn = tm ->next;
tm ->next = tm ->next ->next;
tn ->next = p;
p = tn;
}
}
int main()
{
link p = NULL;
p = initiate();
printlink(p);
findMin(p);
printf("\n");
printlink(p);
destroy(p);
return 0;
}
问题:求平方根
/***************************************
* Function:
* 近似输出平方根
* Lnstree by 2012
****************************************/
#include <stdio.h>
int main ()
{
float a,b,c;
float pr = 1.0f;
scanf("%f", &a);
b = 1.0f;
while (1)
{
c = (float)((b + a/b)/2);
if (c == b)
break;
b = c;
}
printf("%.5f", c);
return 0;
}
问题:打印质数
/***************************************
* Function:
* 打印1~100质数
* Lnstree by 2012
****************************************/
#include <stdio.h>
#include <math.h>
int main (void)
{
int i,j;
int a = 0;
for (i = 2; i <= 100; i++)
{
for (j = 3; j < sqrt(i); j++)
{
if (i % j == 0)
a = 1;
}
if (a == 0)
{
printf("%d ", i);
}
a = 0;
}
}
/***************************************
* Function:
* 小写转大写
* Lnstree by 2012
****************************************/
#include <stdio.h>
int main()
{
char ch;
char a = -33;
while ((ch = getchar()) != EOF)
{
ch = ch & a;
putchar(ch);
}
return 0;
}
/***************************************
* Function:
* 5.1例程 计数数值二进制中1个数
* Lnstree by 2012
****************************************/
#include <stdio.h>
int count_one_bits(unsigned value);
int main()
{
printf("%d", count_one_bits(11));
return 0;
}
int count_one_bits(unsigned value)
{
int ones;
/*
** 当这个值还有一些值为1时
*/
for (ones = 0; value != 0; value = value >> 1)
{
/*
** 如果最低位的值为1,计数增1
*/
if (value % 2 != 0)
{
ones++;
}
}
return ones;
}