当前位置:网站首页>C语言之最小数
C语言之最小数
2022-07-02 06:34:00 【FF小迷糊吖~】
键盘输入N(N ≤ 20)个0-9数字,你可以以任意顺序排列这些数字,但必须全部使用。目标是使得最后得到的数尽可能小(注意0不能做首位)。
例如:
给定两个0,两个1,三个5,一个8,我们得到的最小的数就是10015558。
编程要求:
1、N的取值:N ≤ 20;
2、输入的数中至少包含一个非零数;
3、请利用数组的方式实现;
4、请严格按照输出效果输出;(影响成绩评定)
5、 任务提交代码时需保证其符合行业代码规范,需进行必要的缩进及换行。(影响成绩评定)
运行效果1:
请输入数字个数N:6
请输入6个数字:0 1 7 2 0 3
组成的最小数是:100237
请按任意键继续. . .
运行效果2:
请输入数字个数N:8
请输入8个数字:0 1 5 5 8 1 0 5
组成的最小数是:10015558
请按任意键继续. . .
#include <stdio.h>
int main(void)
{
int N ,arr[20] ,i ,j ,temp;
printf("请输入数字个数N:");
scanf("%d" ,&N);
printf("请输入%d个数字:", N);
for (i = 0; i < N; i++)
scanf("%d" ,&arr[i]);
for (i = 0; i < N - 1; i++)
for(j = 0; j < N - i -1 ; ++j)
{
if(arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
for (i = 0; i < N; i++)
if(arr[i] != 0)
{
temp = arr[i];
arr[i] = arr[0];
arr[0] = temp;
break;
}
printf("组成的最小数是:" );
for (i = 0; i < N; i++)
printf("%d" ,arr[i]);
printf("\n");
return 0;
}
#include <stdio.h>
int main(void)
{
int a[21];
int n,i,j,m;
printf("请输入数字的个数N: ");
scanf("%d",&n);
printf("请输入%d个数字: ",n);
for(i=0;i<n;++i){
scanf("%d",&a[i]);
}
for(i=0;i<n-1;++i){
for(j=0;j<n-1;++j){
if(a[j]>a[j+1]){
m=a[j];
a[j]=a[j+1];
a[j+1]=m;
}
}
}
for(i=0;a[i]==0;++i);
m=a[i];
a[i]=a[0];
a[0]=m;
printf("组成最小的数是: ");
for(i=0;i<n;++i){
printf("%d",a[i]);
}
printf("\n");
return 0;
}
边栏推荐
- 【Go实战基础】gin 如何验证请求参数
- Knowledge points are very detailed (code is annotated) number structure (C language) -- Chapter 3, stack and queue
- ClassFile - Attributes - Code
- 概率还不会的快看过来《统计学习方法》——第四章、朴素贝叶斯法
- Watermelon book -- Chapter 6 Support vector machine (SVM)
- JDBC回顾
- 数构(C语言--代码有注释)——第二章、线性表(更新版)
- 聊聊消息队列高性能的秘密——零拷贝技术
- Typora安装包分享
- [staff] time sign and note duration (full note | half note | quarter note | eighth note | sixteenth note | thirty second note)
猜你喜欢

队列管理器running状态下无法查看通道

【Go实战基础】gin 如何自定义和使用一个中间件

MySQL multi column in operation

View the port of the application published by was

微服务实战|原生态实现服务的发现与调用

微服务实战|手把手教你开发负载均衡组件

Flink - use the streaming batch API to count the number of words

洞见云原生|微服务及微服务架构浅析
![[go practical basis] how to verify request parameters in gin](/img/de/50db131d6993e5d955e3416c667c4c.png)
[go practical basis] how to verify request parameters in gin
![[staff] time mark and note duration (staff time mark | full note rest | half note rest | quarter note rest | eighth note rest | sixteenth note rest | thirty second note rest)](/img/7f/2cd789339237b7a881bfed7b7545a9.jpg)
[staff] time mark and note duration (staff time mark | full note rest | half note rest | quarter note rest | eighth note rest | sixteenth note rest | thirty second note rest)
随机推荐
MySQL error: unblock with mysqladmin flush hosts
Redis sorted set data type API and application scenario analysis
Flink - use the streaming batch API to count the number of words
Chrome user script manager tempermonkey monkey
[staff] time sign and note duration (full note | half note | quarter note | eighth note | sixteenth note | thirty second note)
Bold prediction: it will become the core player of 5g
Chrome用户脚本管理器-Tampermonkey 油猴
自定義Redis連接池
Timed thread pool implements request merging
What are the differences between TP5 and laravel
I've taken it. MySQL table 500W rows, but someone doesn't partition it?
Long summary (code with comments) number structure (C language) -- Chapter 4, string (Part 1)
[go practical basis] how to install and use gin
知识点很细(代码有注释)数构(C语言)——第三章、栈和队列
Matplotlib swordsman line - first acquaintance with Matplotlib
Say goodbye to 996. What are the necessary plug-ins in idea?
定时线程池实现请求合并
Taking the upgrade of ByteDance internal data catalog architecture as an example, talk about the performance optimization of business system
【Go实战基础】gin 如何自定义和使用一个中间件
洞见云原生|微服务及微服务架构浅析