C 基础练习3

本文介绍如何用C语言实现带头结点的单链表,并演示节点的添加、删除及不同排序算法的应用,包括冒泡排序和选择排序。此外,还提供了约瑟夫环、位操作、矩阵乘法等实用示例。

***QT 编译器支持C99方式:
在.pro文件中增加
CONFIG += c++11
QMAKE_CFLAGS += -std=c99***


/* 带头结点单链表
* 1,节点的添加
* 2,节点的删除
* 3,冒泡排序
* 4,选择排序
*/

#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<stdlib.h>
#include<time.h>

typedef struct Node{
    int num;
    struct Node* next;
}node;

void AddNode(node **end, int num,int *size);  //end 应该为指针的指针传址型,否则end的值将不会改变
void PrintLink(node **head);//同上

void DeleNode(node **pre);

void Insert(node **pre,node** node1);

void Swop(node **pre1,node **pre2);
void Swop1(node **node1, node **node2);

void BubbleSort(node *head, int size);
//选择排序尾交换法
void SelectSort(node **head, int size);
//选择排序头插法
void SelectSort1 (node **head, int size);

void main()
{
    srand(time(0));
    node* head, *end;
    int size=0;
    head=(node*)malloc(sizeof(node));
    head->next=NULL;
    head->num=0;
    end=head;
    for(int i=0;i<20;i++)
    {
        AddNode(&end,rand()%100,&size);
    }
    end->next=NULL;
    PrintLink(&head);

//    BubbleSort(head,size);
//    SelectSort(&head,size);
    SelectSort1(&head,size);
    PrintLink(&head);
}
void AddNode(node** end, int num, int *size)
{
    node* ele;
    ele=(node*)malloc(sizeof(node));
    (*end)->next=ele;
    ele->num=num;
    *end=ele;
    (*size)++;
}

void PrintLink(node **head)
{
    node* ele;
    ele=(*head)->next;
    while(ele!=NULL)
    {
        printf("%d,",ele->num);
        ele=ele->next;
    }
    printf("\n");
}

void DeleNode(node **pre)
{
    node* temp=(*pre)->next;
    (*pre)->next=(*pre)->next->next;
    free(temp);
}

void Insert(node **pre, node **node1)
{
    (*node1)->next=(*pre)->next;
    (*pre)->next=*node1;
}

void Swop(node **pre1, node **pre2)
{
    if((*pre1)->next==NULL||(*pre2)->next==NULL)
    {
        printf("pre1||pre2 is wrong");
        return ;
    }
    int temp=(*pre1)->next->num;
    (*pre1)->next->num=(*pre2)->next->num;
    (*pre2)->next->num=temp;

    //链表节点的交换只需要交换节点内的数据即可,不需要进行节点的删除与插入
//    node* pre1Next,*pre2NextNext;
//    pre1Next=(*pre1)->next;
//    pre2NextNext=(*pre2)->next->next;
//    (*pre2)->next->next=(*pre1)->next->next;
//    (*pre1)->next=(*pre2)->next;

//    (*pre2)->next=pre1Next;
//    pre1Next->next=pre2NextNext;
}

void Swop1(node **node1, node **node2)
{
    if((*node1)==NULL||(*node2)==NULL)
    {
        printf("pre1||pre2 is wrong");
        return ;
    }
    int temp=(*node1)->num;
    (*node1)->num=(*node2)->num;
    (*node2)->num=temp;

}

void BubbleSort(node *head,int size)
{
    for(int i=0;i<size;i++)
    {
        node *max=head;
        for(node *temp=head;temp->next!=NULL;)
        {
            if(max->next->num>temp->next->num)
            {
                Swop(&max,&temp);
                max=max->next;
            }else
            {
                max=temp;
                temp=temp->next;
            }
        }
    }
}

