1.程序設計入門

內容

算數表達式

#include<stdio.h>
#include<math.h>

int main()
{
	printf("%.8f\n", 1+2*sqrt(3)/(5-0.1));
	return 0;
}
  1. #include<stdio.h> 頭文件,標準輸入輸出,standard input output.header
  2. #include<math.h> 頭文件,關於數學的一些運算
  3. 整數/整數=整數,浮點數/浮點數=浮點數
  4. sqrt(x),計算x的算數平方根

變量及其輸入

計算圓柱體的表面積

#include<stdio.h>
#include<math.h>
 
int main()
{
	const double pi = acos(-1.0);
	double r, h, s1, s2, s;
	scanf("%lf%lf", &r, &h);
	s1 = pi*r*r;
	s2 = 2.0*pi*r*h;
	s = s1*2.0 + s2;
	printf("Area = %.3f\n", s);
	return 0;
}
  1. const關鍵字聲明常數
  2. acos爲求反餘弦函數,cosπ=-1,arccos(-1) = π

順序結構程序設計

一、 三位數反轉
1.
```
#include<stdio.h>

int main()
{
    int n;
	scanf("%d", &n);
	printf("%d%d%d\n", n%10, n/10%10, n/100);
	return 0; 
}
```
  1. #include<stdio.h>
    
    int main()
    {
    	int n, m;
    	scanf("%d", &n);
    	m = (n%10)*100+(n/10%10)*10+n/100;
    	printf("%03d\n", m);
    	return 0; 
    }
    

二、交換變量

  1. 使用中間變量
    #include<stdio.h>
    
    int main()
    {
    	int a, b, t;
    	scanf("%d%d", &a, &b);
    	t = a;
    	a = b;
    	b = t;
    	printf("%d %d", a, b);
    	return 0;
    }
    
  2. 不使用中間變量
    #include<stdio.h>
    
    int main()
    {
    	int a, b, t;
    	scanf("%d%d", &a, &b);
    	a = a + b;
    	b = a - b;
    	a = a - b;
    	printf("%d %d", a, b);
    	return 0;
    }
    

注: 算法競賽是在比誰能更好地解決問題,而不是在比誰寫的程序看上去更高級。

分支結構程序設計

  1. 雞兔同籠
    #include<stdio.h>
    
    int main()
    {
      int a, b, n, m;
      scanf("%d%d", &n, &m);
      a = (4*n-m)/2;
      b = n-a;
      if(m % 2 == 1 || a<0 || b<0)
      {
      	printf("No answer\n");	
      }
      else
      {
      	printf("%d %d\n", a, b);
      }
      return 0;
    }
    
    C語言中的邏輯運算符都是短路運算符。一旦能夠確定整個表達式的值,就不再繼續計算。用數學知識算出表達式,再用程序實現。
  2. 三整數排序
    #include<stdio.h>
    
    int main()
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        if(a > b)
        {
        	a = a+b;
        	b = a-b;
        	a = a-b; 
        }
        if(a > c)
        {	
        	a = a+c;
        	c = a-c;
        	a = a-c; 
        }
        if(b > c)
     	{
    		b = b+c;
    		c = b-c;
    		b = b-c;
    	}
    	printf("%d %d %d\n", a, b, c);
    	return 0; 
    }
    
    此處後面設計數組之後可用排序算法。

