C++第10周項目2擴展之2參考——迭代求和

課程首頁地址:http://blog.csdn.net/sxhelijian/article/details/7910565


【項目2擴展之2(選做)】計算下面的式子,不能使用求冪函數pow()
式一:

#include <iostream>
using namespace std;
int main( )
{ int i,m=1;  //m表示2的冪,1為2的0次冪,與循環相符double sum=0, s=1.0; //s直接取double型,保證除的結果是浮點型for(i=0;i<=8;++i){sum=sum+s/m;//一樓指出的錯誤表達式是sum=sum+s*i/m;m*=2;   //2的i次冪由此計算得到s=-s;    }cout<<"sum="<<sum<<endl;return 0;
}
思考:本題只累加了9次,如果照此累加20次,會產生什么問題,如何處理?


式二:

#include <iostream>
using namespace std;
int main( )
{ int i,f=1;  //f表示加數分母上的階乘,初值1為1的階乘double sum=0, s=1.0; //s直接取double型,保證除的結果是浮點型for(i=1;i<=9;++i){f*=i;   //計算得到i的階乘sum=sum+s/f;//1樓指出的錯誤表達式是sum=sum+s*i/f;s=-s;    }cout<<"sum="<<sum<<endl;return 0;
}





轉載于:https://www.cnblogs.com/hehe520/archive/2012/10/31/6142651.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/274537.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/274537.shtml
英文地址,請注明出處:http://en.pswp.cn/news/274537.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

swift自行車品牌介紹_品牌101:簡介

swift自行車品牌介紹Sometimes when I’m around designer friends and there’s a lull in the conversation one of us will blurt out, “What is branding, anyway?” Then we shrug our shoulders and chuckle, knowing that the answer is far too complex to sum up in…

flutter 透明度動畫_Flutter中的動畫填充+不透明度動畫?

flutter 透明度動畫Flutter SDK provides us with many widgets which help us in animating elements on screen easily by implicitly managing the animations i.e. we need not worry about creating and managing intances of AnimationController during the lifecycle o…

阿里 P10 是怎樣的存在?

談起中國頂尖的程序員&#xff0c;很多人首先會想到之前的雷軍、張小龍&#xff0c;還有現在的多隆、行癲、道哥等人&#xff0c;但今天我想聊一聊的這位大神&#xff0c;他的技術成就也同樣令人矚目。19 年獲得國家技術發明二等獎、20 年獲得國家計算機協會頒發的“ CCF 杰出工…

vba交付圖表設計_您是在為交付目的而“設計”嗎?

vba交付圖表設計重點 (Top highlight)It’s a regular Monday morning. All the design team is organizing the tasks for the ongoing week and reviewing requirements and deadlines for the various projects at the studio or company you work at. Suddenly, among the …

正則表達式限制文本框只能輸入數字

許多時候我們在制作表單時需要限制文本框輸入內容的類型&#xff0c;下面我們用正則表達式限制文本框只能輸入數字、小數點、英文字母、漢字等各類代碼。 1.文本框只能輸入數字代碼(小數點也不能輸入) <input οnkeyup"this.valuethis.value.replace(/\D/g,)" …

前端必讀書籍推薦

大家好&#xff0c;我是若川。持續組織了近一年的源碼共讀活動&#xff0c;感興趣的可以 點此掃碼加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系…

window程序設計學會_是時候我們學會設計合適的餅圖了

window程序設計學會Pie charts are common in data science — next to the 餅形圖在數據科學中很常見- bar chart and the line plot, the pie chart is incredibly standard and simple. A circle is split into several slices, with each slice’s angle representing how…

「非廣告」程序員如何才能盡量避免被裁?

大家好&#xff0c;我是若川。持續組織了近一年的源碼共讀活動&#xff0c;感興趣的可以 點此掃碼加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系…

使用css制作三角,兼容IE6,用到的標簽divsspan

使用css來制作三角&#xff0c;在日常用得較多。恰好這幾天項目中有用到&#xff0c;之前只是從網上copy下來代碼直接用&#xff0c;但是今天在用的時候遇到一些問題&#xff0c;于是借此機會把這個css繪制三角好好研究下吧。 我們分別寫一個<div>,<s>,<span>…

培訓師 每小時多少錢_每個產品設計師需要了解的品牌知識

