当前位置:网站首页>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;
}
边栏推荐
- Taro2.* applet configuration sharing wechat circle of friends
- 2022 Google CTF SEGFAULT LABYRINTH wp
- Niuke cold training camp 6B (Freund has no green name level)
- from . cv2 import * ImportError: libGL. so. 1: cannot open shared object file: No such file or direc
- [hfctf2020]babyupload session parsing engine
- ESP Arduino (IV) PWM waveform control output
- 负载均衡性能参数如何测评?
- 云呐|工单管理软件,工单管理软件APP
- pyflink的安装和测试
- 字节P7专业级讲解:接口测试常用工具及测试方法,福利文
猜你喜欢
力扣1037. 有效的回旋镖
ClickHouse字段分组聚合、按照任意时间段粒度查询SQL
ESP Arduino (IV) PWM waveform control output
动态规划思想《从入门到放弃》
Build your own website (17)
让我们,从头到尾,通透网络I/O模型
JTAG debugging experience of arm bare board debugging
Force buckle 1037 Effective boomerang
系统休眠文件可以删除吗 系统休眠文件怎么删除
Body mass index program, entry to write dead applet project
随机推荐
golang中的Mutex原理解析
Deep learning framework TF installation
Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform
C# 计算农历日期方法 2022
Supersocket 1.6 creates a simple socket server with message length in the header
Installation of gazebo & connection with ROS
Analysis of mutex principle in golang
Gazebo的安装&与ROS的连接
[batch dos-cmd command - summary and summary] - string search, search, and filter commands (find, findstr), and the difference and discrimination between find and findstr
【js】获取当前时间的前后n天或前后n个月(时分秒年月日都可)
Vocabulary in Data Book
JTAG principle of arm bare board debugging
Windows installation mysql8 (5 minutes)
Can the system hibernation file be deleted? How to delete the system hibernation file
Dark horse notes - create immutable sets and streams
Taro2.* 小程序配置分享微信朋友圈
Failed to successfully launch or connect to a child MSBuild. exe process. Verify that the MSBuild. exe
NEON优化:关于交叉存取与反向交叉存取
Cause of handler memory leak
Send template message via wechat official account