简单计算题一

上传人:汽*** 文档编号:578411389 上传时间:2024-08-24 格式:PPT 页数:42 大小:448.02KB
返回 下载 相关 举报
简单计算题一_第1页
第1页 / 共42页
简单计算题一_第2页
第2页 / 共42页
简单计算题一_第3页
第3页 / 共42页
简单计算题一_第4页
第4页 / 共42页
简单计算题一_第5页
第5页 / 共42页
点击查看更多>>
资源描述

《简单计算题一》由会员分享,可在线阅读,更多相关《简单计算题一(42页珍藏版)》请在金锄头文库上搜索。

1、第二讲n 简单计算题(一)简单计算题(一)ACM算法与程序设计2/38简单计算题简单计算题主要目的:通过编写一些简单计算题的程序主要目的:通过编写一些简单计算题的程序焘悉焘悉CC+语言的基本语法语言的基本语法基本思想:解决简单的计算问题的基本过程包基本思想:解决简单的计算问题的基本过程包括将一个用自然语言描述的实际问题抽象成一括将一个用自然语言描述的实际问题抽象成一个计算问题给出计算过程继而编程实现计个计算问题给出计算过程继而编程实现计算过程,并将计算结果还原成对原来问题的解算过程,并将计算结果还原成对原来问题的解答。这里首要的是读懂问题接清输入和输出答。这里首要的是读懂问题接清输入和输出数据

2、的含义及给出的格式数据的含义及给出的格式并且通过输入输出并且通过输入输出样例验证自己的理解是否正确。样例验证自己的理解是否正确。3/38最小公倍数 http:/ Description求两个正整数的最小公倍数。Input输入数据含有不多于50对的数据,每对数据由两个正整数(0n1,n2y)x=x-y;elsey=y-x;returnx;9/38#includeintgcd(intint);Intmain()intx,y;while(cinxy)coutx/gcd(x,y)*yend1;return0;int gcd(int a,int b)return !b?a:gcd(b,a%b);10/3

3、8POJ 2750 鸡兔同笼鸡兔同笼http:/ input: 2 3 20Sample output: 0 0 5 1013/38#includevoidmain()intnCases,I,nFeet;/InCases表示输入测试数据的表示输入测试数据的组组数,数,nFeet表示输入的脚数表示输入的脚数scanf(“%d”,&nCases);for(i=0;jinCases;i+)scanf(“%d”,&nFeet);if(nFeet%2!=0)/如果有奇数只脚,则没有满足题如果有奇数只脚,则没有满足题意的答案,因为不论意的答案,因为不论2只还是只还是4只,都是偶数只,都是偶数printf(

4、“00n”);elseif(nFeet%4!=0)/若要动物数目最少,使动物若要动物数目最少,使动物尽量有尽量有4只脚,若要动物数目最多,使动物尽量有只脚,若要动物数目最多,使动物尽量有2只只脚脚printf(“%d%dn”,nFeet/4+1,nFeet/2);elseprintf(“%d%dn”,nFeet/4,nFeet/2);14/38TheStoryofZCS1、Linkhttp:/222.197.181.37/ShowProblem.aspx?ProblemID=10882、DescriptionLoveandEternallayhidinnight;GodsaidLetCSbea

5、ndallwaslight.Walkingaloneindarkness,lonelyandneverknewwhatIwouldgothrough.HowmanytimesIwalkedlonelyinthecampus,howmanytimesIwokeupwithastartatmidnight,andhowmanytimesIsangasorrowsongbutnoonecaredabout?TheterriblefeelingisjustlikeaalbatrossaroundmyneckuntilImetCS.Mydoorisopenedandtheskyislightened.I

6、tisamomentformyrenaissance,whichmakesmeunderstandthemeaningofsunshineandrainbowonceagain.WhenIcatchsightofyou,youaretheonlyoneinmyview.Whenyouareoutofsight,youaretheonlyoneinmyheart.WhenIamawake,youaretheonlyoneinmymind.WhenIamasleep,youaretheonlyoneinmydream.Youaremyonlyone.Iwanttocatchsightofyouev

7、eryminuteandeverysecond.-LetterfromKamel(ZS)toCS15/38TheStoryofZCSKamelhaslotsofphotosofCS.Hewantstofillhisdesktopwithherphotos.Thedesktopismadeupofmrowsandncolumns.Onephotooccupiesbconsecutivesquaresinaroworbconsecutivesquaresinacolumn.HewanttoknowwanttoknowwhetheritispossibletocoverhisdesktopwithC

8、Ssphotoswhilenotwophotosoverlapwitheachotherandthewholedesktopisfull?YoumayassumethatKamelhastensofthousandsofphotosofCS.IfitispossiblethenprintYESelseprintNO.16/38TheStoryofZCS3、InputThefirstlineoftheinputsisT(nomorethan120),whichstandsforthenumberoftestcasesyouneedtosolve.EachlinehasthreenumbersM,