培訓師 每小時多少錢重點 (Top highlight)These days, it pays to know about brand. The rise of startups has created thousands of new brand design opportunities, and people of all disciplines are working to help brands compete in a crowded world. Increasingly,…

Android 綁定遠程服務出現 Not Allowed to bind service

E/AndroidRuntime(3783): Caused by: java.lang.SecurityException: Not allowed to bind to service Intent { actcom.lenovo.pleiades.conntek.pad } 主要原因是服務中設有簽名保護&#xff0c;該服務上一次是通過A設備中的Eclipse簽名的&#xff0c;這一次是通過B設備中的Ec…

axios 發布 v1.1.0 據說導致很多網站癱瘓~那么如何自動提升版本號呢~

- 大家好&#xff0c;我是若川。友情提醒&#xff0c;今天還是周二。就不發長篇技術文了~近日&#xff0c;axios 發布了 v1.1.0 版本&#xff0c;調用 axios.get 時報錯&#xff0c;據說導致請求無效很多網站癱瘓。目前官方已發布了 v1.1.1 v1.1.2 修復了該問題。讓我想起群友在…

七月時忙碌而充實的_如何減少忙碌而更有效

七月時忙碌而充實的In our hectic modern world, we believe that rushing from one task to the next and managing multiple priorities shows everyone that we are productive.在忙碌的現代世界中&#xff0c;我們相信從一項任務過渡到下一項任務并處理多項優先事項可以向所…

github上閱讀源碼很費勁?你可能不知道這幾個神器

大家好&#xff0c;我是若川。今天周四&#xff0c;再熬一天就可以放假了。有時候在github瀏覽到合適的倉庫時&#xff0c;總想著打開看看源碼&#xff0c;但又不想克隆。推薦幾個在線編輯器打開github倉庫的網站~最后歡迎大家在文末投票&#xff0c;看看大概有多少人知道或者不…

Codeforces Round #149 (Div. 2)【AK】

吐槽&#xff1a;比賽剛開始codeblocks出了點問題。。邊看題邊弄編譯器。。。囧。。 D居然一直沒看。。因為E題意好懂。。然后sb地卡了一場E。。。戰斗力太不穩定。。。 A... A 1 #include<cstdio>2 #include<cstring>3 #include<iostream>4 #define N 10001…

ui設計卡片陰影_UI設計形狀和對象基礎知識:陰影和模糊

ui設計卡片陰影第三部分 (Part 3) Welcome to the third part of the UI Design super-basics. This time we’ll cover two of the most commonly used effects — shadows and blurs.歡迎使用UI設計超級基礎的第三部分。 這次我們將介紹兩種最常用的效果- 陰影和模糊 。 Und…

干貨 | 帶你玩轉前端性能優化!【留言送書】

大家好&#xff0c;我是若川。之前送過三次Vue的書&#xff0c;現在又和博文視點合作再次爭取了幾本書&#xff0c;具體送書規則看文末。很多人可能有耐心花費一兩個小時在一家火鍋店門口排隊&#xff0c;但幾乎沒有人愿意等30s去加載一個短視頻。事實上&#xff0c;對于大多數…

css3學習手記

完全匹配屬性選擇器&#xff1a;[idvalue] 包含匹配選擇器&#xff1a;[id*value] 首字符匹配選擇器&#xff1a;[id^value] 尾字符匹配選擇器&#xff1a;[id$value] 陰影&#xff1a;box-shadow:3px 2px 1px #000 /*其他瀏覽器*/ -webkit-box-shadow:3px 2px 1px #…

如何進入游戲行業_進入設計行業

如何進入游戲行業We’re living in some weird-ass times. One of the unfortunate results of a global pandemic is loss of jobs and financial security. While people continue to deal with this, the prospect of entering a new field — especially one that’s sligh…

據說99%的人不知道 vue-devtools 還能直接打開對應組件文件?

大家好&#xff0c;我是若川。據說 99% 的人不知道 vue-devtools 還能直接打開對應組件文件&#xff1f;本文原理揭秘曾經寫過以上這篇文章&#xff0c; 也是源碼共讀中的第一期(點擊文末閱讀原文直達)。這個功能如下圖所示。歡迎大家來投票&#xff0c;你的投票很重要。如果不…