当前位置:网站首页>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.* 小程序配置分享微信朋友圈
- C language - array
- Neon Optimization: an optimization case of log10 function
- Data type of pytorch tensor
- Case development of landlord fighting game
- Failed to successfully launch or connect to a child MSBuild. exe process. Verify that the MSBuild. exe
- Transformation transformation operator
- Atomic in golang, and cas Operations
- golang中的WaitGroup实现原理
- pyflink的安装和测试
猜你喜欢

c语言—数组
![[Niuke] b-complete square](/img/bd/0812b4fb1c4f6217ad5a0f3f3b8d5e.png)
[Niuke] b-complete square

Come on, don't spread it out. Fashion cloud secretly takes you to collect "cloud" wool, and then secretly builds a personal website to be the king of scrolls, hehe

Do you understand this patch of the interface control devaxpress WinForms skin editor?

第三方跳转网站 出现 405 Method Not Allowed

黑马笔记---异常处理

Dark horse notes - exception handling

免费白嫖的图床对比

Installation of gazebo & connection with ROS
![[100 cases of JVM tuning practice] 04 - Method area tuning practice (Part 1)](/img/7a/bd03943c39d3f731afb51fe2e0f898.png)
[100 cases of JVM tuning practice] 04 - Method area tuning practice (Part 1)
随机推荐
405 method not allowed appears when the third party jumps to the website
Install Firefox browser on raspberry pie /arm device
pyflink的安装和测试
【案例分享】网络环路检测基本功能配置
自旋与sleep的区别
2022 Google CTF SEGFAULT LABYRINTH wp
移植DAC芯片MCP4725驱动到NUC980
The difference between spin and sleep
Openjudge noi 1.7 10: simple password
让我们,从头到尾,通透网络I/O模型
Wood extraction in Halcon
Do you understand this patch of the interface control devaxpress WinForms skin editor?
2022 Google CTF SEGFAULT LABYRINTH wp
ClickHouse字段分组聚合、按照任意时间段粒度查询SQL
「笔记」折半搜索(Meet in the Middle)
Metauniverse urban legend 02: metaphor of the number one player
[HFCTF2020]BabyUpload session解析引擎
There is an error in the paddehub application
Taro2.* applet configuration sharing wechat circle of friends
gnet: 一个轻量级且高性能的 Go 网络框架 使用笔记