两道签到题:
题目1:字母变换
题目分值:10
---------------------------------------------
题目要求:读入LADYBUG-BIG-HOUSE 并输出其小写形式
代码:
#include <stdio.h>
int main()
{
char c;
while ((c = getchar()) != '\n')
{
if (c >= 'A' && c <= 'Z')
c += 32;
printf("%c", c);
}
}
题目2:疯狂循环
题目分值:10
---------------------------------------------
题目要求:运行程序,使其不停输出“I love GUET!”
#include <stdio.h>
int main()
{
while (true)
{
printf("“I love GUET!”\n");
}
}
正式题目:
0x01:图形打印机
分值:0-25(取正确作答中最高level的分值)
要求:使用循环在屏幕上打印指定的形状。
Level-1(分值:5) |
正三角形或者倒三角形
或者
代码:
#include <stdio.h>
int main()
{
int i, j;
for (i = 1; i <= 6; i++)//改成(i = 6; i >= 1; i--)就是倒三角
{
for (j = 1; j <= i; j++)
{
printf("*");
}
printf("\n");
}
}
Level-2(分值:10) |
金字塔形
或者
代码:
#include <stdio.h>
int main()//正金字塔
{
int i, j;
for (i = 1; i <= 9; i+=2)//换成(i = 9; i >= 1; i-=2)就是倒金字塔
{
for (j = 1; j <= 9; j++)
{
if (j <= (9 - i) / 2 || j > (9 - i) / 2 + i)
printf(" ");
else
printf("*");
}
printf("\n");
}
return 0;
}
Level-3(分值:15) |
空心三角形
代码:
#include <stdio.h>
int main()
{
int i, j;
for (i = 1; i <= 9; i+=2)
{
for (j = 1; j <= 9; j++)
{
if (i == 9 || j == 5 +(i - 1)/2 || j == 5 - (i - 1)/2)
printf("*");
else
printf(" ");
}
printf("\n");
}
}
Level-4(分值:25) |
- k形图案
输入描述:多组输入,一个整数(2~20)。
输出描述:针对每行输入,输出用“*"组成的K形,每个“*"后面有一个空格。
示例:
代码:
#include <stdio.h>
int main()
{
int i;
do
{
scanf("%d", &i);
int j,k;
for (j = i+1; j >= 1; j--)
{
for (k = 1; k <= j; k++)
{
printf("* ");
}
printf("\n");
}
for (j = 2; j <= i+1; j++)
{
for (k = 1; k <= j; k++)
{
printf("* ");
}
printf("\n");
}
} while (i != -1);
return 0;
}
0x02:旋转的数组
分值:25
题目描述:将一个4X4的数组进行逆时针旋转90度后输出,要求原始数组的数据随机输入,新数组以4行4列的方式输出。
示例:
#include <stdio.h>
int main()
{
int n[4][4];
int i;
printf("请输入16个数字:");
for (int p = 0; p <= 3; p++)
{
for (int q = 0; q <= 3; q++)
{
scanf("%d", &n[p][q]);
}
}
printf("矩阵A:\n");
for (int p = 0; p <= 3; p++)
{
for (int q = 0; q <= 3; q++)
{
printf("%4d", n[p][q]);
}
printf("\n");
}
for (int p = 0; p <= 1; p++)
{
for (int q = 0; q <= 1; q++)
{
int i = n[q][3 - p];
n[q][3 - p] = n[3 - p][3 - q];
n[3 - p][3 - q] = n[3 - q][p];
n[3 - q][p] = n[p][q];
n[p][q] = i;
}
}
printf("矩阵B:\n");
for (int p = 0; p <= 3; p++)
{
for (int q = 0; q <= 3; q++)
{
printf("%4d", n[p][q]);
}
printf("\n");
}
return 0;
}
0x03:简单的递归
分值:30
题目描述:
本题要求实现一个函数,对一个整数进行按位顺序输出。
函数接口定义:
void printdigits( int n );
函数 printdigits 应将 n 的每一位数字从高位到低位顺序打印出来,每位数字占一行。
测试程序样例:
#include <stdio.h>
void printdigits( int n );
int main()
{
int n;
scanf("%d", &n);
printdigits(n);
return 0;
}
/* 将你的代码嵌在这里 */
输入样例:
12345
输出样例:
1
2
3
4
5
代码:
void printdigits(int n)
{
int i = 10, j = 0;
while (n > 1)
{
j = j * 10 + n % i;
n /= 10;
}
while (j > 1)
{
printf("%d\n", j % 10);
j = j / 10;
}
}
0x04:约瑟夫小游戏
分值:25
题目描述:
30 个人在一条船上,超载,需要 15 人下船。
于是人们排成一队,排队的位置即为他们的编号。
报数,从 1 开始,数到 9 的人下船。
如此循环,直到船上仅剩 15 人为止,问都有哪些编号的人下船了呢?
代码:
struct ysf {
int id;
struct ysf* next;
};
typedef struct ysf* PNode;
typedef struct ysf* LinkList;
LinkList CreateList()
{
LinkList head = (LinkList)malloc(sizeof(struct ysf));
PNode p = head;
int c = 0;
while (c < 30)
{
c++;
PNode q = (LinkList)malloc(sizeof(struct ysf));
q->id = c;
q->next = NULL;
p->next = q;
p = p->next;
}
p->next = head ->next;
return head;
}
int main()
{
LinkList head = CreateList();
PNode p = head;
PNode q = p->next;
int i = 0, j;
while (i < 15)
{
for (j = 1; j <= 9; j++)
{
if (j == 9)
{
i++;
printf("%d\n",q->id);
p->next = q->next;
q = p->next;
}
else
{
p = q;
q = q->next;
}
}
}
}
0x05:链表多项式
分值:35
题目描述:
用链表表示多项式,并实现多项式的加法运算。
输入格式:
输入在第一行给出第一个多项式POLYA的系数和指数,并以0,0 结束第一个多项式的输入;在第二行出第一个多项式POLYB的系数和指数,并以0,0 结束第一个多项式的输入。
输出格式:
对每一组输入,在一行中输出POLYA+POLYB和多项式的系数和指数。
输入样例:
5,0 2,1 1,6 8,15 0,0
2,1 3,6 4,8 0,0
输出样例:
5,0 4,6 4,8 8,15
代码:
#include<stdio.h>
#include<stdlib.h>
struct polynomial {
int coefficient;
int index;
struct polynomial* next;
};
typedef struct polynomial* PNode;
typedef struct polynomial* LinkList;
LinkList CreateList()
{
LinkList head = (LinkList)malloc(sizeof(struct polynomial));
PNode p = head;
int coefficient;
int index;
scanf("%d,%d", &coefficient, &index);
while (coefficient != 0)
{
PNode q = (PNode)malloc(sizeof(struct polynomial));
q->coefficient = coefficient;
q->index = index;
q->next = NULL;
p->next = q;
p = p->next;
scanf("%d,%d", &coefficient, &index);
}
return head;
}
void print(LinkList head)
{
PNode p = head->next;
while (p!= NULL)
{
printf("%d,%d ", p->coefficient, p->index);
p = p->next;
}
}
void sort(LinkList head)
{
PNode last = head->next;
PNode q, l;
int temp;
while (last->next != NULL) { last = last->next; }
while (last != head->next)
{
q = head->next;
l = q->next;
while (1)
{
if (q->index > l->index)
{
temp = l->coefficient;
l->coefficient = q->coefficient;
q->coefficient = temp;
temp = l->index;
l->index = q->index;
q->index = temp;
}
if (l == last)
{
last = q;
break;
}
else
{
q = l;
l = l->next;
}
}
}
}
LinkList add_Up(LinkList POLYA , LinkList POLYB)
{
LinkList list;
PNode ps;
PNode pa = POLYA->next;
PNode pb = POLYB->next;
PNode p;
PNode q;
PNode l;
if (pa->index > pb->index)
{
ps = POLYB;
p = pb;
q = pa;
}
else
{
ps = POLYA;
p = pa;
q = pb;
}
list = ps;
l = p->next;
while (l!= NULL && q != NULL)
{
if (p->index == q->index)
{
p->coefficient = p->coefficient + q->coefficient;
q = q->next;
}
else if (l->index <= q->index)
{
ps = p;
p = l;
l = l->next;
}
else if (p->index < q->index && l->index > q->index)
{
PNode s = (PNode)malloc(sizeof(struct polynomial));
s->coefficient = q->coefficient;
s->index = q->index;
s->next = l;
p->next = s;
ps = p;
p = l;
ps = p;
l = l->next;
q = q->next;
}
}
while (q != NULL)
{
PNode s = (PNode)malloc(sizeof(struct polynomial));
s->coefficient = q->coefficient;
s->index = q->index;
s->next = NULL;
p->next = s;
p = s;
q = q->next;
}
p = list;
q = list->next;
while (q != NULL)
{
if (q->coefficient == 0)
{
p->next = q->next;
free(q);
q = p->next;
}
else
{
p = q;
q = q->next;
}
}
return list;
}
int main()
{
LinkList p = CreateList();
LinkList q = CreateList();
sort(p);
sort(q);
LinkList l = add_Up(p, q);
print(l);
}