c语言百钱买百鸡问题
第8章综合实训

break;
} }while(Times>0); if(0==Times) { printf("对不起,尝试失败,请重新启动程序.\n"); return; }
8.2.7 万年历程序编写
2.year闰年判断模块
判断year为闰年还是平年,为后续程序设计作判断。仿照算法 设计中的公式,写出闰年判断函数leapFunc(),代码如下:
5 x 3 y z / 2 100 x y z 100
其中,x,y和z都是正整数,对于这样一个不定方程,只能使用试 凑法解决,过程繁琐且容易产生错误。
8.1.2印度国王的奖励
相传古代印度国王要褒奖他的聪明能干的宰相 达依尔(国际象棋发明者),问他要什么?达依尔回 答:“陛下只要在国际象棋棋盘的第一个格子上放 一粒麦子,第二格子放二粒麦子,以后每个格子的 麦子数都按前一格的两倍计算。如果陛下按此法给 我64格的麦子,就感激不尽了。”国王想,“这还 不容易!”让人杠了一袋麦子,但很快用光了,再 扛一袋还不够,请你为国王算一下共要给达依尔多 少小麦?(设1立方米小麦约1.4×108颗)
01 02 03 04 05 06 07 08 09 10 11 int leapFunc(int year) { if(year%4==0&&year%100!=0||year%400==0) { return 1; } else { return 0; } }
函数判断结束后将返回值,若返回1,表示year为闰年,否则为平年。
7.函数入口设计
8.2.10 万年历程序编写
子模块设计完成后,添加主函数并进行代码调试,代码如下:
void main() { int month=0,year=0; int Times=3; printf("请输入要查询的年和月,格式为:xxxx-xx\n"); do { scanf("%d-%d", &year,&month); Times--; if((0!=Times) &&(year<0 || (month<0 || month>12))) { printf("对不起,输入错误,请重新输入,您还有 %d 次尝试机会.\n",Times-1); } else { break; } }while(Times>0); if(0==Times) { printf("对不起,尝试失败,请重新启动程序.\n"); return; } print(month,year); printf("\n");
百元买鸡问题 穷举法 c++

穷举法“穷举法”也称“枚举法”,即将可能出现的各种情况一一测试,判断是否满足条件,采用循环语句可方便实现。
百元买鸡问题。
假定小鸡每只5角,公鸡每只2元,母鸡每只5元。
现在有100块钱,要求买100只鸡,编程列出所有可能的购鸡方案。
分析:设母鸡、公鸡、小鸡分别为x、y、z只,根据题目要求列出方程为:其中包括3个未知数,2个方程,此题有若干个解。
可用如下两种方法实现。
方法一:用三重循环来解决。
#include”iostream.h”#include”iomanip.h”void main(){Int x,y,z;Long k(0);Cout <<”x y z”<<endl;For(x=0;x<100;x++)For(y=0;y<100;y++)For(z=0;z<100;z++){K++;If(((3*x+2*y+0.5z)==100)&&((x+y+z==100)))Cout<<setw(9)<<x<<setw(9)<<y<<setw(9)<<z<<endl;}Cout<<”方法一内执行了”<<k<<”次”<<endl;}方法二:用两重循环来解决,相应程序段如下:Cout <<”x y z”<<endl;For(x=0;x<=33;x++)For(y=0;y<=50;y++){K++;z=100-x-y;if((3*x+2*y+0.5z)==100)cout<<setw(9)<<x<<setw(9)<<y<<setw(9)<<z<<endl;}Cout<<”方法二内循环执行了”<<k<<”次”<<endl;方法一利用三重循环表示三种鸡的只数,考虑了所有购鸡的情况,故共执行了1000000次循环;方法二进行了循环的优化,根据三种鸡共100只的关系,用二重循环实现;同时没种鸡的循环次数不必到100,因为还要满足价格100元得问题,故共执行了1734次循环。
c语言程序设计中常用计算方法

c语⾔程序设计中常⽤计算⽅法上完课过来整理⼀下笔记5551、穷举法1//百钱买百鸡问题(简化后)23 #include <stdio.h>4 #include <stdlib.h>56int main()7 {8int i,j,k;9for(i=0; i<20; i++)10 {11for(j=0; j<34; j++)12 {13 k=100-i-j;14if(i*15+j*9+k==300)15 {16 printf("%d %d %d\n",i,j,k);17 }18 }19 }20return0;21 }2、迭代法1//求解⼀元三次⽅程指定范围的根2 #include <stdio.h>3 #include <stdlib.h>4 #include <math.h>56double biroot(double,double);7double foo(double);89int main()10 {11double x1,x2;12do13 {14 scanf("%lf%lf",&x1,&x2);15 }while(foo(x1)*foo(x2)>0);1617 printf("%.2f\n",biroot(x1,x2));18return0;19 }20double biroot(double x1,double x2)21 {22double ret;23do24 {25 ret=(x1+x2)/2;26if(foo(ret)*foo(x1)>0)27 {28 x1=ret;29 }30else31 {32 x2=ret;33 }34 }while(fabs(x1-x2)>=1e-7);35 ret=(x1+x2)/2;36return ret;37 }38double foo(double x)39 {40return(x*x*x-7.7*x*x+19.2*x-15.3);41 }3、⽜顿迭代(⽜顿切线法)//求解⼀元三次⽅程在1.0附近的根#include <stdio.h>#include <stdlib.h>#include <math.h>double ntroot(double);double foo(double);double dfoo(double);int main(){double x0;scanf("%lf",&x0);printf("%.2f\n",ntroot(x0));return0;}double ntroot(double x){double x0;double f,df;do{x0=x;f=foo(x0);df=dfoo(x0);x=x0-f/df;}while(fabs(x-x0)>=1e-7);return x;}double foo(double x){return (x*x*x-7.7*x*x+19.2*x-15.3); }double dfoo(double x){return(3.0*x*x-15.4*x+19.2);}4、递推法(归纳法)//累加和累乘#include <stdio.h>#include <stdlib.h>#include <math.h>double getSum(int);int main(){int n;scanf("%d",&n);printf("%f\n",getSum(n));return0;}double getSum(int n){int i;double s=0.0;for(i=1;i<=n;i++){s+=pow(-1,i+1)/i;}return s;}//筛选法求素数#include <stdio.h>#include <stdlib.h>#define MAX_SIZE 1000void getPrimeTab(int *,int);void initSieve(int*,int);int getNext(int*,int,int);int outPrimeTab(int *,int);int main(){int n;int cnt;int sieve[MAX_SIZE]={0};initSieve(sieve,MAX_SIZE);scanf("%d",&n);getPrimeTab(sieve,n);cnt=outPrimeTab(sieve,n);printf("total primes = %d\n",cnt); return0;}void initSieve(int*ps,int n){int i;for(i=2;i<n;i++){ps[i]=1;}}int outPrimeTab(int *ps,int n){int i,count=0;for(i=2;i<n;i++){if(ps[i]==1){if(count++%6==0){printf("\n");}printf("%8d",i);}}printf("\n");return count;}int getNext(int*ps,int p,int n){int q=p+1;while(q<n && ps[q]==0){q++;}return q;}void getPrimeTab(int *ps,int n){int p,q,i;for(p=2;p*p<n;p=getNext(ps,p,n)){for(q=p;p*q<n;q=getNext(ps,q,n)){for(i=p*q;i<n;i*=p){ps[i]=0;}}}}//梯形法求定积分#include <stdio.h>#include <stdlib.h>#include <math.h>double integrate(double(*pf)(double),double a,double b,int n); double my_fun(double x);int main(){double a,b;int n;printf(" 积分上限: a= ");scanf("%lf",&a);printf(" 积分下限:b= ");scanf("%lf",&b);printf(" 分割段数: n= ");scanf("%d",&n);printf("sin 函数积分值: %f\n",integrate(sin,a,b,n));printf("x^2 函数积分值: %f\n",integrate(my_fun,a,b,n)); return0;}double integrate(double(*pf)(double),double a,double b,int n) {int i;double h=(b-a)/n;double s=(pf(a)+pf(b))/2;for(i=1;i<=n-1;i++){s+=pf(a+i*h);}s=h*s;return s;}double my_fun(double x){return x*x; }。
百钱买百鸡c语言编程题

"百钱买百鸡"是一个经典的数学问题,故事是这样的:公鸡5钱一只,母鸡3钱一只,小鸡1钱三只,现在要用100钱买100只鸡,问公鸡、母鸡、小鸡各多少只。
下面是一个使用C语言解决这个问题的示例代码:
c
#include <stdio.h>
int main() {
int x, y, z; // x代表公鸡数量,y代表母鸡数量,z代表小鸡数量
for (x = 0; x <= 20; x++) { // 公鸡最多买20只
for (y = 0; y <= 33; y++) { // 母鸡最多买33只
z = 100 - x - y; // 剩余的数量就是小鸡的数量
if (z % 3 == 0 && x * 5 + y * 3 + z / 3 == 100) {
printf("公鸡:%d只,母鸡:%d只,小鸡:%d只\n", x, y, z);
}
}
}
return 0;
}
这段代码使用两个嵌套的for循环来遍历所有可能的公鸡和母鸡的数量组合。
对于每一对组合,它计算出小鸡的数量,然后检查是否满足题目的条件。
如果满足条件,它就打印出这一组解。
“百钱买百鸡”问题的C语言算法分析

“百钱买百鸡”问题的C语言算法分析Abstract:As a process-oriented programming language,C programming language is one of the most classic and popular computer programming languages with the characteristics of the assembly language and the high-level language.It is not only the first choice for the people who begin to learn computer programming,but also the basis for other computer courses and software development.As a difficult point in C Programming Language learning,the loop statement can be used to solve many practical problems of regularly repetitive operation.Taking the case of "spending 100 dollars on 100 chickens",the paper implements design,analysis and optimization,and finally proposes the optimal algorithm.Keywords:C programming language;loop statement;spending 100 dollars on 100 chickens1 引言(Introduction)计算机算法设计是计算机专业学习的核心专业内容,算法设计对于培养一个人的逻辑思维能力具有重要的作用,能进行有效的算法设计是对一个计算机学者的基本要求。
c++、python、vb求解百钱百鸡问题

我国古代数学家张丘建在《算经》一书中曾提出过著名的“百钱买百鸡”问题,该问题叙述如下:鸡翁一,值钱三;鸡母一,值钱二;鸡雏三,值钱一;百钱买百鸡,则翁、母、雏各几何?翻译过来,意思是公鸡一个三块钱,母鸡一个二块钱,小鸡三个一块钱,现在要用一百块钱买一百只鸡,问公鸡、母鸡、小鸡各多少只?题目分析如果用数学的方法解决百钱买百鸡问题,可将该问题抽象成方程式组。
设公鸡x 只,母鸡y 只,小鸡z 只,得到以下方程式组:A:3x+2y+1/3z = 100B:x+y+z = 100C:0 <= x <= 100D:0 <= y <= 100E:0 <= z <= 100如果用解方程的方式解这道题需要进行多次猜解,因此我们用穷举法的方式来解题。
1.C++语言#include<iostream>using namespace std;int main(){int i,j,k,x,y,z;for (i=0;i<=33;i++)for(j=0;j<=50;j++)for(k=0;k<=100;k++)if((3*i+2*j+k/3==100)&&(i+j+k==100)&&k%3==0)cout<<i<<" "<<j<<" "<<k<<endl;return 0;}2.Python语言for i in range(33):for j in range(50):for k in range(100):if (3*i+2*j+k/3==100) and (i+j+k==100) and (k%3==0):print(i,j,k)3.VB语言Dim a As Integer, b As Integer, c As IntegerFor a = 0 To 33For b = 0 To 50For c = 0 To 100If 3 * a + 2 * b + 1 / 3 * c = 100 And a + b + c = 100 ThenPrint "公鸡" & a, "母鸡" & b, "小鸡" & cEnd IfNext cNext bNext a。
“百钱买百鸡”问题的C语言算法分析

“百钱买百鸡”问题的C语言算法分析作者:龙敏敏来源:《软件工程》2017年第03期摘要:C语言是使用时间最久和最普及的计算机高级程序设计语言之一,属于面向过程的程序设计语言,兼有汇编语言和高级语言的双重特点,是人们学习计算机程序设计的首选语言,也是学习其他计算机课程和进行软件开发的基础。
C语言程序设计中的循环语句是C语言的一个难点,可以用来解决许多具有规律性重复操作的实际问题,文章通过对“百钱买百鸡”这个问题的算法进行设计、分析和优化,以寻求解决问题的最优算法。
关键词:C语言;循环语句;百钱买百鸡中图分类号:TP311.1 文献标识码:AAbstract:As a process-oriented programming language,C programming language is one of the most classic and popular computer programming languages with the characteristics of the assembly language and the high-level language.It is not only the first choice for the people who begin to learn computer programming,but also the basis for other computer courses and software development.As a difficult point in C Programming Language learning,the loop statement can be used to solve many practical problems of regularly repetitive operation.Taking the case of "spending 100 dollars on 100 chickens",the paper implements design,analysis and optimization,and finally proposes the optimal algorithm.Keywords:C programming language;loop statement;spending 100 dollars on 100 chickens1 引言(Introduction)计算机算法设计是计算机专业学习的核心专业内容,算法设计对于培养一个人的逻辑思维能力具有重要的作用,能进行有效的算法设计是对一个计算机学者的基本要求。
从“百钱百鸡”问题看如何进行C语言程序设计

三 值钱 一 。百钱买百鸡, 问鸡翁、 雏 各几何 ? 母、
2 穷 举 法
oit(\cc = ,e= , ik %d, c , ncik . r f ” ok %dhn %dc c = " okh ,h ) ) n n h c e c
必须注 意:①上 述 i条 件 中 c i /. 不是 ci /, f hc 30而 k hc 3 k
性 的工作 。在循环程序设计 中就可 以充分发挥计算机 的这
一
为 了提高效率 , 少循环 次数 , 减 利用 百鸡条件 , 上述算
法 可优化 , 变三重循环为二重循环 , 改进 后的 C语 言程序如
下:
# n ld < ti.> icu e sdoh
优势 , 把复杂 问题 的求解过程转换为操作 的多 次重复 。 通过分析可 知, 它们 可能的取值范 围是:ok为 0 1 , c c ~ 9
因 为 C语言 中 ci / 整除, c c = 、 hc 3是 k 当 h k 34或 5时 ,hc/ i ci 3 k
设 分别用变量 c k h n ci c o 、e 、hc k来代表 鸡翁、 鸡母、 鸡雏
的 只数 , 上述 问 题 从 数 学 上 看 是 求 包 含 三个 未 知 数 的 两 个
Li ixa g u Jn in Xi i a Tawu
Ab t a t T e p p r d s u s s te a ta p l ain o n me ai e meh d a k n f me h d o o p p o r mmi g i sr c : h a e ic se h cu l a p i t f E u r t to - i d o t o f lo rg a c o v n n C
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
百钱买百鸡问题——一百个铜钱买了一百只鸡,其中公鸡一只5钱、母鸡一只3钱,小鸡一钱3只,问一百只鸡中公鸡、母鸡、小鸡各多少)。
这是一个古典数学问题,设一百只鸡中公鸡、母鸡、小鸡分别为x ,y ,z ,问题化为三元一次方程组:
⎩
⎨
⎧=++=++)(100)
(1003/35百鸡百钱z y x z y x 这里x,y,z 为正整数,且z 是3的倍数;由于鸡和钱的总数都是100,可以确定x,y,z 的取值范围:
1) x 的取值范围为1~20 2) y 的取值范围为1~33
3) z 的取值范围为3~99,步长为3 对于这个问题我们可以用穷举的方法,遍历x,y,z 的所有可能组合,最后得到问题的解。
数据要求
问题中的常量: 无
问题的输入: 无
问题的输出:
int x ,y ,z /*公鸡、母鸡、小鸡的只数*/ 初始算法
1.初始化为1;
2.计算x 循环,找到公鸡的只数; 3.计算y 循环,找到母鸡的只数; 4.计算z 循环,找到小鸡的只数; 5.结束,程序输出结果后退出。
算法细化
算法的步骤1实际上是分散在程序之中的,由于用的是for 循环,很方便的初始条件放到了表达式之中了。
步骤2和3是按照步长1去寻找公鸡和母鸡的个数。
步骤4的细化 4.1 z =1
4.2 是否满足百钱,百鸡
4.2.1 满足,输出最终百钱买到的百鸡的结果 4.2.2 不满足,不做处理 4.3 变量增加,这里注意步长为3 流程图
图5-8 程序执行流程图
程序代码如下
#include "stdio.h"
main()
{
int x,y,z;
for(x=1;x<=20;x++)
for(y=1;y<=33;y++)
for(z=3;z<=99;z+=3)
{
if((5*x+3*y+z/3==100)&&(x+y+z==100))/*是否满足百钱和百鸡的条件*/
printf("cock=%d,hen=%d,chicken=%d\n",x,y,z);
}
}
分析
程序运行结果如下:
cock=4,hen=8,chicken=78
cock=8,hen=11,chicken=81
cock=12,hen=4,chicken=84
对于这个问题实际上可以不用三重循环,而是用二重循环,因为公鸡和母鸡数确定后,小鸡数就定了,即y
100
z 。
请同学们自己分析二重循环和三重循环的运行次数,做为
x
-
-
练习自己调试这一方法。