当前位置:网站首页>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;
}
边栏推荐
- Taro applet enables wxml code compression
- Tensorflow GPU installation
- LLDP兼容CDP功能配置
- Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform
- Oracle:CDB限制PDB资源实战
- Neon Optimization: an instruction optimization case of matrix transpose
- 2022 Google CTF SEGFAULT LABYRINTH wp
- NEON优化:性能优化经验总结
- NEON优化:矩阵转置的指令优化案例
- 云呐-工单管理制度及流程,工单管理规范
猜你喜欢
动态规划思想《从入门到放弃》
boot - prometheus-push gateway 使用
Niuke cold training camp 6B (Freund has no green name level)
2022 Google CTF segfault Labyrinth WP
Dynamic planning idea "from getting started to giving up"
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
How to manage distributed teams?
Do you understand this patch of the interface control devaxpress WinForms skin editor?
身体质量指数程序,入门写死的小程序项目
[牛客] B-完全平方数
随机推荐
Segmenttree
Lldp compatible CDP function configuration
"Exquisite store manager" youth entrepreneurship incubation camp - the first phase of Shunde market has been successfully completed!
boot - prometheus-push gateway 使用
mysql: error while loading shared libraries: libtinfo. so. 5: cannot open shared object file: No such
Informatics Olympiad YBT 1171: factors of large integers | 1.6 13: factors of large integers
自旋与sleep的区别
Gazebo的安装&与ROS的连接
Build your own website (17)
golang中的atomic,以及CAS操作
[case sharing] basic function configuration of network loop detection
table表格设置圆角
移植DAC芯片MCP4725驱动到NUC980
身体质量指数程序,入门写死的小程序项目
[batch dos-cmd command - summary and summary] - jump, cycle, condition commands (goto, errorlevel, if, for [read, segment, extract string]), CMD command error summary, CMD error
In rails, when the resource creation operation fails and render: new is called, why must the URL be changed to the index URL of the resource?
Taro2.* 小程序配置分享微信朋友圈
Boot - Prometheus push gateway use
[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)
负载均衡性能参数如何测评?