XJTU第十三周大计基编程作业

1.
字符线性表
________________________________________

创建一个字符线性表(顺序表),并实现其基本操作(如插入,查找,删除,输出等)。应用该线性表,将键盘输入的一行字符插入表中,然后输出表中所有字符及表长;再输入一个字符,从表中删除该字符(重复出现应进行多次删除),最后再次输出表中所有字符及表长。
输入样例:
ABCBBDEF 12XYZBA
B
输出样例:
ABCBBDEF 12XYZBA
16
ACDEF 12XYZA
12


样例输入:
A BCBBDEF 12XYZB A
B
样例输出:
A BCBBDEF 12XYZB A
18
A CDEF 12XYZ A
14

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct List
{
	char* List;
	int length;
	int maxlength;
}L;
void main()
{
	const MAXLEN = 100;
	L* CreateList(int len);
	L* sqlist1;
	sqlist1 = CreateList(MAXLEN);
	gets(sqlist1->List);
	sqlist1->length = strlen(sqlist1->List);
	printf("%s\n%d\n", sqlist1->List, sqlist1->length);
	char a;
	scanf_s("%c", &a);
	deletelist(sqlist1, sqlist1->length, a);
	printf("%s\n%d\n", sqlist1->List, sqlist1->length);
}
L* CreateList(int len)
{
	L* sqlist1 = (L*)malloc(sizeof(L));
	if (sqlist1)
	{
		sqlist1->List = (char*)malloc(len * sizeof(char));
		if (sqlist1->List == NULL)
			return NULL;
		sqlist1->maxlength = len;
	}
}
deletelist(L* sqlist1, int len, char a)
{
	int i;
	for (i = 0; i < sqlist1->length; i++)
	{
		if (sqlist1->List[i] == a)
		{
			for (int j = i; j < sqlist1->length; j++)
				sqlist1->List[j] = sqlist1->List[j + 1];
			sqlist1->length--;
			i = i--;
		}
	}
	return sqlist1->length;
}

2.
逆序函数
________________________________________

编写函数,函数原型如下:
void fun(int n,char res[]);
函数功能是将整数n的各位数字逆序排列,存放到res字符数组中。
例如整数1035,逆序后为5301
程序测试举例,如输入:
9680200
则输出:
0020869
________________________________________
样例输入:
123456
样例输出:
654321