//选择排序尾交换法
void SelectSort(node **head, int size)
{
    node *end=*head;
    for(int i=0;i<=size;i++)
    {
        end=end->next;
    }
    for(int i=0;i<size;i++)
    {
        node *max=(*head)->next;
        node *temp=(*head)->next;
        for(;;)
        {
            if(max->num<temp->num)
            {
                max=temp;
            }
            if(temp->next==end)
            {
                break;
            }else{
                temp=temp->next;
            }
        }
        end=temp;
        Swop1(&max,&end);
    }
}
void SelectSort1 (node **head, int size)
{
    node* headTemp=*head;
    for(int i=0;i<size;i++)
    {
        node *max=headTemp;
        for(node *temp=headTemp->next;temp->next!=NULL;temp=temp->next)
        {
            if(max->next->num<temp->next->num)
            {
                max=temp;
            }
        }
        //当对链表中某一个节点进行复制使用动态数组分配一个节点空间之后进行复制,node* temp =max将会出现某些错误
        node* temp=(node*)malloc(sizeof(node));  
        temp->num=max->next->num;
        temp->next=NULL;
        DeleNode(&max);
        Insert(&headTemp,&temp);
        headTemp=headTemp->next;
    }
}

双向链表建立

循环链表建立

约瑟夫环
1. 方法一,暴力求解法:使用数组元素a[i]为1表示第i个成员已死亡直到最后一个成员,遍历数组a即能得到
2. 方法二:数学方法

#include <stdio.h>
int main()
{
    int n, m, i, s = 0;
    printf ("N M = ");
    scanf("%d%d", &n, &m);
    for (i = 2; i <= n; i++)
    {
        s = (s + m) % i;
    }
    printf ("\nThe winner is %d\n", s+1);
}

输入一个数num,将num的高八位与低八位交换输出

#include<stdio.h>
#include<stdlib.h>  //itoa头文件
void main()
{
    int num;
    scanf("%d",&num);
    char string[25];
    itoa(num,string,2);//将整数转换为二进制字符串
    puts(string);
    int n=0x00ff;
    int temp=num&n;  //位于,两位都为1结果为1,否则为0
    num>>=8;
    temp<<=8;
    num=num|temp;   //位或,有一位为1则为1
    itoa(num,string,2);
    puts(string);
    printf("%d",num);
}

读入某一年的年号和该年1月1日是星期几的信息,输出某个月的日历

#include<stdio.h>

void main()
{
    int year,moun,day,week;
    puts("year:");
    scanf("%d",&year);
    puts("week");
    scanf("%d",&week);
    if(week<1||week>7)
    {
        puts("wrong");
        return ;
    }
    if(year%4==0||year%100==0||year%400==0)
    {
        day=1;
    }
    else
        day=0;
    puts("moun");
    scanf("%d",&moun);
    for(int i=1;i<=moun;i++)
    {
        int maxday=0;
        if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
        {
            maxday=31;
        }
        else if(i==2&&day==1)
        {
            maxday=29;
        }else if(i==2&&day==0)
        {
            maxday=28;
        }else
        {
            maxday=30;
        }
        if(i!=moun)
        {
            for(int j=0;j<maxday;j++)
            {
                week=(week+1)%7;
            }
            if(week==0)
            {
                week=7;
            }

        }else
        {
            for(int m=1;m<week;m++)
            {
                printf("   ");
            }
            for(int m=1;m<=maxday;m++)
            {
                    printf("%2d ",m);
                    if((m+week-1)%7==0)
                    printf("\n");
            }
            }

    }
}

输入两个矩阵,计算矩阵的乘积

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

