每日学习分享
今天我学到了什么——
1、bool——素数的应用

bool b = false;

以上声明,相当于变量b 的值是false,当用于循环内部会改变布尔的值时,需要考虑使用时的位置。
例如,在寻找素数的过程中,bool 应放在内循环外。

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>		// 包含有bool 的头文件

int main(int argc, char *argv[]) {
	int m, n;
	bool b = true;
	for(m = 101; m < 200; m++){
		b = true;
		for(n = 2; n< m; n++){
			if((m % n) == 0 )
				b = false;	
		}
		if(b == true)
			printf("%d\n",m);
	}
	return 0;
	
}

2、斐波那契数列的应用
例题如下:
古典问题(兔子生崽):有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?(输出前40个月即可)
代码是这样的:

#include<stdio.h>
 
int main()
{
    int f1=1,f2=1,i;
    for(i=1;i<=20;i++)
    {
        printf("%12d%12d",f1,f2);
        if(i%2==0) printf("\n");
        f1=f1+f2;
        f2=f1+f2;
    }
    
    return 0;
}

但在分析的过程中,使用斐波那契函数数列同样可以解决。
于是思考在写程序的时候可以从简单的已知入手,然后根据条件寻找线索,逐步**。
在掌握一些基础方法的时候,应当注意将所学理论拓展应用在其他地方,才会又更多的收获!!!
代码分享如下:

#include <stdio.h>
#include <stdlib.h>
int fab(int n); 

int main(int argc, char *argv[]) {
	int n;
	
	scanf("%d",&n);
	printf("%d\n",fab(n));
	printf("done!");
	
	return 0;	
}

int fab(int n){
	if(n == 2 || n == 1)
		return 2;
	else
		return (fab(n-1) + fab(n-2));
	
	return 0;	
}

内容复习:声明和定义函数

4、ASCII 码 与 特殊图案
特殊图案的输出应该是通过ASCII转换而成的,因此掌握一些常用和有趣的ASCII码很有必要呀~
例如:\1 表示笑脸,\14表示心心,\176 和\219是常用黑白键的打印……

#include <stdio.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
	int i, j;
	
	printf("\2\2\n");
	for (i = 1; i < 13; i ++){
		for (j = 0; j <= i; j++){
			printf("%c",176);
		}
		printf("\n");
	}
	return 0;
}

每日学习分享Day1
继续加油哟!

相关文章:

  • 2021-08-03
  • 2021-11-11
  • 2021-06-06
  • 2021-09-15
  • 2021-09-29
  • 2021-10-28
  • 2021-09-22
  • 2021-07-22
猜你喜欢
  • 2021-04-24
  • 2021-06-17
  • 2021-11-07
  • 2021-05-24
  • 2021-05-10
  • 2021-09-09
  • 2021-06-29
相关资源
相似解决方案