天之道

享受编程的乐趣。
posts - 118, comments - 7, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

编写一个返回循环链表中节点数的函数

实现代码如下:

#include<stdio.h>
#include<stdlib.h>
typedef struct node *link;
struct node{ int item; link next; };

int node_number(link p,int n)
{
    int count=0,i;
    for(i=0;i<n-1;i++)
    {
        p=p->next;
    }
    while(p->item)
    {
        p->item=0;
        p=p->next;
        count++;
    }
    return count;
}

int main()
{
    
    int i,N;
    link t=(link)malloc(sizeof(node));
    t->item=1;
    t->next=t;
    link x=t;
    for(i=2;i<=10;i++)
    {
        x = (x->next= (link)malloc(sizeof(node)));
        x->item=i;
        x->next=t;
    }
    printf("Please input the order of node: ");
    scanf("%d",&N);
    printf("total number of nodes is: %d\n",node_number(t,N));
    return 0;
}

posted @ 2012-08-17 01:43 hoshelly 阅读(265) | 评论 (0)编辑 收藏

假设有N个人决定选出一个领导人,方法如下:所有人排成一个圆圈,按顺序数数,每隔第M个人出局,此时他两边的人靠拢重新形成圆圈。问题是找出哪一个人将会是最后剩下的那个人。我们希望打印出所有人的出局顺序和最后选出的领导人是哪一位。

这个问题称为约瑟夫问题,可以利用链表解决。

代码如下:

  //约瑟夫问题
  
  #include<stdio.h>
  #include<stdlib.h>
  typedef struct node *link;
  struct node { int item; link next; }; //定义结点
  int main()
  {
     int i,N,M;
     printf("Input N and M: "); //N表示共有N个人,M表示每隔第M个人要出局
     scanf("%d%d",&N,&M);
     link t = (link)malloc(sizeof(node)); //新建结点t
     link x=t; 
     t->item = 1; t->next=t; //创建一个代表1号的单个节点的循环链表
     for(i=2;i<=N;i++)
     {
         x=(x->next= (link)malloc(sizeof(node)));//将2~N号按序插到之前创建的单个节点的循环链表中
         x->item=i; x->next=t;
     }
 
     while(x!= x->next) //如果不是最后一个节点,因为是循环链表,所以x!=x->next
     {
         for(i=1;i<M;i++) //则顺着链表向前遍历,数出M-1个元素
             x=x->next;
         printf("%d ",x->next->item);
         x->next = x->next->next; //删除第M个元素
         N--; //节点数减1
     }
     printf("\n%d\n",x->item); //最后打印出最后一个节点
     return 0;
 }

posted @ 2012-08-17 00:14 hoshelly 阅读(1030) | 评论 (0)编辑 收藏

编写程序,对于N个随机产生的单位正方形中的点,统计可以被长度小于d的直线连结的点对数。

程序如下:

typedef structfloat x; float y; } point; //定义点的数据类型
float distance(point,point); //两点之间距离函数
float distance(point a,point b)

   float dx= a.x - b.x, dy= a.y - b.y;
   return sqrt(dx*dx + dy*dy);
}
//以上为头文件 Point.h 的内容



#include<stdio.h> 
#include<stdlib.h>
#include<math.h>
#include "Point.h"
float randFloat()
{  return 1.0*rand()/RAND_MAX; } //产生随机数的函数
int main()
{
    float d,N;
    int i,j,cnt=0;
    scanf("%f%f",&d,&N); //d为要求两点之间距离小于的长度,N为测试的点 
    point *a = (point *)malloc(sizeof(point)*N); //动态生成数据类型为point的数组a
    for(i=0;i<N;i++)
{
a[i].x = randFloat(); a[i].y = randFloat(); 
}
    for(i=0;i<N;i++)
        for(j=i+1;j<N;j++)
            if(distance(a[i],a[j])<d) //如果两点之间的距离小于d,那么cnt加1
                     cnt++;
    printf("%d edges shorter than %f\n",cnt,d); //输出有多少条边的长度小于d
    return 0;
}

posted @ 2012-08-14 21:39 hoshelly 阅读(296) | 评论 (0)编辑 收藏


