当前位置:网站首页>12月1日作业
12月1日作业
2022-07-22 18:07:00 【我的博尔赫斯】

这道题目说给出n个数,要我们去重并排序,注意一下这里输入的是正整数,也就是说输入的是不为零的数。
对于这种要求去重的题目我们可以用下面这种方法做
arr[k] = k;这里我们直接把k赋值给对应下标的数组,这样的话重复的数会直接剩下一个,同时数的排列也是从小到大,一举两得。
具体思路:先创建一个足够大的数组,将输入的序列放到对应下标数组,这是输入部分。
然后将数组中不为零的元素打印出来即可
具体代码:
//法一
int main()
{
int n = 0;
int k = 0;
int tmp = 0;
//指定输入多少个数
scanf("%d", &n);
int arr[10000] = { 0 };
//输入
for (int i = 0; i < n; i++)
{
scanf("%d", &k);
arr[k] = k;
}
for (int i = 0; i < 10000; i++)
{
if (arr[i] != 0)
printf("%d ", arr[i]);
}
}
这道题目不难,主要是要找到函数关系。
我们可以知道小乐乐 需要上楼的时间是

按照这个方程直接写代码
//int main()
//{
// int n = 0;
// scanf("%d", &n);
// //计算小乐乐还要等几波
// int wait = n / 12;
// int time = (wait * 4) + 2;
// printf("%d", time);
// return 0;
//}
这道题目有一点技巧性,需要思考。
我们想一想我们想把一个矩阵转置打印,是不是有两种情况,一种是我把矩阵先转置,然后打印。还有一种是我们直接转置打印矩阵,并不改变矩阵。
那么哪种更简单呢?肯定是第二种了。
下面我们来看下具体思路:输入矩阵,然后打印
具体代码:
//int main()
//{
// int m = 0;
// int n = 0;
// int arr[10][10] = { 0 };
// scanf("%d %d", &n, &m);
// //输入初始矩阵
// for (int i = 0; i < n; i++)
// {
// for (int j = 0; j < m; j++)
// {
// scanf("%d ", &arr[i][j]);
// }
//
// }
// //打印+转置
// for (int i = 0; i < m; i++)
// {
// for (int j = 0; j < n; j++)
// {
// printf("%d", arr[j][i]);
// }
// }
// return 0;
//}这里的打印+转置是精髓之处,把矩阵的行和列调换,


int main()
{
int count = 0;
int n = 0;
scanf("%d", &n);
int arr[50] = { 0 };
for (int i = 0; i < n; i++)
{
scanf("%d ", &arr[i]);
}
int k = 0;
scanf("%d", &k);
for (int i = 0; i < n; i++)
{
if (arr[i] == k)
{
count++;
if (i < n - 1)
{
for (int j = i; j < n; j++)
{
arr[j] = arr[j + 1];
}
i--;
}
}
}
for (int i = 0; i < n - count; i++)
{
printf("%d ", arr[i]);
}
return 0;
}代码思路:

边栏推荐
猜你喜欢
随机推荐
合肥工业大学信息论与编码课程设计,含代码,可视化界面,课设报告
Leetcode-343. integer splitting
WPS MathType installation error 53
DOM—节点操作(二)
ba_ Shell learning summary
Leetcode-646. longest number pair chain
[reprint] CentOS 7 install MySQL + MySQL common commands + docker run MySQL
JQ data is output in the background, and the assembled record table has no effect on pop-up events
Druid source code read the recycle process of 7-druiddatasource
js--Date對象&三元錶達式
Jena fuseki online update database
Using sed to remove ASCII control characters from files
leetcode-204.计数质数
Correct the bug that relfinder draws lines in the wrong direction
代码随想录笔记_数组_977有序数组的平方
Druid source code read the getconnection process of 4-druiddatasource
ES6的新增属性
Druid source code read the init process of 2-druiddatasource
改正 RelFinder 画线方向错误的 bug
jupyter import包失败







