C语言难题

Python012

C语言难题,第1张

递归解决就ok

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

static int result=-1

void func(int *a,int *b,int n,int k, int *f, int min, int s)

{

int i

if(s == k)

{

if(result == -1 || result > min)

result = min

return

}

for(i = 0 i < n i ++)

{

if(f[i] == 0)

{

f[i] = 1

func(a,b,n,k,f,min+a[i]+b[i]*s,s+1)

f[i] = 0

}

}

}

int main()

{

int i

int *a, *b, *f

int n,k,m

scanf("%d%d%d", &n,&k,&m)

a=malloc(sizeof(int) * n)

b = malloc(sizeof(int) * n)

f = malloc(sizeof(int) * n)

for(i = 0 i < n i ++)

{

scanf("%d%d",a+i, b+i)

f[i] = 0

}

if(m == 0)

{

func(a,b,n,k,f,0,0)

printf("%d\n", result)

}

else

{

int min_r=-1

for(i = 0 i < n i ++)

{

result = 0

func(a,b,n,k,f,0,0)

result += a[n-1]

if(i == 0 || min_r > result)

min_r = result

printf("%d\n", min_r)

}

}

return 0

}

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <stdbool.h>

#include <windows.h>

#define MAXSEAT 12

typedef struct

{

int Seat

bool Saled

char Name[20]

char Card_No[20]

} _PLANE

typedef struct

{

int x

int y

}PT_STRUCT

PT_STRUCT pt

void setxy(int x, int y)

void getxy()

void Input(char* buf)

void Title()

void Init(_PLANE* s)

void ShowEmpty(_PLANE* s)

void ShowBySeat(_PLANE* s)

void ShowByName(_PLANE* s)

int  SortByName(_PLANE* s, _PLANE* buf)

void SaleTicket(_PLANE* s)

void DelSeat(_PLANE* s)

void Title()

{

system("cls")

printf("1. 显示空位数目和空位列表")

printf("\n2. 显示按座位编号的已分配座位列表")

printf("\n3. 显示按人名排序的已分配座位列表")

printf("\n4. 分配一个座位给顾客")

printf("\n5. 删除一个已分配的座位")

printf("\n6. 退出")

printf("\n\n请选择(1-6): ")

}

int main()

{

_PLANE Plane[MAXSEAT]

Init(Plane)

char Sel

while(1)

{

Title()

fflush(stdin)

Sel=getche()

if(Sel<'1' || Sel>'6')

{

continue

}

switch(Sel)

{

case '1':

ShowEmpty(Plane)

break

case '2':

ShowBySeat(Plane)

break

case '3':

ShowByName(Plane)

break

case '4':

SaleTicket(Plane)

break

case '5':

DelSeat(Plane)

break

case '6':

exit(0)

break

}

}

return 0

}

void Init(_PLANE* s)

{

int i

for(i=0i<MAXSEATi++)

{

s[i].Seat=i+1

s[i].Saled=false

strcpy(s[i].Name,"\0")

strcpy(s[i].Card_No,"\0")

}

}

void ShowEmpty(_PLANE* s)

{

int i, EmptySeatCount=0

printf("\n\n空位列表: \n\n")

for(i=0i<MAXSEATi++)

{

if(s[i].Saled==false)

{

EmptySeatCount++

printf("%d  ",s[i].Seat)

}

}

printf("\n\n还有%d个空位\n\n按任意键返回",EmptySeatCount)

fflush(stdin)

getch()

}

void ShowBySeat(_PLANE* s)

{

int i

printf("\n\n已分配的座位列表: \n\n")

for(i=0i<MAXSEATi++)

{

if(s[i].Saled==true)

{

printf("%d  ",s[i].Seat)

}

}

printf("\n\n按任意键返回")

fflush(stdin)

getch()

}

int SortByName(_PLANE* s, _PLANE* buf)

{

int i,j=0

_PLANE temp

for(i=0i<MAXSEATi++)

{

if(s[i].Saled==true)

{

buf[j++]=s[i]

}

}

if(j<2)

{

return j

}

int k

for(i=0i<j-1i++)

{

for(k=0k<j-1-ik++)

{

if(strcmp(buf[k].Name,buf[k+1].Name)>0)

{

temp=buf[k]

buf[k]=buf[k+1]

buf[k+1]=temp

}

}

}

return j

}

void ShowByName(_PLANE* s)

{

_PLANE buf[MAXSEAT]

int Count=SortByName(s,buf)

printf("\n\n已分配的座位列表: \n\n")

int i

for(i=0i<Counti++)

{

printf("%d(%s)  ",buf[i].Seat,buf[i].Name)

}

printf("\n\n按任意键返回")

fflush(stdin)

getch()

}

void SaleTicket(_PLANE* s)

