1. 程式人生 > >第一章:程序設計入門

第一章:程序設計入門

\n ont 浮點型 表達 math 結果 模板 題目 大表

隨學筆記:

小計:

<1>: ACM比賽中不能使用#include<conio.h> 中包含的getch(),clrscr()等函數,不能使用getche(),gotoxy()等函數。

<2>: 算法競賽中如發現題目有異議應向相關人員詢問,不應主觀臆斷。

  例如: 例題1-2中所講三位數翻轉末尾0是否輸出。

<3>: 盡量使用const關鍵字申明常數。

  例如:const double Pi =acos(-1.0);

<4>: 邏輯運算符:

  &&和&

& 無論左邊結果是什麽,右邊還是繼續運算;

&&當左邊為假,右邊不再進行運算。

但是兩者的結果是一樣的

  ||和|   

    | 無論左邊結果是什麽,右邊還是繼續運算;

||當左邊為真,右邊不再進行運算。

但是兩者的結果是一樣的

  無特殊要求情況下,相比之下&&和||更高效。

<5>: 平時練習和寫模板時應適當添加註釋讓別人更容易理解,讓自己保持思路清晰。

  //內容 OR

  /* 內容 */

例題1-1:

  <1>:浮點型輸入輸出時:float輸入輸出都是%f,double輸入時為%lf,輸出時為%f。

  

 1 #include <cstdio>
 2 #include <iostream>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     double t1;
 8     scanf("%lf",&t1);
 9     printf("%f\n",t1);
10 
11     float t2;
12     scanf("%f",&t2);
13     printf("%f\n",t2);
14     
15     return 0;
16 }

  <2>:圓周率π用表達式 acos(-1.0)表示。

     這其中acos為math.h裏的函數,double acos(double x);//x∈[-1,1]。//應用函數時記得申明頭文件#include<cmath>

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <iostream>
 4 using namespace std;
 5 const double Pi =acos(-1.0);
 6 int main()
 7 {
 8 
 9     printf("%f\n",Pi);
10 
11     return 0;
12 }

   相關習題及代碼:

1-4: 正弦和余弦

輸入integer n,輸入n的正弦和余弦值。

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <iostream>
 4 using namespace std;
 5 const double Pi=acos(-1.0);
 6 
 7 int main()
 8 {
 9     int n;
10     while(~scanf("%d",&n)){
11         double x=n*1.0/180*Pi;
12         printf("%.2f\n%.2f\n",sin(x),cos(x));
13     }
14     return 0;
15 }

課後問題:

  <1>: int 型數值的最大表示範圍:

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <iostream>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int n1=1,cnt1=1;
 9     while(++n1>0)
10       cnt1++;
11     printf("%d\n",cnt1);
12 
13     int n2=-1,cnt2=-1;
14     while(--n2<0)
15       cnt2--;
16     printf("%d\n",cnt2);
17     return 0;
18 }

  <2>: else總是與他前面離他最近的未配對的if配對。

 1 #include <cstdio>
 2 #include <iostream>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     int a=1,b=2;
 8     if(a)
 9       if(b)
10         a++;
11       else
12         b++;
13     printf("%d\n%d\n",a,b);
14 
15      return 0;
16 }


  /*可替換代碼為
    if(a)
  b ? a++:b++;
  */

第一章:程序設計入門