继续C语言作业程序 发现程序上传给大家批是好事 群众的眼睛是雪亮的 也感谢各位的批评指正。。
编写程序,输入一个整数x,按下式输出对应的y值。
程序代码:
#include<stdio.h>
int main(void)
{
int x = 0;
int y = 0;
printf("Please input x:");
scanf("%d",&x);
if (x < 0)
{
printf("Input may be wrong.");
return ;
}
if (x%2 == 0)
{
printf("2+4+6+...+%d=",x);
/* save mem and two dec is faster than +2 */
for (; x>=0; x--,x--)
{
y += x;
}
printf("%d",y);
}
else
{
printf("1+3+5+...+%d=",x);
for (; x>=0; x--,x--)
{
y += x;
}
printf("%d",y);
}
return 0;
}
输入一个不大于2×109(用long型数据)的正整数x,求这个数每位数字之和。例如,若输入33456781,输出37。
程序代码:
#include<stdio.h>
int main(void)
{
long x = 0;
int sum = 0; /* not too big */
printf("Please input x,x<=2^31-1:");
scanf("%ld",&x);
if (x < 0)
{
printf("Input may be wrong.");
}
while (x != 0)
{
sum += x%10;
x = (long)(x /10);
}
printf("Add up every number in x is %d",sum);
return 0;
}
键盘输入一组学生成绩,输入以-1作为输入的结束。统计这批数据的最大值,最小值,平均值。
程序代码:
#include<stdio.h>
#include<math.h>
#ifndef FSMLALL
#define FSMLALL 0.000001
#endif
int main(void)
{
float score = 0.0;
int num = 0;
float sum = 0.0;
float max = 0.0;
float min = 3e38; /* erery number is smaller than it */
printf("Please input a student's score(s) and ended by -1\n");
while (scanf("%f",&score),fabs(score + 1.0) >= FSMLALL) /* score != -1 */
{
num++;
sum += score;
if ((score - max) > FSMLALL) /* score > max */
{
max = score;
}
if ((min - score) > FSMLALL) /* min > score */
{
min = score;
}
}
printf("Max score is %f.Min score is %f.Average of scores is %f",max,min,(float)(sum/num));
return 0;
}
编写程序,使用循环输出如下图所示的数字塔图形。
程序代码:
#include<stdio.h>
#define N 8
/*
00000001
000000222
0000033333
00004444444
000555555555
0066666666666
07777777777777
888888888888888
*/
int main(void)
{
int n = 0;
int i = 1;
int j = 1;
for (i=1; i<=N; i++)
{
for (j=1; j<=N-i; j++)
{
putchar(' ');
}
for (j=1; j<=2*i-1; j++)
{
printf("%d",i);
}
putchar('\n');
}
for (i=N; i>=1; i--) /* just let i=N and dec i */
{
for (j=1; j<=N-i; j++)
{
putchar(' ');
}
for (j=1; j<=2*i-1; j++)
{
printf("%d",i);
}
putchar('\n');
}
return 0;
}
从键盘输入一个大于3的正整数,输出距离该数最近的素数。根据输入的数不同,此问题可能有一个答案(或者比输入的数大或者比输入的数小),也可能需要输出两个值(一个比输入的数大,一个比输入的数小,两个距离输入的数一样近)。
程序代码:
#include<stdio.h>
#include<math.h>
#include<stdbool.h> /* a C99 header */
/* return true if x is a prime */
bool _IsPrime(int x)
{
int i = 2;
for (i=2; i<=(int)sqrt(x); i++)
{
if (x%i == 0)
{
return false;
}
}
return true;
}
int main(void)
{
int n = 0;
int i = 0;
int bigpr = 0;
int smallpr = 0;
printf("Pls Input N(>3 AND MUST BE AN INTEGER):");
scanf("%d",&n);
if (n <= 3)
{
printf("Input may be wrong.");
return ;
}
for (i=n+1; ;i++)
{
if (_IsPrime(i))
{
bigpr = i;
break ;
}
}
for (i=n-1;i>=3; i--)
{
if (_IsPrime(i))
{
smallpr = i;
break ;
}
}
printf("Nearest prime is(are) ");
if ((bigpr-n)>(n-smallpr))
{
printf("%d",smallpr);
}
else
{
if((bigpr-n)<(n-smallpr))
{
printf("%d",bigpr);
}
else
{
printf("%d %d",smallpr,bigpr);
}
}
return 0;
}
键盘输入一个数给m,计算并返回满足表达式:1+(1+2)+(1+2+3)+(1+2+3+4)+……+(1+2+3+……+n)<=m最大的n。例如,当m=10000时,程序输出:n=38。
程序代码:
#include<stdio.h>
/* return 1+2+3+..+x and return -1 if wrong*/
/* the bigger the faster */
int _Addup(int x)
{
if (x<0)
{
/* wrong input */
return -1;
}
return (x*(++x)/2);
}
int main(void)
{
int m = 0;
int i = 1;
int sum = 0;
printf("Pls Input M:");
scanf("%d",&m);
for (i=1; ; i++)
{
sum += _Addup(i);
if (sum > m)
{
printf("Max n in 1+(1+2)+(1+2+3)+(1+2+3+4)+...+(1+2+3+……+n)<=m is %d",i);
break ;
}
}
return 0;
}
输入一串字符,以“#”作为输入结束标志,显示其中字母与数字字符的个数。
程序代码:
#include<stdio.h>
int main(void)
{
int ch_num=0,dig_num=0;
char ch = '\0';
ch_num = dig_num = 0;
do
{
putchar(ch=getch()); /* use getch() to input a string */
/* 完善程序,统计52个英文字母与十个数字字符的个数 */
if ((ch >= 'A' && ch <= 'Z')||(ch >= 'a' && ch <= 'z'))
{
ch_num++;
}
if ((ch >= '0' && ch <= '9'))
{
dig_num++;
}
} while (ch != '#'); /* 给出循环条件 */
putchar('\n');
printf("The number of chars is:%d\n",ch_num);
printf("The number of digital is:%d\n",dig_num);
return 0;
}
活动中有三门礼炮,各装有十枚炮弹。第一门礼炮每隔3秒发一枚炮弹,第二门每隔5秒、第三门每隔7秒发一枚炮。问观礼群众一共听到几声炮响。三门炮的第一发炮弹是同时发出的,同一时间发出炮声算一响。
程序代码:
#include<stdio.h>
int main(void)
{
int t = 0;
int sound = 0;
int shell3s = 10;
int shell5s = 10;
int shell7s = 10;
for (t=0; t<=63; t++)
{
if (((t%3==0)&&(shell3s>0))||((t%5==0)&&(shell5s>0))||((t%5==0)&&(shell5s>0)))
{
sound++;
if ((t%3 == 0)&&(shell3s>0))
{
printf("t=%d,Shell3s fired!\n",t);
shell3s--;
}
if ((t%5 == 0)&&(shell5s>0))
{
printf("t=%d,Shell5s fired!\n",t);
shell5s--;
}
if ((t%7 == 0)&&(shell5s>0))
{
printf("t=%d,Shell7s fired!\n",t);
shell7s--;
}
}
}
printf("There are %d hits.",sound);
return 0;
}
输出[m,n]范围内的所有斐波那契(Fibonacci)数,m,n这两个数由键盘输入。
程序代码:
#include<stdio.h>
/* 1,1,2,3,5,8,13,21,34 */
int main(void)
{
int m = 0;
int n = 0;
int i = 0;
long a = 1l; /* l must be used */
long b = 1l;
long c = 0l;
printf("Pls Input M:");
scanf("%d",&m);
printf("Pls Input N:");
scanf("%d",&n);
printf("Fibonacci in [%d,%d]:",m,n);
if (m==1)
{
if (n==1)
{
printf("1,");
}
else
{
printf("1,1,");
}
}
if (m==2)
{
printf("1,");
}
for (i=3; i<=n; i++)
{
c=a+b;
a=b;
b=c;
if (i>=m)
{
printf("%ld,",c);
}
}
return 0;
}
[ 本帖最后由 zklhp 于 2011-4-15 21:08 编辑 ]






