滴水逆向联盟
标题: 基于visual Studio2013解决C语言竞赛题之0307函数求值 [打印本页]
作者: 大灰狼 时间: 2014-10-10 09:45
标题: 基于visual Studio2013解决C语言竞赛题之0307函数求值
题目
解决代码及点评这又是个条件函数,但是这个函数无法用switch来解决,因为switch只能用于和某条件相等情况下,而这个函数的范围是无穷的
遇到这种问题,我们还是需要用复合的if语句来解决
- <pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code">#include <stdio.h>
- #include <stdlib.h>
- void main()
- {
- float x;
- float y;
-
- printf("please input x\n");
- scanf_s("%f",&x);
- if (x>=0&&x<15) // 如果x在0~15之间,
- {
- y=x*40/15+10; // 计算函数值
- }
- else if (x<30) // 如果x在15到30之间,则固定为50,这里x<30条件即表示15到30,因为小于15时,第一个if语句处理掉了,以下类似
- {
- y=50;
- }
- else if (x<45)
- {
- y= 50-(x-30)*10/15;
- }
- else if (x<75)
- {
- y=(x-45)*20/30+40;
- }
- else if (x<90)
- {
- y=60-(x-75)*10/15;
- }
- else
- {
- printf("无意义");
- return;
- }
-
- printf("%f",y);
-
-
-
- system("pause");
- }
- </pre><br><br>
欢迎光临 滴水逆向联盟 (http://www.dtdebug.com/) |
Powered by Discuz! X3.2 |