当前位置:网站首页>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;
}
边栏推荐
- Do you understand this patch of the interface control devaxpress WinForms skin editor?
- Go zero micro service practical series (IX. ultimate optimization of seckill performance)
- 安全保护能力是什么意思?等保不同级别保护能力分别是怎样?
- Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform
- Receive user input, height BMI, BMI detection small business entry case
- Supersocket 1.6 creates a simple socket server with message length in the header
- Metauniverse urban legend 02: metaphor of the number one player
- 分享一个通用的so动态库的编译方法
- Data type of pytorch tensor
- Installation of torch and torch vision in pytorch
猜你喜欢

移植DAC芯片MCP4725驱动到NUC980

2022 Google CTF segfault Labyrinth WP

pytorch之数据类型tensor
![[batch dos-cmd command - summary and summary] - jump, cycle, condition commands (goto, errorlevel, if, for [read, segment, extract string]), CMD command error summary, CMD error](/img/a5/41d4cbc070d421093323dc189a05cf.png)
[batch dos-cmd command - summary and summary] - jump, cycle, condition commands (goto, errorlevel, if, for [read, segment, extract string]), CMD command error summary, CMD error

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

JTAG principle of arm bare board debugging

【案例分享】网络环路检测基本功能配置

Wood extraction in Halcon

Transformation transformation operator

Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
随机推荐
Can the system hibernation file be deleted? How to delete the system hibernation file
Taro 小程序开启wxml代码压缩
Receive user input, height BMI, BMI detection small business entry case
Js逆向——捅了【马蜂窝】的ob混淆与加速乐
Neon Optimization: an optimization case of log10 function
Gazebo的安装&与ROS的连接
NEON优化:log10函数的优化案例
Segmenttree
Vocabulary in Data Book
第三方跳转网站 出现 405 Method Not Allowed
Dark horse notes - exception handling
Atomic in golang and CAS operations
Transformation transformation operator
golang中的WaitGroup实现原理
[batch dos-cmd command - summary and summary] - string search, search, and filter commands (find, findstr), and the difference and discrimination between find and findstr
[HFCTF2020]BabyUpload session解析引擎
Tensorflow GPU installation
Byte P7 professional level explanation: common tools and test methods for interface testing, Freeman
Spark TPCDS Data Gen
405 method not allowed appears when the third party jumps to the website