9、N,B(1=M,N=1000,1=B=M,N).4、OutputEachYESorNOoccupiesoneline.17/385、SampleInput24845736、SampleOutputYESNO7、SourcekamelTheStoryofZCS18/38分析分析TheStoryofZCS证明证明19/38#includevoidmain()intT,M,N,B;scanf(%d,&T);while(T-)scanf(%d%d%d,&M,&N,&B);if(M%B=0|N%B=0)printf(YESn);elseprintf(NOn);TheStoryofZCS20/381021

10、 Fibonacci Again21/38ProblemDescription There are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) + F(n-2) (n=2). Input Input consists of a sequence of lines, each containing an integer n. (n 1,000,000).Output Print the word yes if 3 divide evenly into F(n). Print the word no i

11、f not.22/38Sample input: 0 1 2 3 4 5 6Sample output: no no yes no no no yesTimeLimit:2000/1000MS(Java/Others)MemoryLimit:65536/32768K(Java/Others)23/38题目分析:能被能被3 3整除的整数的特点?整除的整数的特点?还要看程序吗?还要看程序吗?如果两个数的和能被如果两个数的和能被3 3整除,这两个数有什么特点?整除,这两个数有什么特点?关于能否被关于能否被3 3整除,这两个数一共有多少种组合?整除,这两个数一共有多少种组合?24/38#include

12、intmain()longn;while(scanf(%ld,&n)!=EOF)if(n%8=2|n%8=6)printf(yesn);elseprintf(non);return0;25/38http:/ 入门训练的好选择入门训练的好选择题目评述:27/38ProblemDescription The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the eleva

13、tor will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop.For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on t

14、he 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.Input There are multiple test cases. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100. A test case with N = 0 denotes t

15、he end of input. This test case is not to be processed.Output Print the total time on a single line for each test case. 28/38Sampleinput:1232310Sampleoutput:174129/38intmain()intn,i,a101;while(scanf(%d,&n) &n!=0)a0=0;intvalue;for(i=1;i=n;i+)scanf(%d,&value);ai=value;intsum=0;for(i=0;in;i+)if(aiai+1)

16、sum=sum+(ai-ai+1)*4+5);printf(%dn, sum); return0;30/38http:/ 31/38ProblemDescription Given a positive integer N, you should output the most right digit of NN.Input The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases

17、 follow.Each test case contains a single positive integer N(1=N=1,000,000,000).Output For each test case, you should output the rightmost digit of NN.32/38Sampleinput:234Sampleoutput:7633/38数据规模数据规模 很大很大暴力方法暴力方法 该打该打基本思路基本思路 规律规律34/38#includemain()intT,i,a,digit;longn;scanf(%d,&T);for(i=0;iT;i+)scan

18、f(%ld,&n);a=n%10;if(a=0|a=1|a=6|a=9|a=5)digit=a;elseif(a=4)digit=6;elseif(a=2)if(n%4=0)digit=6;elsedigit=4;elseif(a=3)if(n%4=1)digit=3;elsedigit=7;elseif(a=7)if(n%4=1)digit=7;elsedigit=3;elseif(n%4=0)digit=6;elsedigit=4;printf(%dn,digit);35/38人见人爱人见人爱ABAB 求求AB的最后三位数表示的整数的最后三位数表示的整数(1=A,B=10000)2 3 1

19、2 6 8 98436/38HDOJ_2035 人见人爱AB 最暴力的暴力?最暴力的暴力?改进的暴力?改进的暴力?还有规律么?还有规律么?37/38#includeintmain()inta,b,i,a0,m;while(scanf(%d%d,&a,&b)!=EOF&(a!=0|b!=0)a0=a=a%1000;for(i=2;i=b;i+)m=a0*a;a0=m%1000;printf(%dn,a0);return0;38/38附:初学者常见问题初学者常见问题39/38一、编译错误Main函数必须返回int类型(正式比赛)不要在for语句中定义类型_int64不支持,可以用longlong代

20、替使用了汉语的标点符号itoa不是ansi函数能将整数转换为字符串而且与ANSI标准兼容的方法是使用sprintf()函数intnum=100;charstr25;sprintf(str,%d,num);另外,拷贝程序容易产生错误40/38常见的代码:scanf(%dn,&icase);for (i=0;iicase;i+) scanf(%c%d%d,&opera,&num1,&num2); 41/38思考上面程序上面程序有什么问题?有什么问题?42/38其他问题:1、需要什么基础?(C/C+)4、可以退课吗?(Ofcourse!)3 、如何加入集训队、如何加入集训队? 2、英语不好怎么办?(问题不大)

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 建筑/环境 > 施工组织

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号