1.题目:创建单链表并赋值,要求遍历能输出1~9
#include <stdio.h>
#include <stdlib.h>
struct node
{
int num;
struct node * next;
};
typedef struct node Node;
typedef struct node * Link;
void create_link(Link * head)
{
*head = NULL;
}
void malloc_ok(Link new_node)
{
if(new_node == NULL)
{
printf("malloc error\n");
exit(-1);
}
}
void create_node(Link * new_node,int i)
{
(*new_node) = (Link)malloc(sizeof(Node));
malloc_ok(*new_node);
(*new_node)->num = i;
}
void insert_node_tail(Link * head,Link new_node)
{
Link p;
p = *head;
if(p == NULL)
{
*head = new_node;
new_node->next = NULL;
}
else
{
while(p->next != NULL)
{
p = p->next;
}
p->next = new_node;
new_node->next = NULL;
}
}
void display(Link head)
{
Link p;
p = head;
while(p != NULL)
{
printf("num = %d\n", p->num);
p = p->next;
}
}
int main()
{
Link head;
Link new_node;
int i;
create_link(&head);
for(i = 1; i < 10; i++)
{
create_node(&new_node,i);
insert_node_tail(&head,new_node);
}
display(head);
return 0;
}
2.题目:读取一个5*5数组,然后显示每行的和与每列的和
#include <stdio.h>
int main()
{
int a[5][5] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25};
int b[5],c[5];
int i,j,temp = 0;
for(i = 0; i < 5; i++)
{
temp = 0;
for(j = 0; j < 5; j++)
{
temp = temp + a[i][j];
}
b[i] = temp;
}
for(j = 0; j < 5; j++)
{
temp = 0;
for(i = 0; i < 5; i++)
{
temp = temp + a[i][j];
}
c[j] = temp;
}
for(i = 0; i < 5; i++)
{
printf("第%d行的和为%d.\n",i+1, b[i]);
}
for(i = 0; i < 5; i++)
{
printf("第%d列的和为%d.\n",i+1 ,c[i]);
}
return 0;
}
3.题目:编程判断字符串是否为回文
判断一个字符串是否是回文,例如单词‘level’
#include <stdio.h>
#include <string.h>
int huiwen(char *str)
{
int len = strlen(str);
int i,j,temp;
for(i = 0, j = len - 1; i <= j; i++,j--)
{
if(str[i] == str[j])
{
temp = 1;
}
else
{
temp = 0;
break;
}
}
return temp;
}
int main()
{
char str[100];
printf("请输入字符串。\n");
gets(str);
if(huiwen(str))
{
printf("%s是回文\n",str);
}
else
{
printf("%s不是回文\n",str);
}
return 0;
}
4.题目:生日蜡烛
某君从某年开始每年都举办一次生日party,并且每次都要吹熄与年龄相同根数的蜡烛。
现在算起来,他一共吹熄了236根蜡烛。
请问,他从多少岁开始过生日party的? (第七届蓝桥杯B组真题(第二题))
#include <stdio.h>
int main()
{
int i,j,n;
printf("请输入多少根蜡烛。\n");
scanf("%d",&n);
for(i = 1; i <=200; i++)
{
for(j = i; j <= 200; j++)
{
if((i+j)*(j-i+1)/2 == n)
{
printf("他是从%d岁过生日的。\n",i);
}
}
}
return 0;
}
5.题目:编程实现查找两个字符串的最大公共子串
示例:“aocdfe"和"pmcdfa"最大公共子串为"cfd”
#include <stdio.h>
#include <string.h>
void substr(char *s1, char *s2, int len)
{
int i, j, m = 0;
int p, q;
int length = strlen(s2);
char a[100] = {0};
char b[100] = {0};
for(i = 0; i < len; i++)
{
for(j = 0; j < length; j++)
{
strcpy(a, "0");
p = i;
q = j;
while(s1[p] == s2[q] && s1[p] != 0)
{
a[m] = s2[q];
p++;
q++;
m++;
}
if(strlen(a) > strlen(b))
{
strcpy(b, a);
}
m = 0;
}
}
printf("最大公子串:%s\n", b);
}
int main()
{
char str1[100] = {0};
char str2[100] = {0};
int len;
printf("请输入字符串1:");
scanf("%s", str1);
printf("请输入字符串2:");
scanf("%s", str2);
len = strlen(str1) <= strlen(str2) ? strlen(str1) : strlen(str2);
if(len == strlen(str1))
{
substr(str1, str2, len);
}
else if(len == strlen(str2))
{
substr(str2, str1, len);
}
return 0;
}
7.题目:创建一个学生学号及姓名的单链表,即节点包括学生学号、姓名及指向下一个节点的指针,链表按学生的学号排列。再从键盘输入某一学生姓名,将其从链表中删除。
# include <stdlib.h>
# include <malloc.h>
struct node /*节点的数据结构*/
{
int num;
char str[20];
struct node *next;
};
/* * * * * * * * * * * * * * * * * * * * * * * * * * * */
main( )
{
/*函数声明*/
struct node *creat();
struct node *insert();
struct node *delet();
void print( );
struct node *head;
char str[20];
int n;
head=NULL; /*做空表*/
head=creat(head); /*调用函数创建以head 为头的链表*/
print(head);/*调用函数输出节点*/
printf("\n input inserted num,name:\n");
gets(str); /*输入学号*/
n=atoi(str);
gets(str); /*输入姓名*/
head=insert(head, str, n); /*节点插入链表*/
print(head);
printf("\n input deleted name:\n");
gets(str); /*输入被删姓名*/
head=delet(head,str); /*用函数删除节点*/
print(head); /*调用函数输出节点*/
return;
}
/* * * * * * * * * * * * * * * * * * * * * */
/* * * 创建链表* * * * * * * * * * * */
struct node *creat(struct node *head)
{
char temp[30];
struct node *pl,*p2;
pl=p2=(struct node*)malloc(sizeof(struct node));
printf("input num, name: \n");
printf("exit:double times Enter!\n");
gets(temp);
gets(pl->str);
pl->num=atoi(temp);
pl->next = NULL ;
while(strlen(pl->str)>0)
{
if(head==NULL)//if the chain is null
head= pl;//
else
p2->next=pl;//set the p2->next NULL
p2 = pl;//
pl=(struct node*)malloc(sizeof(struct node));
printf ("input num, name: \n");
printf("exit:double times Enter!\n");
gets(temp);
gets(pl->str);
pl->num=atoi(temp);
pl->next= NULL;
}
return head;
}
/* * * * * * * * * * * * * * * * * * * */
/* * * * * * * * * * 插入节点* * * * * * * * * */
struct node *insert(head, pstr,n)
struct node *head;
char *pstr;
int n;
{
struct node *pl,*p2,*p3;
pl=(struct node*)malloc(sizeof(struct node));
strcpy (pl->str, pstr);
pl->num = n;
p2 = head;
if(head == NULL)
{
head = pl;
pl->next = NULL;
}
else
{
while (n>p2->num&&p2->next!=NULL)
{
p3 = p2;
p2= p2->next;
}
if (n<=p2->num)
if (head==p2)
{
head = pl;
pl->next = p2;
}
else
{
p3->next = pl;
pl->next= p2;
}
else
{
p2->next= pl;
pl->next = NULL;
}
}
return(head);
}
/* * * * * * * * * * * * * * * * * * * * * * * * */
/* * * * * 删除节点* * * * * * * * * * * * */
struct node *delet (head, pstr)
struct node *head;
char *pstr;
{
struct node *temp,*p;
temp =head;
if (head==NULL)
printf("\nList is null!\n");
else
{
temp = head;
while(strcmp(temp->str,pstr)!= 0 && temp->next!=NULL)
{
p = temp;
temp= temp->next;
}
if(strcmp(temp->str,pstr)== 0)
{
if (temp== head)
{
head = head->next;
free(temp);
}
else
{
p->next =temp->next;
printf("delete string :%s\n",temp->str);
free(temp);
}
}
else
printf("\nno find string!\n");
}
return(head);
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* * * * * * * * * * 链表各节点的输出* * * * * * * * * */
void print(struct node *head)
{
struct node *temp;
temp = head;
printf("\n output strings:\n");
while (temp!=NULL)
{
printf("\n%d-----%s\n",temp->num,temp->str);
temp= temp->next;
}
return;
}