C語言程式設計100例之(7):級數求和

来源:https://www.cnblogs.com/cs-whut/archive/2019/11/16/11870415.html
-Advertisement-
Play Games

例7 級數求和 題目描述 已知: Sn =1+1/2+1/3+…+1/n。顯然對於任意一個整數 k,當 n 足夠大的時候,Sn>k。 現給出一個整數 k,要求計算出一個最小的 n,使得 Sn>k。 輸入格式 一個正整數 k 輸出格式 一個正整數 n 輸入樣例 1 輸出樣例 2 (1)編程思路。 用簡 ...


例7    級數求和

題目描述

已知: Sn =1+1/2+1/3+…+1/n。顯然對於任意一個整數 k,當 n 足夠大的時候,Sn>k。

現給出一個整數 k,要求計算出一個最小的 n,使得 Sn>k。

輸入格式

一個正整數 k

輸出格式

一個正整數 n

輸入樣例

1

輸出樣例

2

        (1)編程思路。

        用簡單的迴圈完成多項式求和。迴圈控制條件為和S<=K。

        (2)源程式。

#include <stdio.h>

int main()

{

         int k,n;

         double s;

         s=0;

         n=0;

         scanf("%d",&k);

         do {

                   n++;

                   s+=1.0/n;

         }while (s<=k);

         printf("%d\n",n);

         return 0;

習題7

7-1  Deck

        本題選自北大POJ題庫 (http://poj.org/problem?id=1607)

Description

A single playing card can be placed on a table, carefully, so that the short edges of the card are parallel to the table's edge, and half the length of the card hangs over the edge of the table. If the card hung any further out, with its center of gravity off the table, it would fall off the table and flutter to the floor. The same reasoning applies if the card were placed on another card, rather than on a table.

Two playing cards can be arranged, carefully, with short edges parallel to table edges, to extend 3/4 of a card length beyond the edge of the table. The top card hangs half a card length past the edge of the bottom card. The bottom card hangs with only 1/4 of its length past the table's edge. The center of gravity of the two cards combined lies just over the edge of the table.

Three playing cards can be arranged, with short edges parallel to table edges, and each card touching at most one other card, to extend 11/12 of a card length beyond the edge of the table. The top two cards extend 3/4 of a card length beyond the edge of the bottom card, and the bottom card extends only 1/6 over the table's edge; the center of gravity of the three cards lines over the edges of the table.

If you keep stacking cards so that the edges are aligned and every card has at most one card above it and one below it, how far out can 4 cards extend over the table's edge? Or 52 cards? Or 1000 cards? Or 99999?

Input

Input contains several nonnegative integers, one to a line. No integer exceeds 99999.

Output

The standard output will contain, on successful completion of the program, a heading:

Cards Overhang

(that's two spaces between the words) and, following, a line for each input integer giving the length of the longest overhang achievable with the given number of cards, measured in cardlengths, and rounded to the nearest thousandth. The length must be expressed with at least one digit before the decimal point and exactly three digits after it. The number of cards is right-justified in column 5, and the decimal points for the lengths lie in column 12.

Sample Input

1

2

30

Sample Output

Cards  Overhang

    1     0.500

    2     0.750

   30     1.997

        (1)編程思路。

        本題題意為:將紙牌沿桌子推出,第1張推出桌面1/2,第2張推出第1張1/4,第3張1/6,依次類推。求n張紙牌推出桌面的長度。

        即輸入正整數n,求S=1/2+1/4+…+1/(2n)的值。

        簡單迴圈處理即可。

      (2)源程式。

#include <stdio.h>

int main()

{

         int n,i;

         double s;

         printf("Cards  Overhang\n");

         while (scanf("%d",&n)!=EOF)

         {

            s=0;

            for (i=1;i<=n;i++)

                   s+=1.0/(2*i);

            printf("%5d  %8.3lf\n",n,s);

         }

         return 0;

 }

7-2  求π的值

題目描述

根據公式

 

 

 計算π的值,要求精確到最後一項的絕對值小於10–4。

輸入格式

輸出格式

求得的π的值。

      (1)編程思路。

        若計算如下多項式

 

 

         用一個簡單迴圈即可實現。

           int n=1;

           double p=0.0;

    while (1.0/n>=0.0001)

    {

        p=p+1.0/n;

        n+=2;

    }

        但現在是加1項然後減1項交替進行,如何解決?

        一個最簡單的辦法是用一個變數flag作為符號標誌,初始時f=1,運算p=p+1.0*f/n=p+1.0/n;完成加項運算;之後 f=-f,f值為-1,運算p=p+1.0*f/n=p-1.0/n;完成減項運算;再之後 f=-f,f值為1,…。這樣,通過f的值在1,-1之間切換從而完成加項與減項的交替進行。

         (2)源程式。

#include <stdio.h>

int main()

{

           int n=1,f=1;

           double p=0.0;

           while (1.0/n>=0.0001)

    {

        p=p+1.0*f/n;

        f=-f;

        n+=2;

    }

    printf("%f\n",4*p);

    return 0;

}

7-3  u Calculate e

        本題選自北大POJ題庫 (http://poj.org/problem?id=1517)

Description

A simple mathematical formula for e is

e=Σ0<=i<=n1/i!

where n is allowed to go to infinity. This can actually yield very accurate approximations of e using relatively small values of n.

Input

No input

Output

Output the approximations of e generated by the above formula for the values of n from 0 to 9. The beginning of your output should appear similar to that shown below.

Sample Input

no input

Sample Output

n e

- -----------

0 1

1 2

2 2.5

3 2.666666667

4 2.708333333

...

    (1)編程思路。

        題目要求根據公式e=1+1/1!+1/2!+…+1/n!求e的值。可以用一個簡單迴圈完成計算。設初始時,e=1.0,p=1,迴圈程式編寫如下:

         for (i=1;i<=n;i++)

         {

                  p=p*i;

                  e=e+1.0/p;

         }

        註意:不要寫成二重迴圈

         for (e=1.0, i=1;i<=n;i++)

         {

                  for (p=1,j=1;j<=i;j++)   // 迴圈求 i! 的值

                       p=p*i;

                    e=e+1.0/p;

         }

        因為,i!=i*(i-1)!,這樣求i!時可以利用前一次求得的(i-1)!,無需每次重新求取。

        (2)源程式。

#include <stdio.h>

int main()

          int i,p;

         double e;

         printf("n e\n");

         printf("- -----------\n");

         printf("0 1\n");

         e=1.0;

         p=1;

         for (i=1;i<=9;i++)

         {

        p=p*i;

        e=e+1.0/p;

        if (i==1) printf("%d %.0f\n",i,e);

        else if (i==2) printf("%d %.1f\n",i,e);

       else printf("%d %.9f\n",i,e);

         }

        return 0;

}


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • ECharts動態獲取後臺傳過來的json數據進行多個折線圖的顯示,折線的數據由後臺傳過來 ECharts 多個折線圖動態獲取json數據 效果圖如下: js部分: function mychart1(datetime,dateNums1,dateNums2,dateNums3,dateNums4) ...
  • 一、圖表背景色配置項,如背景顏色漸變 https://www.echartsjs.com/zh/option.html#backgroundColor 二、圖表中圖形的顏色,如柱狀圖行採用漸變顏色顯示 options = { series: [ { name: '財經新聞', barWidth: ' ...
  • 直接上核心代碼,其實官網介紹的很詳細: var pageSize = 5;//每次請求新聞的條數 flow.load({ elem: '#newsList' //指定列表容器 ,scrollElem: '#newsList'//滾動條所在元素 ,done: function(page, next){ ...
  • 最主要的css樣式: white-space: nowrap; overflow: hidden; text-overflow: ellipsis; 例子: 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> ...
  • 本文由葡萄城技術團隊於博客園翻譯並首發 轉載請註明出處:葡萄城官網,葡萄城為開發者提供專業的開發工具、解決方案和服務,賦能開發者。 JavaScript最初是為Web應用程式創建的。但是隨著前端技術的發展,大多數開發人員更喜歡使用基於JavaScript的框架。它簡化了你的代碼以及使你能完成更多全棧 ...
  • (馬蜂窩技術原創內容,公眾號 ID: mfwtech) 引言 消費者的狂歡節「雙 11」剛剛過去。在電商競爭環境日益激烈的今天,為了抓住流量紅利,雙 11 打響的已經不僅僅是「促銷戰」,也是「營銷戰」,這對平臺的技術支撐能力提出新的要求。 從 2014 年的「318 大促」,到正在進行的 「馬蜂窩雙 ...
  • 阿裡巴巴高級無線開發專家宋照春在高德技術專場做了題為《高德客戶端及引擎技術架構演進與思考》的演講,主要分享了高德地圖客戶端技術架構沿著「上漂下沉」、「模塊化、Bundle化」的思路演進所做的一系列架構升級中的經驗和思考。 ...
  • 例8 尼科徹斯定理 題目描述 尼科徹斯定理可以敘述為:任何一個整數的立方都可以表示成一串連續的奇數的和。需要註意的是,這些奇數一定是連續的,如:1,3,5,7,9,…。 例如,對於整數5,5*5*5=125=21+23+25+27+29。 對於整數6,216=31+33+35+37+39+41, 也 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...