当前位置:网站首页>C语言编程训练题目:左旋字符串中的k个字符、小乐乐与欧几里得、打印箭型图案、公务员面试、杨树矩阵
C语言编程训练题目:左旋字符串中的k个字符、小乐乐与欧几里得、打印箭型图案、公务员面试、杨树矩阵
2022-07-24 17:29:00 【eat_sleep_play( )】
目录

一、左旋字符串中的K个字符。
题目描述:ABCDE 左旋一个字符得到BCDEA,左旋两个得到CDEAB
难易程度:
#include<stdio.h>
int my_strlen(char* str)//求字符个数
{
int count = 0;//记录字符的个数
while (*str != '\0')
{
count++;
str++;
}
return count;
}
void left_rotate(char arr[], int k)
{
int i = 0;
int len = my_strlen(arr);
for (i = 0; i < k; i++)
{
//左旋字符步骤
//1.将要旋转的字符先存起来
char tmp = arr[0];
//2.将剩下的字符向前移动
int j = 0;
for (j = 0; j < len - 1; j++)
{
arr[j] = arr[j + 1];
}
//3.把之前存好的字符存放到数组的空位中
arr[len - 1] = tmp;
}
}
int main()
{
int k = 0;
scanf("%d", &k);
char arr[] = "ABCDEF";
left_rotate(arr, k);
printf("左旋的结果:%s\n", arr);
return 0;
}二、小乐乐与欧几里得(求最大公约数)
题目描述:小乐乐最近在课上学习了如何求两个正整数的最大公约数与最小公倍数,但是他竟然不会求两个正整数的最大公约数与最小公倍数之和,请你帮助他解决这个问题。
难易程度:
#include<stdio.h>
int main()
{
long long a = 0;
long long b = 0;
long long comax = 0;
long long comin = 0;
long long k = 0;
scanf("%lld %lld", &a, &b);
k = a * b;
while (a && b)
{
if (a > b)
{
a = a % b;
}
else
{
b = b % a;
}
}
comax = a > b ? a : b;
comin = k / comax;
printf("%lld\n", comax + comin);
}三、打印箭型图案
题目描述:利用循环。使用 * 打印箭型图案
难易程度:
#include<stdio.h>
int main()
{
int n, i, j;
while (scanf("%d", &n) != EOF)
{
for (i = 0; i < n + 1; i++) //控制上半部分*输出(n+1行)
{
for (j = i; j < n; j++) //控制空格输出
{
printf(" ");
}
for (j = 0; j <= i; j++) //控制*输出
{
printf("*");
}
printf("\n"); //换行
}
for (i = 0; i < n; i++) //控制下半部分*输出(n行)
{
for (j = 0; j <= i; j++)
{
printf(" ");
}
for (j = i; j < n; j++)
{
printf("*");
}
printf("\n");
}
}
return 0;
}四、公务员面试
题目描述:公务员面试现场打分。有7位考官,从键盘输入若干组成绩,每组7个分数(百分制),去掉一个最高分和一个最低分,输出每组的平均成绩。(注:本题有多组输入)
难易程度:
#include <stdio.h>
int main()
{
int a = 0;
int max = 0;//最大值
int small = 100;//最小值
int sum = 0;
int count = 0;
while (scanf("%d", &a) != EOF)
{
if (a > max)//判定最高分
{
max = a;
}
if (a < small)//判定最低分
{
small = a;
}
sum += a;
count++;//计数器
if (count == 7)//计数器=7时代表一组的分数好了可以进行计算
{
printf("%.2f\n", (sum - max - small) / 5.0);
count = 0;//重置
max = 0;//重置
small = 100;//重置
sum = 0;//重置
}
}
return 0;
}五、杨氏矩阵
题目描述:
有一个数字矩阵,矩阵的每行从左到右是递增的,矩阵从上到下是递增的,请编写程序在这样的矩阵中查找某个数字是否存在。
要求:时间复杂度小于O(N);
难易程度:
#include<stdio.h>
find_k(int arr[3][3], int k, int* row, int* col)
{
int x = 0;//行
int y = *col - 1;//列
while (x <= *row && y >= 0)
{
//要查找的数字小于矩阵右上角的数字
if (k < arr[x][y])
{
//矩阵列数减1
y--;
}
else if (k > arr[x][y])
{
//矩阵行数减1
x++;
}
else
{
//要查找的数字等于矩阵右上角的数字 - 找到了
*row = x;
*col = y;
return 1;//找到了
}
}
*row = -1;
*col = -1;
return -1;//没找到
}
int main()
{
int arr[3][3] = { 1,2,3,4,5,6,7,8,9 };
int k = 0;//要查找的数字
scanf("%d", &k);
int row = 3;//行
int col = 3;//列
int ret = find_k(arr, k, &row, &col);
if (1 == ret)
{
printf("找到了,下标是:%d %d", row, col);
}
else
{
printf("找不到了\n");
}
return 0;
}
边栏推荐
- Summary of ROS master-slave communication experience
- Iftnews | Christie's launched its venture capital department, aiming at Web3 and metauniverse industries
- 二维卷积——torch.nn.conv2d的使用
- Hcip fourth day notes
- Baidu PaddlePaddle easydl x wesken: see how to install the "eye of AI" in bearing quality inspection
- 量化框架backtrader之一文读懂Indicator指标
- CDN (content delivery network) content distribution network from entry to practice
- 2022 Niuke summer multi school K - link with bracket sequence I (linear DP)
- Exception handling - a small case that takes you to solve NullPointerException
- Iqiyi Tiktok reconciled, Weibo lying gun?
猜你喜欢

The results of the second quarter online moving people selection of "China Internet · moving 2022" were announced

Colleges and universities have introduced 23 Philippine doctors with heavy funds, and the relevant departments have launched an investigation!

Today, I met a 38K from Tencent, which let me see the ceiling of the foundation

2022 Yangtze River Delta industrial automation exhibition will be held in Nanjing International Exhibition Center in October

Df2net 3D model deployment

Use Matplotlib to simulate linear regression

Exception handling - a small case that takes you to solve NullPointerException

Separation and merging of channels
![[matlab]: basic knowledge learning](/img/8c/d13597e402c55df6cbd5e008aef0a5.png)
[matlab]: basic knowledge learning

mysql 查询某字段中以逗号分隔的字符串的方法
随机推荐
wallys/IPQ8074A 4x4 2.4G 8x8 5G 802.11ax
2022 牛客暑期多校 K - Link with Bracket Sequence I(线性dp)
还在用Xshell?你out了,推荐一个更现代的终端连接工具!
Heuristic merging (including examples of general formula and tree heuristic merging)
It's time to consider slimming down your app
Opencv has its own color operation
Apachecon Asia 2022 opens registration: pulsar technology issues make a big debut
UFW port forwarding
什么是模糊理论,基础,流程
Niuke linked list solution record
2022 ranking list of database audit products - must see!
Six ways for JS to implement inheritance
[waiting for insurance] what does waiting for insurance rectification mean? What are the rectification contents?
Still developing games with unity? Then you're out. Try unity to build an answer system
PAT甲级——A + B 格式
电脑监控是真的吗?4个实验一探究竟
Image information is displayed by browser: data:image/png; Base64, + image content
AutoCAD - join merge command
实习报告1——人脸三维重建方法
Navicate connects Alibaba cloud (explanation of two methods and principles)