// 送分, 我就不客气笑纳了, 3分钟写完.
#include<stdio.h>
#include<ctype.h>
int alpha[30];
int main()
{
int i;
char str[100];
gets(str);
for (i = 0; str[i]; i ++)
{
if (isalpha(str[i]))
{
alpha[str[i] - 'a'] ++;
}
}
for (i = 0; i < 26; i ++)
{
if (alpha[i]) printf("%c=%d\n", (char)(i+'a'), alpha[i]);
}
return 0;
}
// 真几年没写了, 从struct从零开始写, 有机会在改进一下.
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node * next;
};
int main()
{
int len = 0;
printf("\n请输入链表(非数表示结束)\n");
struct node *Head = (struct node *) malloc(sizeof(struct node)), *pre;
pre = Head;
while(1)
{
int n;
printf("结点值:");
if(scanf("%d", &n) == 0) break;
struct node *temp = (struct node *) malloc(sizeof(struct node));
temp->data = n;
temp->next = NULL;
pre->next = temp;
pre = temp;
len++;
}
struct node *p;
printf("\n原来表:\n");
for (p = Head->next; p; p = p->next)
{
printf("%4d", p->data);
}
struct node *pp;
p = Head->next;
int flag = 1;
while(1)
{
pp = p->next;
if(flag) // 恰好避免一堆烦人的讨论
{
p->next = NULL;
flag = 0;
}
else
{
p->next = Head->next;
Head->next = p;
if (pp == NULL) break;
}
p = pp;
}
printf("\n\n反转表:\n");
for (p = Head->next; p; p = p->next)
{
printf("%4d", p->data);
}
// 忘了怎么逆序了
/*
printf("\n\n反转表:\n");
int a[100], i;
for (i = 0, p = Head->next; p; p = p->next, i ++)
{
a[i] = p->data;
// printf("%4d", p->data);
}
for (i = 0; i < len; i ++)
{
printf("%4d", a[len - 1 - i]);
}
*/
return 0;
}
// 按照模板修改的
#include<stdio.h>
#include<stdlib.h>
struct link{
int data;
struct link *next;
};
void DisplayNode(struct link *head)
{
struct link *p = head;
int j = 1;
while(p != NULL)
{
// printf("%5d%10d\n", j, p->data);
printf("The value is ==>%d\n", p->data);
p = p->next;
j++;
}
}
void DeleteMemory(struct link *head)
{
struct link *p = head, *pr = NULL;
while(p != NULL)
{
pr = p;
p = p->next;
free(pr);
}
}
struct link *AppendNode(struct link *head) // 返回指针,
{
struct link *p = NULL, *pr = head;
int data;
p = (struct link *) malloc(sizeof(struct link));
if (p == NULL)
{
printf("No enough memory to allocate!\n");
exit(0);
}
if (head == NULL) // 若原链表为空表
{
head = p; // 将新建节点置为头节点.
}
else // 若原链表为非空, 则将新建节点添加到表尾
{
while(pr->next != NULL)
{
pr = pr->next; // 然pr指向下一个结点
}
pr->next = p; // 然末节点的指针域指向新建节点
}
//printf("Input node data:") ;
scanf("%d", &data);
p->data = data;
p->next = NULL; // 将新建节点置为表尾
return head;
}
void reverseprint(struct link *head)
{
if(head == NULL) return;
reverseprint(head->next);
printf("The value is ==>%d\n", head->data);
}
int main(void)
{
return 0;
}
// 这题不给过, 估计它怕学生提交的代码没有free, 所以就不判断这道题, 哈哈.
// 我还特意写了个递归的free很是方便, DeleteMemory
#include<stdio.h>
#include<stdlib.h>
struct link{
int data;
struct link * next;
};
struct link * AppendNode(struct link * head)
{
struct link *p = NULL, *pr = head;
p = (struct link *) malloc(sizeof(struct link));
if(head == NULL)
{
head = p;
}
else
{
while(pr->next != NULL) // bug!!!
{
pr = pr->next;
}
pr->next = p;
}
// 最后才对p赋值!!!
int data;
scanf("%d", &data);
p->data = data;
p->next = NULL;
return head;
}
void PrintNode(struct link *head)
{
struct link *p = head;
while(p != NULL)
{
printf("%4d", p->data);
p = p->next;
}
}
struct link *DeleteNode(struct link *head, int x)
{
struct link *p = head, *pr = head; // 先让 p就位!!!! p就是要删除的结点, pr是p的前驱结点.
while(p != NULL && p->data != x) // 这思路可以啊, 找到待删除结点, 后续再做删除处理.
{
pr = p;
p = p->next;
}
if (p->data == x)
{
if (p == head)
{
head = p->next;
}
else
{
pr->next = p->next;
}
free(p);
}
else // 妙啊
{
printf("This Node has not been found! \n");
}
return head;
}
void DeleteMemory(struct link *head)
{
if(head == NULL) return;
struct link *p = head;
DeleteMemory(head->next);
free(p);
}
int main()
{
int i, j, n, x;
struct link *head;
printf("输入数组6个元素的值。\n");
for (i = 0; i < 6; i ++)
{
head = AppendNode(head);
}
printf("输入要删除的数据x: ");
scanf("%d", &x);
printf("\n此链表各个结点的数据域为:\n");
PrintNode(head);
head = DeleteNode(head, x);
printf("\n删除后链表各个结点的数据域为:\n");
PrintNode(head);
DeleteMemory(head);
return 0;
}
困难——一维数组 3道题
// 这题有难度啊!!! 卧槽!!! 考这个直接寄. 大家基本都要寄. 除非遇到过原题, 不然临场想不出来的.
#define NUM 10
#include <stdio.h>
int i[NUM];
main()
{ int sum, n,total,k,flag,count=0;
printf("Please enter requried terms (<=10):");
scanf("%d",&n);
printf(" their sum:");
scanf("%d",&total);
sum=0;
k=n;
i[n]=1;
printf("There are following possible series:\n");
while(1)
{
if(sum+i[k]<total)
if(k<=1)
{i[1]=total-sum;flag=1;}
else
{sum+=i[k--];
i[k]=i[k+1];
continue;
}
else if(sum+i[k]>total||k!=1)
{sum-=i[++k];
flag=0;
}
else
flag=1;
if(flag)
{
printf("[%d]:",++count);
for(flag=1;flag<=n;++flag)
printf("%d",i[flag]);
printf("\n");
}
if(++k>n)
break;
sum-=i[k];
i[k]++;
}
}
// 排序后, 贪心.
#include<stdio.h>
void Sort(int a[100], int n)
{
int i, j;
for (i = 0; i < n; i ++)
{
for (j = n - 1; j > i; j --)
{
if (a[j] < a[j - 1])
{
int t = a[j - 1];
a[j - 1] = a[j];
a[j] = t;
}
}
}
}
int f(int a[100], int n)
{
int res = 0, i, j;
Sort(a, n);
for (i = 2; i < n; )
{
//printf("%d\n", i);
if (a[i] - a[i - 1] == 1 && a[i - 1] - a[i - 2] == 1)
{
res ++;
i += 3;
}
else i ++;
}
return res;
}
int main()
{
int a[100];
int n;
printf("Input n:");
scanf("%d", &n);
printf("Input card numbers:");
int i;
for (i = 0; i < n; i ++)
{
scanf("%d", &a[i]);
}
printf("Exchanged:%d", f(a, n));
return 0;
}
无聊
困难——简单指针变量 1道题
// 困难完结撒花✿✿ヽ(°▽°)ノ✿
#include<stdio.h>
int main()
{
char str[100];
printf("请输入一个字符串,长度小于等于100:") ;
gets(str);
printf("该字符串的内存编码为: ");
int i;
for (i = 0; str[i]; i ++)
{
printf("%x ", (unsigned char)str[i]); // 为了不输出多余的ffff, 因为char可以为负数;
}
return 0;
}