当前位置:网站首页>C language instance_ two
C language instance_ two
2022-07-07 01:24:00 【Vicky__ three thousand and twenty-one】
1. Charity fundraising
In the whole hospital 10000 Among the students , Solicit charitable donations , When the total reaches 10 Ten thousand yuan will end , Count the number of donations at this time , And the average number of donations per person .
#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. Find the sum of factorials
Please use single cycle and double cycle ( nesting ) There are two ways to find 1!+2!+…+10! And . Output the results obtained in two ways .
function cycle1() For single loop implementation , function cycle2() Realize for double loop , Please complete . Note that both functions have no return value , Please print out the result directly .
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. Convention common multiple
Write two functions , Find the maximum common divisor and the minimum common multiple of two integers respectively , Call these two functions with the main function , And output the result . Two integers are entered by the keyboard .
#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. Write a function to find the value of the expression
It has the following expression s = 1 + 1 / 3 + (1 * 2) / (3 * 5) + (1 * 2 * 3) / (3 * 5 * 7) + … + (1 * 2 * 3 * … * n) / (3 * 5 * 7 * … * (2 * n + 1)).
Write a function to find the given n The corresponding expression s Value .
#include<stdio.h>
// Write the function required by the topic
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. Sum up
To give you one n, Ask you to write a function to find 1+2+…+n.
#include<stdio.h>
// Write function
int main(void)
{
/*********Begin*********/
int n,s;
scanf("%d",&n);
s=(1+n)*n/2;
printf("%d",s);
/*********End**********/
return 0;
}
边栏推荐
- Oracle:CDB限制PDB资源实战
- Oracle: Practice of CDB restricting PDB resources
- C语言实例_2
- Vocabulary in Data Book
- NEON优化:性能优化经验总结
- [JS] obtain the N days before and after the current time or the n months before and after the current time (hour, minute, second, year, month, day)
- Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
- NEON优化:log10函数的优化案例
- Tensorflow GPU installation
- Installation of torch and torch vision in pytorch
猜你喜欢
黑马笔记---创建不可变集合与Stream流
Make Jar, Not War
字节P7专业级讲解:接口测试常用工具及测试方法,福利文
Transformation transformation operator
1123. The nearest common ancestor of the deepest leaf node
资产安全问题或制约加密行业发展 风控+合规成为平台破局关键
[case sharing] basic function configuration of network loop detection
免费白嫖的图床对比
系统休眠文件可以删除吗 系统休眠文件怎么删除
"Exquisite store manager" youth entrepreneurship incubation camp - the first phase of Shunde market has been successfully completed!
随机推荐
[signal and system]
SuperSocket 1.6 创建一个简易的报文长度在头部的Socket服务器
安全保护能力是什么意思?等保不同级别保护能力分别是怎样?
C语言实例_5
ESP Arduino (IV) PWM waveform control output
golang中的atomic,以及CAS操作
接收用户输入,身高BMI体重指数检测小业务入门案例
table表格设置圆角
golang中的Mutex原理解析
Receive user input, height BMI, BMI detection small business entry case
斗地主游戏的案例开发
"Exquisite store manager" youth entrepreneurship incubation camp - the first phase of Shunde market has been successfully completed!
力扣1037. 有效的回旋镖
自旋与sleep的区别
【芯片方案设计】脉搏血氧仪
树莓派/arm设备上安装火狐Firefox浏览器
ClickHouse字段分组聚合、按照任意时间段粒度查询SQL
Oracle: Practice of CDB restricting PDB resources
boot - prometheus-push gateway 使用
golang中的WaitGroup实现原理