大计基编程题(第十三周)

排序算法

如果不太了解各种排序算法可以参考菜鸟教程(希望对你有帮助)

菜鸟教程排序算法

题目一

题目描述

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

题解

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>

int main() {
	char save[1000];//创建一个字符线性表
	gets(save);
	char save1[1000];
	char b,j=0;
	scanf("%c", &b);
	for (size_t i = 0; i < strlen(save); i++)
	{
		if (save[i]!=b)
		{
			save1[j] = save[i];
			j++;
		}
	}
	save1[j] = '\0';
	printf("%s\n%d\n", save, strlen(save));
	printf("%s\n%d", save1, j);
	return 0;
}

题目二

题目描述

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

题解

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>

void fun(int n, char res[]) {
	int i = 0;
	while (n!=0)
	{
		res[i] = (n % 10 + '0');
		n /= 10;
		i++;
    }
}
int main() {
	int num;
	scanf("%d", &num);
	char res[100] = { '\0' };
	fun(num, res);
	printf("%s", res);
	return 0;
}

题目三

题目描述

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

输入样例:
21
输出样例:
1 7 19

题解

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>

//删除数组中序号为(2n-1)的数
int delete2(int array[],int n) { 
	int array1[6000];
	int num = n;
	for (size_t i = 0,j=0; i < n; i++)
	{
		if ((i + 1) % 2 != 0)
		{
			array1[j] = array[i];
			j++;
		}
		else num--;
	}
	for (size_t i = 0; i < num; i++)
	{
		array[i] = array1[i];
	}
	return num;
}
//删除数组中序号为2n的数
int delete3(int array[], int n) {
	int num=n;
	int array1[6000];
	for (size_t i = 0, j = 0; i < n; i++)
	{
		if ((i + 1) % 3 != 0)
		{
			array1[j] = array[i];
			j++;
		}
		else num--;
	}
	for (size_t i = 0; i < num; i++)
	{
		array[i] = array1[i];
	}
	return num;
}
int main() {
	int num;
	scanf("%d", &num);
	int n = num;
	int array[6000];
	for (size_t i = 0; i < num; i++)
	{
		array[i] = i+1;
	}
	while (n>3)
	{
		n = delete2(array,n);
		if (n<=3)
		{
			break;
		}
		n = delete3(array,n);
	}
	for (size_t i = 0; i < n-1; i++)
	{
		printf("%d ", array[i]);
	}
	printf("%d", array[n-1]);
}

题目四

题目描述

题目描述:

编写字符串反转函数void mystrrev(char string[]),该函数的功能为将指定字符串中的字符顺序颠倒排列,然后再编写主函数调用该反转函数验证之。

输入输出格式:
输入: 任意一个字符串,可能含有空格
输出: 逆序后的字符串

样例:
输入:
Hello,everyone
输出:
enoyreve,olleH

题解

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
void mystrrev(char string[]) {
	char string1[1000];
	int i = 0;
	for (i; i < strlen(string); i++)
	{
		string1[i] = string[strlen(string) - 1 - i];
	}
	for (int j=0;  j< i; j++)
	{
		string[j] = string1[j];
	}
}
int main() {
	char string[1000];
	gets(string);
	mystrrev(string);
	printf("%s", string);
}

题目五

题目描述

题目描述:

一个数组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

题解

#include<stdio.h>
int main(){
  int n,m;
  scanf("%d %d",&n,&m);
  int a[100];
  for(int i=0;i<n;i++){
    scanf("%d",&a[i]);
  }
  for(int i=1;i<=m;i++){ //拆分成m次每次只向右移动一位
    int temp=a[n-1];
    for(int j=n-1;j>0;j--){
      a[j]=a[j-1];
    }
    a[0]=temp;
  }
  for(int i=0;i<n-1;i++){
    printf("%d ",a[i]);
  }
  printf("%d",a[n-1]);
  return 0;
}

题目六

题目描述

题目描述:
编写字符查找函数,函数原形为:
int mystrchr(char string[],char c);
功能是在字符串string中查找c中的字符,如果找到则返回该字符的索引值(即下标,否则返回-1)。

编写主函数进行测试。输入一个字符串数据(长度<80)和一个字符,输出该字符的序号(从0开始)。

样例:
输入为:
asdffg & *123 hjkl
f
输出为:
3

题解

#include<stdio.h>
int mystrchr(char string[], char c) //声明函数
{
    int result;
    for (int i = 0; i < 80; i++)
    {
       if (string[i] == c)
         {
           return i;
         }
      }
      return -1;
}
       
int main()
{
    char string[80], c;
    gets(string); scanf("%c", &c); //依次输入数组和字符
    printf("%d", mystrchr(string, c)); //mystrchr(string, c)就是函数中result的值
    return 0;
}

题目七

题目描述

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

题解

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
int main()
{
	char a[90];
	gets(a);
		int t = (strlen(a) + 2) / 3;
		for (int i = 0; i < t; i++)
		{
			printf("%c", a[i]);
			if (i != t - 1)
			{
				for (int j = 0; j < (strlen(a) - 2 * t); j++)
					printf(" ");
				printf("%c", a[strlen(a) - 1 - i]);
			}
			else for (int k = t; k <= (strlen(a) - t); k++)
				printf("%c", a[k]);
			printf("\n");
		}
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值