2015年蓝桥杯C/C++ B组题目题解
1.?輸入一個(gè)字符串,求它包含多少個(gè)單詞。單詞間以一個(gè)或者多個(gè)空格分開(kāi)。
第一個(gè)單詞前,最后一個(gè)單詞后也可能有0到多個(gè)空格。
比如:" abc xyz" 包含兩個(gè)單詞,"ab c xyz " 包含3個(gè)單詞。
如下的程序解決了這個(gè)問(wèn)題,請(qǐng)?zhí)顚?xiě)劃線部分缺失的代碼。
注意:只填寫(xiě)劃線部分的代碼,不要填寫(xiě)任何多余的內(nèi)容。比如已經(jīng)存在的小括號(hào),注釋或說(shuō)明文字等。
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include <math.h>using namespace std;int get_word_num(char* buf) {int n = 0;int tag = 1;char* p = buf;for( ;*p!=0&&*p!=13 && *p!=10; p++){if(*p==' '&&tag==0) tag=1;if( *p!=' ' && tag==1 ){n++; tag=0;}}return n; }int main() {char buf[1000];fgets(buf, 1000, stdin);printf("%d\n", get_word_num(buf));return 0; }?
2. ? ??1/1 + 1/2 + 1/3 + 1/4 + ... 在數(shù)學(xué)上稱為調(diào)和級(jí)數(shù)。
它是發(fā)散的,也就是說(shuō),只要加上足夠多的項(xiàng),就可以得到任意大的數(shù)字。
但是,它發(fā)散的很慢:
前1項(xiàng)和達(dá)到 1.0
前4項(xiàng)和才超過(guò) 2.0
前83項(xiàng)的和才超過(guò) 5.0
那么,請(qǐng)你計(jì)算一下,要加多少項(xiàng),才能使得和達(dá)到或超過(guò) 15.0 呢?
請(qǐng)?zhí)顚?xiě)這個(gè)整數(shù)。
注意:只需要填寫(xiě)一個(gè)整數(shù),不要填寫(xiě)任何多余的內(nèi)容。比如說(shuō)明文字。
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include <math.h>using namespace std;int main() {double ans=0;double i=1.0;while(ans < 15.0){ans+=(1.0/i);i+=1.0;}printf("%lf %lf\n", ans, i);return 0; }? ?答案:15.000000 ? ?1835422.000000 ? ?應(yīng)為:1835422
?3. ??如果x的x次冪結(jié)果為10(參見(jiàn)【圖1.png】),你能計(jì)算出x的近似值嗎?
顯然,這個(gè)值是介于2和3之間的一個(gè)數(shù)字。
請(qǐng)把x的值計(jì)算到小數(shù)后6位(四舍五入),并填寫(xiě)這個(gè)小數(shù)值。
注意:只填寫(xiě)一個(gè)小數(shù),不要寫(xiě)任何多余的符號(hào)或說(shuō)明。
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include <math.h> #define eps 1e-7using namespace std;int main() {double aim = 10.0;double x;double L=2.0, R=3.0;//二分枚舉while(L-R < (-eps)){double mid=(L+R)/2;if( pow(mid,mid) > aim ){R=mid;}else{L=mid;}}printf("%lf\n", pow(L, L)); //最后得到的是9.999999printf("%lf %lf\n", L, R); //L=R=2.506184return 0; }
?
轉(zhuǎn)載于:https://www.cnblogs.com/yspworld/p/5042844.html
總結(jié)
以上是生活随笔為你收集整理的2015年蓝桥杯C/C++ B组题目题解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 英国的挑战者2坦克?
- 下一篇: c++笔试题两道,求解当中一道