#include<stdio.h>
#include<string.h>
void fun(int n, char res[]);
int main()
{
	char res[20] = {' '};
	int n;
	scanf_s("%d", &n);
	fun(n, res);
}
void fun(int n, char res[])
{
	int i = 0;
	while (1)
	{
		res[i] = n % 10 + '0';
		printf("%c", res[i]);
		i++;
		n = n / 10;
		if (n== 0)
			break;
	}

3.
新兵队列训练(选做)
________________________________________

某部队进行新兵队列训练,将新兵从1开始按顺序依次编号,并排成一行横队,训练的规则如下:从头开始1至2报数,凡报到2的出列,剩下的向小序号方向靠拢,再从头开始进行1至3报数,凡报到3的出列,剩下的向小序号方向靠拢,继续从头开始进行1至2报数,以后从头开始轮流进行1至2报数、1至3报数直到剩下的人数不超过三人为止。编写程序,输入数N为最开始的新兵人数(20 < N < 6000),输出剩下的新兵最初的编号。
输入样例:
21
输出样例:
1 7 19
________________________________________
样例输入:
21
样例输出:
1 7 19

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

int do_remove(int a[], int t, int c)
{
    int i, j;

    for (i = j = 0; i < t; i++)
    {
        if(i % c != c - 1)
            a[j++] = a[i];
        if (t - i + j == 3) break;
    }
    for (; i < t; i++)
        a[j++] = a[i];
    return j;
}

int main()
{
    int n, i, c = 2;
    int* a;

    scanf_s("%d", &n);
    a = (int*)malloc(n * sizeof(int));
    for (i = 0; i < n; i++)
        a[i] = i + 1;
    while (n > 3)
    {
        n = do_remove(a, n, c);
        if (c == 2) c = 3;
        else c = 2;
    }
    printf("%d %d %d\n", a[0], a[1], a[2]);

    return 0;
}

**4.
反转字符串


题目描述:**
编写字符串反转函数void mystrrev(char string[]),该函数的功能为将指定字符串中的字符顺序颠倒排列,然后再编写主函数调用该反转函数验证之。
输入输出格式:
输入:任意一个字符串,可能含有空格
输出:逆序后的字符串
样例:
输入:
Hello,everyone
输出:
enoyreve,olleH
________________________________________
样例输入:
123 456 7890
样例输出:
0987 654 321

#include<stdio.h>
#include<string.h>//需要使用strlen函数求数组的长度;
void mystrrev(char string[])
{
	int len = strlen(string);//求string数组的长度;
	for(int i = 0; i <= len/2; i++)//从第一项开始,每一项与对应一项进行交换;从0开始,到中间项结束;
	{
		char temp;
		temp = string[i];
		string[i] = string[len - i - 1];
		string[len - i - 1] = temp;
	}
}
int main()
{
	char string[1000];
	gets(string);//有可能会存在空格,所以用gets函数来接收;
	mystrrev(string);
	printf("%s", string);
	return 0;
}

5.
右移
________________________________________

题目描述:
一个数组A中存有n(n>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移m(m>=0)个位置,即将A中的数据由(A0 A1……An-1)变换为(An-m …… An-1 A0 A1……An-m-1)(最后m个数循环移至最前面的m个数)。输入n ( 1<=n<=100)、m(m>=0)及n个整数,输出循环右移m位以后的整数序列。
输入输出格式:
输入:
第一行依次输入数组元素的个数和移动的位数
第二行输入数组
输出:
移位后的数组
样例
6 2
1 2 3 4 5 6
5 6 1 2 3 4
________________________________________
样例输入:
3 4
1 2 3
样例输出:
3 1 2

#include<stdio.h>
int main()//直接在主函数中完成;
{
    int n, m;
    scanf_s("%d %d", &n,&m);
    int a[100] = { " " }, a1[100] = { " " };
    int i;
    static int j = 0;//j声明为静态,下面的两次循环中连接使用;
    for (i = 0; i < n; i++)
        scanf_s("%d", &a[i]);
    if (m > n)//m如果大于n,说明是经过了k次循环的移动;
    {
        m = m % n;//此时将m置为余数;
        for (i = n - m; i < n-1; i++)
        {
            a1[j] = a[i];
            j = j + 1;
        }
        for (i = 0; i < n - m; i++)//再从第一项开始顺序赋给a1;
        {
            a1[j] = a[i];
            j++;
        }
    }
    else
    {
        for (i = n - m; i < n; i++)
        {
            a1[j] = a[i];
            j = j + 1;
        }
        for (i = 0; i < n - m; i++)
        {
            a1[j] = a[i];
            j++;
        }
    }
    for (i = 0; i < n; i++)
        printf("%d ", a1[i]);
}

6.
字符串查询
_______________________________________

题目描述:
编写字符查找函数,函数原形为:
int mystrchr(char string[],char c);
功能是在字符串string中查找c中的字符,如果找到则返回该字符的索引值(即下标,否则返回-1)。
编写主函数进行测试。输入一个字符串数据(长度<80)和一个字符,输出该字符的序号(从0开始)。
样例:
输入为:
asdffg & *123 hjkl
f
输出为:
3
________________________________________
样例输入:
Xx Yy Zz Ax
W
样例输出:
-1

#include<stdio.h>
int mystrchr(char string[], char c)
{
    int result;
    for (int i = 0; i < 80; i++)
    {
        if (string[i] == c)//依题意,找出一个即退出循环;
        {
            result = i;
            break; 
        }
        if (string[i] == '\0')//当为0时,说明检索完毕;
        {
            result = -1; 
            break;
        }
    }
    return result;
}
int main()
{
    char string[80], c;
    gets(string);
    scanf_s("%c", &c);
    printf("%d", mystrchr(string, c)); 
    return 0;
}

7.
编程题 - Helloworld for U 【选做】
________________________________________

Given any string of N (>=5) characters, you are asked to form the characters into the shape of U. For example, “helloworld!” can be printed as:
h !
e d
l l
lowor
That is, the characters must be printed in the original order, starting top-down from the left vertical line with n1characters, then left to right along the bottom line with n2 characters, and finally bottom-up along the vertical line with n3 characters. And more, we would like U to be as squared as possible – that is, it must be satisfied that n1 = n3 = max { k| k <= n2 for all 3 <= n2 <= N } with n1 + n2 + n3 - 2 = N.
Input Specification: Each input file contains one test case. Each case contains one string with no less than 5 and no more than 80 characters in a line. The string contains no white space. Output Specification: For each test case, print the input string in the shape of U as specified in the description.
Sample Input:
helloworld!
Sample Output:
h !
e d
l l
lowor

________________________________________
样例输入:
NextNumber:2
样例输出:
N 2
e :
x r
tNumbe

#include<stdio.h>
#include<string.h>
int main()
{
    char a[100];
    gets(a);
    int len = strlen(a);//求出总长度;
    int side = (len + 2) / 3, mid = len - side * 2;//side为两边长度(包括底部两个),mid表示中间数字(不包括底部两个);
    int i,j;
    for (i = 0; i < side-1 ; i++)//输出side:共side-1行;
    {
        printf("%c", a[i]);
        for (j = 0; j < mid; j++)//中间有mid个空格;
            printf(" ");
        printf("%c\n", a[len - 1 - i]);
    }
    for (i = side-1; i < side+mid + 1; i++)//下部所有的数字,下标从side-1开始,到side+mid结束;
        printf("%c", a[i]);
    return 0;
}

。。。。。。
中文注释什么的统统没写,就等着复习的时候尽情浪费时间吧!
。。。。。。

猜你喜欢

转载自blog.csdn.net/Jefferymeng/article/details/106343839
今日推荐