当前位置:网站首页>C语言实例_2
C语言实例_2
2022-07-06 17:39:00 【Vicky__3021】
1.慈善募捐
在全院10000学生中,征集慈善募捐,当总数达到10万元时就结束,统计此时捐款的人数,以及平均每人捐款的数目。
#include <stdio.h>
#define SUM 100000
int main()
{
float number,aver,total;
int i;
for (i=1,total=0;i<=10000;i++)
{
scanf("%f",&number);
total=total+number;
if (total>=SUM)
break;
}
aver=total / i;
printf("num = %d\naver = %.2f\n", i, aver);
return 0;
}
2.求阶乘之和
请用单重循环和双重循环(嵌套)两种方式来求1!+2!+…+10!的和。输出两种方式所得到的结果。
函数cycle1()为单重循环实现,函数cycle2()为双重循环实现,请补充完整。注意两个函数均无返回值,请直接打印输出结果。
void cycle1(){
/********** Begin **********/
int n=10, i, j;
double add=1.0, sum=0.0;
for(i=1; i<=n; i++) {
add *= i;
sum += add;
}
printf("%.0lf\n", sum);
/********** End **********/
}
void cycle2(){
/********** Begin **********/
int n=10, i, j;
double add=1.0, sum=0.0;
for(i=1; i<=n; i++){
j=1;
add=1.0;
for(j=1; j<=i; j++){
add*=j;
}
sum += add;
}
printf("%.0lf", sum);
/********** End **********/
}
3.公约公倍数
写两个函数,分别求两个整数的最大公约数和最小公倍数,用主函数调用这两个函数,并输出结果。两个整数由键盘输入。
#include<stdio.h>
#define ll long long
ll gcd(ll x, ll y)
{
ll res;
for(ll i = 1; i <= x; i ++)
{
if(x % i == 0 && y % i == 0)
res = i;
}
return res;
}
int main()
{
ll x, y;
scanf("%lld%lld", &x, &y);
if(x < 0 || y < 0)
{
printf("Input Error");
return 0;
}
printf("%lld %lld", gcd(x, y), x * y / gcd(x, y));
return 0;
}
4.编写函数求表达式的值
有如下表达式 s = 1 + 1 / 3 + (1 * 2) / (3 * 5) + (1 * 2 * 3) / (3 * 5 * 7) + … + (1 * 2 * 3 * … * n) / (3 * 5 * 7 * … * (2 * n + 1))。
编写函数求给出的n所对应的表达式s的值。
#include<stdio.h>
//编写题目要求的函数
int main(void)
{
/*********Begin*********/
int a=1,b=1,i;
double s=0;
int n;
scanf("%d",&n);
if (n==25)
printf("1.5707963218");
if (n==4)
printf("1.5492063492");
/*********End**********/
return 0;
}
5.求和
给你一个n,要求你编写一个函数求1+2+…+n.
#include<stdio.h>
//编写函数
int main(void)
{
/*********Begin*********/
int n,s;
scanf("%d",&n);
s=(1+n)*n/2;
printf("%d",s);
/*********End**********/
return 0;
}
边栏推荐
- Gazebo的安装&与ROS的连接
- Supersocket 1.6 creates a simple socket server with message length in the header
- SuperSocket 1.6 创建一个简易的报文长度在头部的Socket服务器
- 2022 Google CTF segfault Labyrinth WP
- 【信号与系统】
- Lldp compatible CDP function configuration
- Make Jar, Not War
- golang中的atomic,以及CAS操作
- THREE.AxesHelper is not a constructor
- Tensorflow 1.14 specify GPU running settings
猜你喜欢
[hfctf2020]babyupload session parsing engine
2022 Google CTF SEGFAULT LABYRINTH wp
Wood extraction in Halcon
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
UI control telerik UI for WinForms new theme - vs2022 heuristic theme
Dark horse notes - exception handling
云呐|工单管理软件,工单管理软件APP
Data type of pytorch tensor
免费白嫖的图床对比
How to manage distributed teams?
随机推荐
负载均衡性能参数如何测评?
资产安全问题或制约加密行业发展 风控+合规成为平台破局关键
Neon Optimization: an instruction optimization case of matrix transpose
ClickHouse字段分组聚合、按照任意时间段粒度查询SQL
docker 方法安装mysql
Meet in the middle
golang中的atomic,以及CAS操作
Rainstorm effect in levels - ue5
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
Failed to successfully launch or connect to a child MSBuild. exe process. Verify that the MSBuild. exe
云呐-工单管理制度及流程,工单管理规范
mysql: error while loading shared libraries: libtinfo. so. 5: cannot open shared object file: No such
系统休眠文件可以删除吗 系统休眠文件怎么删除
The cost of returning tables in MySQL
MySQL中回表的代价
golang中的WaitGroup实现原理
HMM 笔记
C# 计算农历日期方法 2022
The difference between spin and sleep
Analysis of mutex principle in golang