//模拟抛硬币的实验

#include<stdio.h>
#include<stdlib.h>
int heads()   //返回0或非0值
{
    return rand() <RAND_MAX/2;
}

int main()
{
    int i,j,cnt;
    int N,M;
    scanf("%d%d",&N,&M); //抛一枚硬币N=32次,如此做M=1000次这样的实验
    int *f=(int *)malloc((N+1)*sizeof(int));
    for(j=1;j<=N;j++) //初始化数组全部为0值
        f[j]=0;
    for(i=1;i<M;i++,f[cnt]++)  //开始抛硬币,f[cnt]记录第cnt次抛硬币出现正面的次数
        for(cnt=1,j=1;j<=N;j++) //开始第一轮共32次的抛硬币实验
            if(heads())    cnt++; //如果出现正面,即heads()返回值为1,则对应着f[cnt]++,同时cnt++,此处利用数组索引统计正面出现次数,负面数组值始终为0
            
    for(j=1;j<=N;j++)
    {
        printf("%2d ",j);
        for(i=0;i<f[j];i+=10) printf("*"); //正面每出现十次打印一个星号
        printf("\n");
    }
    return 0;
}

posted @ 2012-08-14 10:37 hoshelly 阅读(812) | 评论 (0)编辑 收藏

// From < C Programming FAQs > 
找出所有小于10000的素数,算法原理请自行google 埃拉托色尼筛法

程序代码:

#define N 10000
#include<stdio.h>
int main()
{
    int i,j,a[N];
    for(i=2;i<N;i++) a[i]=1; //将数组中的值全部设为1
    for(i=2;i<N;i++)
        if(a[i])
            for(j=i;i*j<N;j++)   a[i*j]=0; //将索引为2,3,5,的倍数的数组元素设为0,因为这些数不是素数
    for(i=2;i<N;i++)
        if(a[i]) printf("4%d\n",i); //遍历打印出找到的素数
    printf("\n");
    return 0;
}

posted @ 2012-08-13 22:17 hoshelly 阅读(551) | 评论 (0)编辑 收藏

输入10个学生的成绩,编写一程序对学生的成绩按从高到低输出,要求用链表实现。

#include<stdio.h>
#include<stdlib.h>
struct Stu
{
    int score;
    struct Stu *next;
};
typedef struct Stu Node;
int main()
{
    int i;
    Node *head,*p,*q;
    head=(Node*)malloc(sizeof(Node)); //创建头结点
    if(head == NULL)
    {
        printf("Memory is not enough!");
        return 0;
    }
    head->next=NULL;
    for(i=0;i<10;i++)
    {
        p=(Node*)malloc(sizeof(Node)); //创建一个新结点p
        if(p == NULL)
        {
            printf("no enough memory!");
            return 0;
        }
        printf("Input the %dth student's score: ",i+1);
        scanf("%d",&p->score); //输入成绩
        q=head;
        while(q->next != NULL) //遍历链表
        {
            if(q->next->score < p->score) //如果发现链表中的某个成绩比当前输入成绩小,就跳出循环,在其前面插入当前输入成绩
                break;
            q=q->next; //继续遍历直到遍历的成绩比当前输入的成绩小
        }
        p->next=q->next; //这是当前成绩插入到链表中比其小的成绩前面的代码
        q->next=p;

    }
    p=head->next;
    while(p !=NULL)  
    {
        printf("%d ",p->score);
        p=p->next;
    }

p=head;
while(p->next !=NULL)
{
    q=p->next;
    p->next=q->next;
    free(q);
}
free(head);

return 0;
}

posted @ 2012-08-12 22:23 hoshelly 阅读(1974) | 评论 (0)编辑 收藏

编写一个函数totsubstrnum(char *str, char *substr) ,它的功能是:统计子字符串substr在字符串str中出现的次数。

思想:len2为子串的长度,设置变量 i =0, 利用strncmp函数将str+i 开始的len2个字符与子串substr进行比较,如果相等,则count加1,此时 i 加 len2,如果不等,则 i 加1,继续寻找。

代码测试通过:

