当前位置:网站首页>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;
}
边栏推荐
- C# 计算农历日期方法 2022
- boot - prometheus-push gateway 使用
- 第三方跳转网站 出现 405 Method Not Allowed
- HMM notes
- Meet in the middle
- Docker method to install MySQL
- Vocabulary in Data Book
- Spark TPCDS Data Gen
- [100 cases of JVM tuning practice] 04 - Method area tuning practice (Part 1)
- [100 cases of JVM tuning practice] 05 - Method area tuning practice (Part 2)
猜你喜欢
云呐-工单管理制度及流程,工单管理规范
让我们,从头到尾,通透网络I/O模型
界面控件DevExpress WinForms皮肤编辑器的这个补丁,你了解了吗?
Force buckle 1037 Effective boomerang
云呐|工单管理软件,工单管理软件APP
How to manage distributed teams?
Let's see through the network i/o model from beginning to end
Dynamic planning idea "from getting started to giving up"
身体质量指数程序,入门写死的小程序项目
Js逆向——捅了【马蜂窝】的ob混淆与加速乐
随机推荐
The MySQL database in Alibaba cloud was attacked, and finally the data was found
In rails, when the resource creation operation fails and render: new is called, why must the URL be changed to the index URL of the resource?
from . cv2 import * ImportError: libGL. so. 1: cannot open shared object file: No such file or direc
c语言—数组
Informatics Olympiad YBT 1171: factors of large integers | 1.6 13: factors of large integers
让我们,从头到尾,通透网络I/O模型
Docker method to install MySQL
Table table setting fillet
docker 方法安装mysql
Informatics Orsay Ibn YBT 1172: find the factorial of n within 10000 | 1.6 14: find the factorial of n within 10000
MySQL中回表的代价
Rainstorm effect in levels - ue5
taro3.*中使用 dva 入门级别的哦
Realize incremental data synchronization between MySQL and ES
前置机是什么意思?主要作用是什么?与堡垒机有什么区别?
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
Neon Optimization: an instruction optimization case of matrix transpose
Neon Optimization: performance optimization FAQ QA
[Niuke] [noip2015] jumping stone
Lldp compatible CDP function configuration