您好,欢迎来到爱站旅游。
搜索
您的当前位置:首页1、编写一个程序从键盘输入10个数,要求输出其中最小的

1、编写一个程序从键盘输入10个数,要求输出其中最小的

来源:爱站旅游


1、编写一个程序从键盘输入10个数,要求输出其中最小的。

#include

main()

{

int a[10]; /*定义数组a有10个元素,使用0~9*/

int i,min;

for(i=0;i<=9;i++) /*键盘输入10个数据*/

scanf(\"%d\

printf(\"The old numbers are:\\n\");

for(i=0;i<=9;i++) /*屏幕输出输入的数据*/

printf(\"%5d\

min=a[0];

for(i=1;i<=9;i++) /*相邻元素进行比较,如果前面比后面的大则进行交换*/

if(min>a[i]) min=a[i];

printf(\"\\nThe minimum number is:%d\\n\屏幕输出最小的数据

return 0;

}

2、编写一个函数根据以下公式计算s,计算结果作为函数值返回;n通过形参传入。

s=

#include

double f1(int n);

void main()

{

int n=1;

printf(\"Input the value of n\\n\");

scanf(\"%d\

printf(\"%lf\

}

double f1(int n)

{int i;

double term,s=0;

for(i=1;i<=n;i++)

{ term=1.0/(2*i-1);

s+=term;}

return(s);

}

3、输出1000年(包括1000年)到1999年之间的所有闰年,要求每三个一行,分行输出。

#include

void main()

{

int i=0;

int year,leap;

for(year=1000;year<2000;year++)

{

if (year%4==0)

if(year%100==0)

if(year%400==0)

leap=1;

else

leap=0;

else

leap=1;

else

leap=0;

if(leap)

{i++;

printf(\"%d\\

if (i%3==0) printf(\"\\n\");

}

}

}

4、编写程序打印所有的“水仙花数”。“水仙花数”指一个三位数,其各位数字立方和等于该数本身,例如153是一个“水仙花数”,因为 153=1×1×1+3×3×3+5×5×5。

#include

void main()

{

int i,j,k,n;

printf(\"result is:\");

for(n=100;n<1000;n++)

{

i=n/100;

j=(n-i*100)/10;

k=n%10;

if(n==i*i*i+j*j*j+k*k*k)

printf(\"%d \

}

printf(\"\\n\");

}

5. 从键盘输入3个整数,求其中的最大数和最小数,并输出结果。

# include \"stdio.h\"

void main()

{

int x, y, z, max, min;

printf(\"Please input three integer number:\");

scanf(\"%d %d %d\

min = max = x;

if(x==y && y==z)

printf(\"x = y = z, max=min=%d\\n\

else

{

if (y>max)

max = y;

if (z>max)

max = z;

if (ymin = y;

if (zmin = z;

printf(\"max=%d, min=%d\\n\

}

}

6. 从键盘上输入一个3*3的整数矩阵,求其各行的平均值并输出,输出时保留两位小数。

#include

void main()

{

int a[3][3],b[3]={0};

int i,j;

printf(\"请输入一个三行三列的整数矩阵:\\n\");

for(i=0;i<3;i++)

for(j=0;j<3;j++)

scanf(\"%d\j]);

for(i=0;i<3;i++)

for(j=0;j<3;j++)

b[i]=b[i]+a[i][j];

for(i=0;i<3;i++)

printf(\"该矩阵第%d行元素的平均值是:%.2f\\n\

}

7. 输出x2的值,x取值从0到10。

#include \"stdio.h\"

void main()

{

int x;

for(x=0; x<=10; x++)

printf(\"square(%d)=%d\\n\

}

8. 从键盘上输入一个3*4的整数矩阵,要求输出其最大元素的值,以及它的行号和列号。

#include \"stdio.h\"

void main()

{

int a[3][4], max, i, j, row, colum;

printf(\"请输入3*4的整数矩阵:\\n\");

for(i=0;i<3;i++)

for(j=0;j<4;j++)

scanf(\"%d\j]);

max=a[0][0];

row=0;

colum=0;

for(i=0;i<3;i++)

for(j=0;j<4;j++)

if (a[i][j]>max)

{

max=a[i][j];

row=i;

colum=j;

}

printf(\"max=%d, row=%d, colum=%d \\n\

}

9. 1. 求出10至1000之内能同时被2、3、7整除的数,并输出。

#include

main()

{

int i;

for(i=10;i<1000;i++)

if (i%2==0&&i%3==0&&i%7==0)

printf(“%d,” i);

}

10. 输入一字符串,检查是否回文 (回文是指正反序相同,如,LeveL),若是则输出“Yes”,否则输出“No”。

#include

#include

main()

{

int i,j,tag=0;

char ch[50];

printf(“Please input a string:”);

scanf(“%s”,ch);

j=strlen(ch);

for (i=0;iif(ch[i]==ch[j-i])

tag=1;

if (tag)

printf(“Yes.\\n ”);

else

printf(“No. \\n”);

}

11. 输入三个整型数,找出其中数值最大者并输出。

#include

main()

{

int a,b,c,max;

printf(”Please input 3 integer:”);

scanf(“”%d%d%d”,&a,&b,&c);

max=a;

if (maxif (maxprintf(“The max integer is %d.”,max);

}

12. 编写程序在屏幕上显示如下图形:

1 2 3 4 5

5 1 2 3 4

4 5 1 2 3

3 4 5 1 2

2 3 4 5 1

#include

#include

main()

{

int i,j;

for (i=0;i<5;i++)

{ for(j=1;j<=5;j++)

printf(“%3d”,( 6 – i + j ) %6 );

printf(”\\n”);

}

}

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- azee.cn 版权所有

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务