void main()
{
    srand(time(0));
    int arow=3,aline=5,brow=aline,bline=7;
    int a[arow][aline],b[brow][bline],c[arow][bline];
    printf("a:\n");
    for(int i=0;i<arow;i++)
    {
        for(int j=0;j<aline;j++)
        {
            a[i][j]=rand()%10;
            printf("%3d ",a[i][j]);
        }
        printf("\n");
    }
    printf("\n");
     printf("b:\n");
    for(int i=0;i<brow;i++)
    {
        for(int j=0;j<bline;j++)
        {
            b[i][j]=rand()%10;
            printf("%3d ",b[i][j]);
        }
        printf("\n");
    }
     printf("\n");
     printf("result:\n");
    for(int i=0;i<arow;i++)
    {
        int row=0;
        for(;row<bline;row++)
        {
            int sum=0;
            for(int j=0;j<aline;j++)
            {
                sum=sum+a[i][j]*b[j][row];
            }
            c[i][row]=sum;
        }
    }
    for(int i=0;i<arow;i++)
    {
        for(int j=0;j<bline;j++)
        {
            printf("%3d ",c[i][j]);
        }
        printf("\n");
    }
}
  • 将十进制数转换为二~十六进制任意进制数
  • 任意进制转十进制方法:
  • x7 x6 x5 x4,x3 x2 x1 x0为n进制转十进制步骤
  • num=0;
  • for(int i=0;i<7;i++)
  • {
  • num=num+xi*pow(n,i);
  • }
    *
  • 十进制转n进制方法一
  • num;
  • res=0;
  • while(num!=0)
  • {
  • if(num
//方法一
#include<stdio.h>

int Exchange(int num,int n);

void main()
{
    int num=0;
    int n=0;
    scanf("%d %d",&num,&n);
    if(n<2||n>16)
    {
        puts("wrong");
        return;
    }
    printf("result:%d",Exchange(num,n));
}

int Exchange(int num, int n)
{
    if(num<n)
    {
        return num;
    }
    else
    {
        return ((num%n)+Exchange(num/n,n)*10);
    }
}

字符串赋值问题:
字符串定义:char c[20]; c=”abcd”;为错误的赋值方式
char c[20]=”abcd”;为正确的赋值方式


if-else问题
else遵循就近原则


函数变量问题:
1. 在一个函数 内定义的变量在本函数范围内都有效是错误的原因为:
在一个函数内定义变量有两种情况1)在函数的开头定义,2)在函数的复合语句内定义,第二中在本函数内无效只在复合语句内部有效
2. 函数中的形式参数是局部变量
3. 在一个函数内定义的变量只在本函数范围内有效
4. 在不同的函数中可以使用相同的名字的变量


以下程序输出的内容为( 11,11,11 )。
因为p为a的指针,当p在myfunc中自增时a也会自增,所以此时a的值为11

#include <stdio.h>
int myfunc(int a,int *p){a++,(*p)++; return *p;}
void main(){int a=10,b, *p; p=&a; b=myfunc(a,p); printf("%d,%d,%d",a,b,*p);}

编写完整程序,计算1-x+x^2/2!-x^3/3!+…+x^n/n!,只允许有Main函数,不允许定义其他函数,x是单浮点数,n是整数。
动态规划思想

#include<stdio.h>

int main()
{
    float x;
    int n,sum=1,temp=1,temp1=1;
    scanf("%f",&x);
    scanf("%d",&b);
    for(int i=1;i<=n;i++)
    {
        temp=temp*x;
        temp1=temp1*i;
        sum+=(-1*(temp/temp1);
    }
    return 0;
}
内容概要:本文介绍了基于Matlab代码实现的【EI复现】考虑网络动态重构的分布式电源选址定容优化方法,重点研究在电力系统中结合网络动态重构技术进行分布式电源(如光伏、风电等)的最佳位置选择与容量配置的双层优化模型。该方法综合考虑配电网结构变化与电源布局之间的相互影响,通过优化算法实现系统损耗最小、电压稳定性提升及可再生能源消纳能力增强等多重目标。文中提供了完整的Matlab仿真代码与案例验证,便于复现实验结果并拓展应用于微网、储能配置与配电系统重构等相关领域。; 适合人群:电力系统、电气工程及其自动化等相关专业的研究生、科研人员及从事新能源规划与电网优化工作的工程师;具备一定Matlab编程基础和优化理论背景者更佳。; 使用场景及目标:①用于科研论文复现,特别是EI/SCI级别关于分布式能源优化配置的研究;②支【EI复现】考虑网络动态重构的分布式电源选址定容优化方法(Matlab代码实现)撑毕业设计、课题项目中的电源选址定容建模与仿真;③辅助实际电网规划中对分布式发电接入方案的评估与决策; 阅读建议:建议结合提供的网盘资源下载完整代码与工具包(如YALMIP),按照文档目录顺序逐步学习,注重模型构建思路与代码实现细节的对应关系,并尝试在不同测试系统上调试与扩展功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值