#include<stdio.h>
#include<string.h>
int totsubstrnum(char *str, char *substr);
int main()
{
    char str[80],substr[80];
    printf("Input string: ");
    gets(str);
    printf("Input substring: ");
    gets(substr);
    printf("count = %d\n",totsubstrnum(str,substr));

    return 0;
}

int totsubstrnum(char *str, char *substr)
{
    int i=0,count=0,len1,len2;
    len1=strlen(str);
    len2=strlen(substr);
    while(i <= len1-len2)
    {
        if(strncmp(str+i,substr,len2) == 0)
        {
            count++;
            i +=len2;
        }
        else
            i++;
    }
    return (count);
}

posted @ 2012-08-12 16:05 hoshelly 阅读(1557) | 评论 (0)编辑 收藏

编写一函数strlshif(char *s, int n),其功能是把字符串s中的所有字符左移n个位置,字符串中的前n个字符移到最后。

代码测试通过:


#include<stdio.h>
#include<string.h>
void strlshif(char *s, int n);
void main()
{
    char str[]="0123456789";
    strlshif(str,3);
    printf("%s\n",str);
}

void strlshif(char *s, int n)
{
    int i,len;
    char ch;
    len=strlen(s);
    for(i=0;i<n;i++)
    {
        ch=s[0];
        strncpy(s,s+1,len-1);
        s[len-1]=ch;
    }
}

那么若是不用strncpy函数功能,如何使指定的字符串左移n位?
代码测试通过,如下:

#include<stdio.h>
#include<string.h>
int main()
{
    char str[]="0123456789";
    char sstr[80]={0}; //使用一数组储存移动后的字符串
    char *p;
    int c,j;
    static int i,n;
    p=&str[0];
    printf("input the number: \n");
    scanf("%d",&n); //输入要左移的前n个字符,即将这n个字符移动到最后面
    c=n;
    while( c-- && p++ ); //找到没有移动过的剩下的全部字符,把它们储存在数组sstr 中

        for(i=0;i<strlen(str)-n;i++)
        {
            sstr[i]= *p;
            p++;
        }
        p=&str[0];  //指针指向第一个字符
        for(j=i;j<strlen(str);j++) //将要移动的字符一个一个地“接”到数组sstr后面
        {
            sstr[j]= *p;
            p++;
        }
        sstr[j]='\0'; //最后字符串结尾用'\0'
        printf("%s",sstr);  
        return 0;
}



        

posted @ 2012-08-12 14:14 hoshelly 阅读(873) | 评论 (0)编辑 收藏

// s 是字符串 startloc 是开始取的位置   len表示取得子串长度
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void substr(char *s, int startloc,int len)
{
    if((startloc < 0) || (startloc >= strlen(s)) || (len<0))
    {
        printf("input error!");
        exit(0);
    }
    int i,c=0;
    char sstr[80];
    while(*s !='\0')
    {
        if(c!=startloc)
        {
            ++c;
            s++;
        }
        else
        {
            for(i=0;i<len;i++)
            {
                sstr[i]= *s;
                s++;
            }
            sstr[i]='\0';
            break;
        }
    }
    printf("%s",sstr);
}

int main()
{
    char str[80];
    int s,l;
    printf("Input string: ");
    gets(str);
    printf("Start Location: ");
    scanf("%d",&s);
    printf("Substring length: ");
    scanf("%d",&l);
    substr(str,s,l);
    return 0;
}

    

posted @ 2012-08-12 11:45 hoshelly 阅读(213) | 评论 (0)编辑 收藏

类似C语言中的strcat()函数,编程实现mystrcat( char *str, char * destr)的功能并测试 。


代码测试通过:

#include<stdio.h>
void mystrcat( char *str,char *destr)
{
    while(*str !='\0')
        str++;
    while(*destr !='\0')
    {
        *str = *destr;
        str++;
        destr++;
    }

    *str = '\0';
}

int main()
{
    char str[30],destr[30];
    printf("input string and substring: ");
    scanf("%s%s",str,destr);
    mystrcat(str,destr);
    printf("%s",str);
    return 0;
}

posted @ 2012-08-12 10:16 hoshelly 阅读(319) | 评论 (0)编辑 收藏

仅列出标题
共12页: First 2 3 4 5 6 7 8 9 10 Last