{

int EmptySeat[MAXSEAT]={0}

int EmptySeatCount=0

int p=0,i

for(i=0i<MAXSEATi++)

{

if(s[i].Saled==false)

{

EmptySeatCount++

EmptySeat[p++]=s[i].Seat

}

}

printf("\n\n请从以下空位中选择:\n\n")

for(i=0i<EmptySeatCounti++) 

{

printf("%d  ",EmptySeat[i])

}

printf("\n\n请选择座位: ")

char buf[20]

Input(buf)

int SelSeat=atoi(buf)

if(SelSeat<1 || SelSeat>MAXSEAT)

{

printf("\n\n你选择的座位不符合")

fflush(stdin)

getch()

return

}

for(i=0i<EmptySeatCounti++)

{

if(SelSeat==EmptySeat[i])

{

break

}

}

if(i==EmptySeatCount)

{

printf("\n\n你选择的座位已经售出!")

fflush(stdin)

getch()

return

}

s[SelSeat-1].Saled=true

printf("\n\n请输入你的姓名: ")

Input(buf)

strcpy(s[SelSeat-1].Name,buf)

printf("\n\n请输入你的身份证号: ")

Input(buf)

strcpy(s[SelSeat-1].Card_No,buf)

printf("\n\n售票成功! 按任意键返回主菜单")

fflush(stdin)

getch()

}

void setxy(int x, int y)

{

   COORD coord = {x, y}

   SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord)

}

void getxy()

{

HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE)

    COORD coordScreen = {0, 0} //光标位置

    CONSOLE_SCREEN_BUFFER_INFO csbi

    if (GetConsoleScreenBufferInfo(hConsole, &csbi))

    {

        pt.x=csbi.dwCursorPosition.X

        pt.y=csbi.dwCursorPosition.Y

    }

}

void Input(char* buf)

{

int len=strlen(buf)

int c

int i=0

while(1)

{

c=getch()

if(c==8 && i<=0)

{

continue

}

if(c==10 || c==13)

{

buf[i]='\0'

break

}

if(c==8 && i>0)

{

// buf[i]='\0'

if(i>0)

{

i--

}

buf[i]='\0'

getxy()

setxy(pt.x-1,pt.y)

printf(" ")

setxy(pt.x-1,pt.y)

}

else if(i>=0)

{

buf[i++]=c

printf("%c",c)

}

if(i>=buf)

{

return

}

}

}

void DelSeat(_PLANE* s)

{

int i

bool find=false

char buf[20]

printf("\n\n请输入退票人的身份证号码: ")

Input(buf)

for(i=0i<MAXSEATi++)

{

if(s[i].Saled==true)

{

if(strcmp(s[i].Card_No,buf)==0)

{

find=true

s[i].Saled=false

strcpy(s[i].Card_No,"")

strcpy(s[i].Name,"")

printf("\n\n退票成功! 按任意键返回主菜单")

fflush(stdin)

getch()

return

}

}

}

if(find==false)

{

printf("\n\n未找到该身份证信息")

fflush(stdin)

getch()

}

}

typedef struct stu{

char * name,gender

int score

}STU

void f(char *p)

{

//p=(char *)malloc(10)

strcpy(p,"Qian")

}

int main(void)

{

STU a={NULL,'m',290},b

a.name=(char *)malloc(10)

strcpy(a.name,"Zhao")

b = a

f(b.name)

b.gender ='f'

b.score =350

printf("%s,%c,%d

\n",a.name,a.gender,a.score)

printf("%s,%c,%d

\n",b.name,b.gender,b.score)

return 0

}

为啥注释掉了以后的答案变成:Qian,m,290,Qian,f,

350,?

而不注释掉答案却是: Zhao,m,290,Zhao

,f,350?

你的目的是通过函数去修改另外一个结构体对象b中成员name

的值。

观察到成员中的gender和score基本上没有什么值得研究 ,

所以只研究第一个成员name。

在你执行了代码b=a的时候,b和a分别在内存条上占用

sizeof(stu)的空间大小。而且,这两个空间中的值都是一

模一样的。

当你执行了函数f()的时候,分两种情况。

第一没有注释:函数f会在栈上分配4个字节的内存(sizeof

(char*),因为p的类型就是char*)。那么p的值就是等于

b.name。

b.name的值我们假设是0x1234567,那么现在p的值也就是

0x1234567。没有注释的情况下,p的值马上被修改成malloc

的返回值,所以现在p的值一定不是0x1234567。

假如malloc的返回值是0x7654321,那么现在p的值也就是

0x7654321。你使用了strcpy,那么0x7654321首地址开始的

内容就变成了zhao。

所以,你没有改变0x1234567所指向的内容。结果就是

0x1234567指向的内容还是为zhao。

第二有注释:函数f会在栈上分配4个字节的内存(sizeof

(char*),因为p的类型就是char*)。那么p的值就是等于

b.name。

b.name的值我们假设是0x1234567,那么现在p的值也就是

0x1234567。有注释的时候。p的值没有被malloc改变,还是

0x1234567.你使用了strcpy,就修改了0x1234567所指向的

内存。

最开始的内存中值是zhao,你修改后就变成了qian.

你的这个例子错误很多,但是基于你是实验的目的,我就不指

正了。针对不注释的情况,如果你要改变的话,我认为比较正

确的做法应该是这样的。

typedef struct stu{

char * name,gender

int score

}STU

void f(char **p)

{

*p=(char *)malloc(10)

strcpy(*p,"Qian")

}

int main(void)

{

STU a={NULL,'m',290},b

a.name=(char *)malloc(10)

strcpy(a.name,"Zhao")

b = a

f(&b.name)

free(a.name)

a.name=NULL:

return 0

}