練習

  1. 輸出%d
    #include<stdio.h>
    
    int main()
    {
    
    	printf("%%d");
    	
    	return 0;
    }
    
  2. 輸入三個整數,輸出他們的平均值,保留三位小數。
    #include<stdio.h>
    
    int main()
    {
      int a, b, c;
      scanf("%d%d%d", &a, &b, &c);
      printf("%.3f",(float)(a+b+c)/3.0);
      return 0;
    }
    
  3. 輸入華氏溫度f,輸出對應的攝氏溫度c,保留三位小數。c=5(f-32)/9
    #include<stdio.h>
    
    int main()
    {
    	float f, c;
    	scanf("%f", &f);
    	c = 5*(f-32)/9;
    	printf("%.3f", c);
    	return 0;
    }
    
  4. 輸入正整數n,輸出1+2+…+n。
      #include<stdio.h>
    
    int main()
    {
    	int n, sum;
    	scanf("%d", &n);
    	sum = 0;
    	for(int i = 1; i<=n; i++){
    		sum = sum+i;
    	}
    	printf("%d", sum);
    	return 0;
    }
    
  5. 輸入正整數n,輸出n度的正弦、餘弦函數值。
    #include<stdio.h>
    #include<math.h>
    
    int main()
    {
    	int n, sum;
    	scanf("%d", &n);
    
    	printf("%f,%f",sin(n),cos(n));
    	return 0;
    }
    
  6. 一件衣服95元,若消費滿300元,可打85折。輸入購買衣服件數,輸出需要支付的金額,保留兩位小數。
    #include<stdio.h>
    #include<math.h>
    
    int main()
    {
    	const int eachJ = 95;
    	int a, all;
    	scanf("%d", &a);
    	all = a*eachJ;
    	if(all >= 300){
    		printf("%.2f", (float)all*0.85);
    	}else{
    		printf("%.2f", (float)all);
    	}
    	return 0;
    }
    
  7. 輸入三角形3條邊的長度值(均爲正整數),判斷是否能爲直角三角形的三個邊長,如果可以,輸出yes,如果不能,則輸出no。如果根本無法構成三角形,則輸出not a triangle。
    #include<stdio.h>
    #include<math.h>
    
    int main()
    {
    	int a, b, c, max;
    	scanf("%d%d%d", &a, &b, &c);
    	if(a > b){
    		if(a > c){
    			max = a;
    		}else{
    			max = c;
    		}
    	}else{
    		if(b > c){
    			max = b;
    		}else{
    			max = c;
    		}
    	} 
    	if((a+b+c-max) > max){
    		// 如果c是斜邊,判斷是直角三角形(a+b)*(a+b) = c*c +2*a*b 
    		int cheng = 2;
    		if(a != max){
    			cheng = cheng*a;
    		}
        	if(b != max){
        		cheng = cheng*b;
        	}
        	if(c != max){
    			cheng = cheng*c;
    		}
    		if((a+b+c-max)*(a+b+c-max) == max*max+cheng){
    			printf("yes");
    		}else{
    			printf("no");
    		}
    	}else{
    		printf("not a triangle");
    	}
    	return 0;
    }
    
  8. 輸入年份,判斷是否爲閏年,如果是,則輸出yes,否則輸出no。
    #include<stdio.h>
    
    int main()
    {
      int a;
      scanf("%d", &a);
      if(a%4 == 0 && a%100 != 0){
      	printf("yes");
      }else{
      	printf("no");
      }	 
      return 0;
    }  
    
  9. int 的最大值最小值?
    #include<stdio.h>
    
    int main()
    {
    	int a;
      	a = 1;
    	while(a+1>0){
    		a++;
    	}
    	printf("int max: %d", a);
    	a = -1;
    	while(a-1<0){
    		a--;
    	}
    	printf("int min: %d", a);
    	return 0;
    }
    


10. double 最多精確到小數點多少位?
```
#include<stdio.h>

int main()
{
	double a;
	a = 4.0;
    printf("%.30f", a/3.0);
	return 0;
}
```


11. double最大正數值,最小正數值?
思路,最小正數值,就是即將變爲0的特別小的值,根據上一問題,最多精確到小數點後16位,最小值也就是0.0000000000000001
最大值目前還不清楚,,,
12. 邏輯運算符的優先級
```
#include<stdio.h>

int main()
{
    int a, b, c;
    a = 1;
    b = 1;
    c = 0;
 //	printf("%d", a || (b && c));
  	printf("%d", a || b && c);
	return 0;
}
```
加不加括號都一樣,表明邏輯與比邏輯或優先級高
  1. if(a)if(b)x++;else y++;的確切含義?
    else是與第二個if配對,if沒有大括號時只會執行下一條語句,如果沒有特殊情況不會執行第二條指令,但是由於此題中if的下一條語句是if根據判斷執行相應的x++或y++。
個人感受

學完這一章收穫還是不小的吧,之前也是學過c語言的,忘了差不多了,今天從頭開始學,學到了很多以前不會的地方,加油!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章