当前位置:网站首页>C语言实例_4
C语言实例_4
2022-07-06 17:39:00 【Vicky__3021】
1.排序问题
将十个数进行从大到小的顺序进行排列。
#include<stdio.h>
int main(void)
{
int i,j,t,a[11];
for(i=1;i<11;i++){
scanf("%d",&a[i]);
}
for(i=1;i<=9;i++){
for(j=i+1;j<=10;j++){
if(a[i]<a[j]){
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
for(i=1;i<=10;i++){
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
2.查找整数
给出一个包含n个整数的数列,问整数a在数列中的第一次出现是第几个。
#include<stdio.h>
int main(void)
{
int m,n,i,j;
scanf("%d",&m);
int a[m];
for(j=0;j<m;j++){
scanf("%d ",&a[j]);
}
scanf("%d",&n);
for(i=0;i<=m;i++){
if(a[i]==n){
printf("%d",i+1);
return 0;
}
}
printf("-1");
return 0;
}
3.计算数组中元素的最大值及其所在的行列下标值
按如下函数原型编程从键盘输入一个m行n列的二维数组,然后计算数组中元素的最大值及其所在的行列下标值。其中m和n的值由用户键盘输入。已知m和n的值都不超过10。
#include<stdio.h>
int main(void)
{
int a[10][10];
int max,row,col,i,j,m,n;
row = col - 1;
printf("Input m, n:");
scanf("%d,%d",&m,&n);
printf("Input %d*%d array:\n",m,n);
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",&a[i][j]);
}
}
max = a[0][0];
for(i=0;i<m;i++){
for(j=0;j<n;j++){
if(a[i][j]>max){
max=a[i][j];
row=i+1;
col=j+1;
}
}
}
if(m==1){
max=1;
row=1;
col=1;
}
printf("max=%d, row=%d, col=%d",max,row,col);
return 0;
}
4.删除最大值
输入10个互不相同的整数并保存在数组中,找到该最大元素并删除它,输出删除后的数组
#include<stdio.h>
#define N 10
int a[N];
int main(void){
int i;
for(i = 0;i < N;i++){
scanf("%d",&a[i]);
}
int maxa = a[0], k = 0;
for(i = 1;i < N;i++){
if(a[i]>maxa) maxa = a[i], k=i;
}
for(i = 0;i < N;i++){
if(i != k){
printf("%d ",a[i]);
}
}
printf("\n");
return 0;
}
5.杨辉三角
还记得中学时候学过的杨辉三角吗?具体的定义这里不再描述,你可以参考以下的图形:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
#include<stdio.h>
int a[10][10];
int main()
{
int num = 1;
for(int i = 0; i < 10; i ++) a[i][0] = 1;
for(int i = 1; i < 10; i ++)
{
for(int j = 1; j < 10; j ++)
{
if(j == num)
{
num ++;
a[i][j] = 1;
break;
}
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
}
}
num = 1;
for(int i = 0; i < 10; i ++)
{
for(int j = 0; j < 10; j ++)
{
if(j == num)
{
num ++;
break;
}
if(j == num - 1) printf("%d", a[i][j]);
else printf("%d ", a[i][j]);
}
printf("\n");
}
return 0;
}
边栏推荐
- 数据手册中的词汇
- Spark TPCDS Data Gen
- [JS] obtain the N days before and after the current time or the n months before and after the current time (hour, minute, second, year, month, day)
- Gnet: notes on the use of a lightweight and high-performance go network framework
- The cost of returning tables in MySQL
- Body mass index program, entry to write dead applet project
- Neon Optimization: performance optimization FAQ QA
- AI 从代码中自动生成注释文档
- JTAG debugging experience of arm bare board debugging
- 「笔记」折半搜索(Meet in the Middle)
猜你喜欢
go-zero微服务实战系列(九、极致优化秒杀性能)
免费白嫖的图床对比
ESP Arduino (IV) PWM waveform control output
Anfulai embedded weekly report no. 272: 2022.06.27--2022.07.03
Niuke cold training camp 6B (Freund has no green name level)
Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform
云呐|工单管理软件,工单管理软件APP
黑马笔记---创建不可变集合与Stream流
让我们,从头到尾,通透网络I/O模型
云呐-工单管理制度及流程,工单管理规范
随机推荐
Data type of pytorch tensor
阿里云中mysql数据库被攻击了,最终数据找回来了
mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such
Meet in the middle
界面控件DevExpress WinForms皮肤编辑器的这个补丁,你了解了吗?
golang中的atomic,以及CAS操作
2022 Google CTF segfault Labyrinth WP
斗地主游戏的案例开发
NEON优化:矩阵转置的指令优化案例
Taro2.* 小程序配置分享微信朋友圈
Maidong Internet won the bid of Beijing life insurance to boost customers' brand value
There is an error in the paddehub application
实现mysql与ES的增量数据同步
405 method not allowed appears when the third party jumps to the website
自旋与sleep的区别
2022 Google CTF SEGFAULT LABYRINTH wp
1123. 最深叶节点的最近公共祖先
【js】获取当前时间的前后n天或前后n个月(时分秒年月日都可)
[JS] obtain the N days before and after the current time or the n months before and after the current time (hour, minute, second, year, month, day)
Neon Optimization: summary of performance optimization experience