Aha C language study notes

Hello World

#include<stdio.h>
/*
技术要点:
    初学者在编写程序时,经常会忘记在语句后边添加分号,


*/

int main()
{
    
    

	printf("hello world");
	printf("\n");//换行
	return 0;
}

sum operation

/*编写程序:计算10+20 并输出结果
*    解题思路:
* (1)定义一个数据类型
* i 、n、sum均为整型,并为sum赋值:sum=0
* i赋值为10;n赋值为20; 将i与n求和,结果赋值给sum,最后输出sum

*/
//引用头文件
#include<stdio.h>
int main()
{
    
    
	int i, n, sum = 0;
	i = 10;
	n = 20;
	sum = i + n;
	printf("%d\n", sum);

	return 0;
}

data input

//#include<stdio.h>
//#include<stdlib.h>
//
//int main()
//{
    
    
//	while (true)
//	{
    
    
//		system("color  f5");
//		int a, b, c;
//		printf("这是一个加法计算器,欢迎使用\n");
//		printf("-------------------------------\n");
//		printf("请输入第一个数字(按回车键继续):\n");
//		scanf("%d", &a);
//		printf("请输入第二个数字(按回车键继续):\n");
//		scanf("%d", &b);
//		c = a + b;
//		printf("它们的和是:\n");
//		printf("%d", c);
//
//
//		system("pause");
//
//	}
//	return 0;
//}

#include<stdio.h>
#include<stdlib.h>

int main()
{
    
    
	while (1)
	{
    
    
		int a, b, c;
		printf("欢迎使用计算器\n");
		printf("请输入第一个数(按回车键继续):\n");
		scanf("%d", &a);
		printf("请输入第二个数字(按回车键继续):\n");
		scanf("%d", &b);
		c = a + b;
		printf("它们的和为:\n");
		printf("%d+%d=%d\n", a, b, c);
		c = a - b;
		printf("它们的差为:\n");
		printf("%d-%d=%d\n", a, b, c);
		c = a * b;
		printf("它们的积为:\n");
		printf("%d*%d=%d\n", a, b, c);
		c = a / b;
		printf("它们的商为:\n");
		printf("%d/%d=%d\n", a, b, c);





	}


	system("pause");
	return 0;
}

Print square

#include<stdio.h>

int main()
{
    
    
	printf("* * * * * \n");
	printf("*       *\n");
	printf("*       *\n");
	printf("*       *\n");
	printf("* * * * * \n");





	return 0;
}

Calculate the perimeter of a square

/*实例:计算正方形的周长
   解题思路:
   定义数据类型
   变量a定义为整型,表示正方形边长,变量b定义为整形变量,用来存储正方形的周长;
   根据正方形的周长等于边长*4输出周长
   */
//#include<stdio.h>//引用头文件
//
//int main()
//{
    
    
//	int a, b;
//	a = 4;
//	b = a * 4;
//	printf("%d\n", b);
//
//
//	return 0;
//}
#include<stdio.h>

int main()
{
    
    
	int a, b;
	a = 4;
	b = a * 4;
	printf("%d\n", b);

	return 0;
}

Print quotes

/*能否成为真的编程高手,在于是否有毅力坚持学习和练习
* 输出名言:贵有恒,何必三更睡五更起,;最无益,只怕一日曝十日寒				
*/
//引用头文件
#include<stdio.h>

int main()
{
    
    
	printf("贵有恒,何必三更睡五更起;最无益,只怕一日曝十日寒");
		printf("\n");//换行


	return 0;
}

Find the maximum value of three numbers

/*三个数中找最大值的算法思路:
   1、首先先定义四个变量a  b   c  分别存放从键盘输入的三个数  变量d为存放最大数
   2、比较a b 的大小,将最大的数赋值给d
   3、在比较c d 的大小   如果c大于d  则将c的值赋值给d
   4、输出d
*/

#include<stdio.h>
#include<stdlib.h>


int main() {
    
    
	while (1) //或者true
	{
    
    
		//定义四个变量存放键盘输入的三个数合输出最大值的数a b c d
		int  a, b, c, d;
		//从键盘输入三个数
		printf("请输入第一个数(按回车键继续):\n");
		scanf("%d", &a);
		printf("请输入第二个数(按回车键继续):\n");
		scanf("%d", &b);
		printf("请输入第三个数(按回车键继续):\n");
		scanf("%d", &c);
		if (a > b)
			d = a;
		else
			d = b;

		if (c > d)
			d = c;
		printf("最大数为:\n");
		printf("%d\n", d);

		system("pause");
	}
	return 0;
}

方法二
//#include<stdio.h>
//#include<stdlib.h>
//
//int main()
//{
    
    
//	while (1)
//	{
    
    
//		//定义三个变量
//		int a, b, c;
//		//从键盘中输入三个数
//		scanf("%d\n %d\n %d\n %d\n", &a, &b, &c);
//		if (a >= b && a >= c)
//		printf("%d\n", a);
//
//		if (b >= a && b >= c)
//		printf("%d\n", b);
//
//		if (c > a && c > b)
//		printf("%d\n", c);
//
//		system("pause");
//	}
//	return 0;
//}

Determine even number

	/*判断一个数是否是偶数的算法
	  1、首先需要一个房子(变量)装这个数
	  2、判断这个数是不是偶数?(偶数:也就是这个数除2余数为0)
	  
	  */

//#include<stdio.h>
//#include<stdlib.h>
//
//int main()
//{
    
    
//	while (1)
//	{
    
    
//		int a;
//		printf("请输入一个数:\n");
//		scanf("%d", &a);
//		if (a % 2 == 0)
//		printf("是偶数\n");
//		if (a % 2 != 0)
//		printf("不是偶数\n");
//
//
//
//		system("pause");
//	}
//	return 0;
//}


//判断一个数是否是7的倍数的算法

//#include<stdio.h>
//#include<stdlib.h>
//
//int main()
//{
    
    
//	while (1)
//	{
    
    
//		int a;
//		printf("请输入一个数字:\n");
//		scanf("%d", &a);
//		if (a % 7 == 0)
//			printf("是7的倍数\n");
//		if (a % 7 != 0)
//			printf("不是7的倍数\n");
//
//
//		system("pause");
//	}
//	return 0;
//}

#include<stdio.h>
#include<stdlib.h>

int main()
{
    
    
	while (1)
	{
    
    
		int a;
		printf("请输入一个数:\n");
		scanf("%d", &a);
		if (a % 7 == 0)
			//当条件为真时,执行该语句
			printf("是7的倍数\n");
		else
			条件为假时执行该语句
			printf("不是7的倍数\n");


		system("pause");
	}
	return 0;
}

Determine the size of two numbers

//比较两个数的大小

/*算法思路:
1、首先定义三个数a,b,c,  a,b存放键盘输入的两个数  c存放a和b中最大的那个数
int a,b,c;
2、然后读入从键盘输入的两个数,分别存放到变量a和b中
scanf("%d %d",&a,&b);
3、使用if  else 语句判断a,b的大小
if (a>b)  c=a;
else  c=b;
*/


#include<stdio.h>
#include<stdlib.h>

int main()
{
    
    
	while (1)
	{
    
    
		int a, b, c;
		printf("请输入第一个数(按回车键继续):\n");
		scanf("%d", &a);
		printf("请输入第二个数(按回车键继续):\n");
		scanf("%d", &b);
		if (a > b)
			c = a;
		else
			c = b;
		printf("最大数为:\n");
		printf("%d", c);

		system("pause");
	}
	return 0;
}

sort

//比较两个数的大小

/*算法思路:
1、首先定义三个数a,b,c,  a,b存放键盘输入的两个数  c存放a和b中最大的那个数
int a,b,c;
2、然后读入从键盘输入的两个数,分别存放到变量a和b中
scanf("%d %d",&a,&b);
3、使用if  else 语句判断a,b的大小
if (a>b)  c=a;
else  c=b;
*/


#include<stdio.h>
#include<stdlib.h>

int main()
{
    
    
	while (1)
	{
    
    
		int a, b, c;
		printf("请输入第一个数(按回车键继续):\n");
		scanf("%d", &a);
		printf("请输入第二个数(按回车键继续):\n");
		scanf("%d", &b);
		if (a > b)
			c = a;
		else
			c = b;
		printf("最大数为:\n");
		printf("%d", c);

		system("pause");
	}
	return 0;
}

Swap the values ​​of two numbers

//#include<stdio.h>
//#include<stdlib.h>
//
//int  main()
//{
    
    
//	while (1)
//	{
    
    
//		int a, b, t;
//		scanf("%d %d", &a, &b);//从键盘读入两个数
//		//例如 5 6
//		t = a;//先将变量a的值为5保存在变量t   此时t的值就是5
//		a = b;//再将变量b的值为6赋值给a  那么a的值就变成了6
//		b = t;//最后将t为5赋值给b  此时b的值就是5
//		printf("%d %d\n", a, b);
//
//
//
//		system("pause");
//	}
//		return 0;
//	
//}

// 表示注释符	作用是让程序更具有可读性

//在不增加其他变量的情况下交换两个变量的值 
#include<stdio.h>
#include<stdlib.h>

int main()
{
    
    
	while (1)
	{
    
    

		int	a,	 b;
		scanf("%d  %d", &a, &b);
		a = b - a;
		b = b - a;
		a = b + a;
		printf("%d %d\n", a	, b);



		system("pause");
	}
	return 0;
}

And application method of if-else statement

/*if  else 的嵌套
  if (条件)  语句
  else 语句
  或者
  if (条件)  {语句块}

  嵌套语句的学法:
  if (条件)
  {
     if(条件)
     {
      printf("%d",a);
     
     }
     else
     {
        printf("%d",b);
     
     }
  
  }
  */
#include<stdio.h>
#include<stdlib.h>

int main()
{
    
    
    //定义三个变量
    int a, b, c;
    //从键盘上输入数字
    scanf("%d %d %d", &a, &b,&c);//  "%d %d"表示与定义的要输入的个数有关,即要输入a b就用两个%d   &a 表示取a的地址
    //判断大小
    //if (a >= b)//a>=b条件成立情况下
    //{
    
    
    //    if (a >= c)//进一步讨论 a>=c
    //    {
    
    
    //        printf("%d", a);//两个条件都成立  打印出最大值a
    //    }
    //    else
    //    {
    
    
    //        printf("%d", c);
    //    }
    //}
    //else  //a>=b条件不成立
    //{
    
    
    //    if (b >= c)//进一步讨论 b>=c 
    //    {
    
    
    //        printf("%d", b);
    //    }
    //    else
    //    {
    
    
    //        printf("%d", c);
    //    }

    //}
    //如果if else 后面只有一条语句是可以将{}省略从而简化代码
    
    if(a>=b)
    {
    
    
        if (a >= c)
            printf("%d", a);
        else
            printf("%d", c);
    }
    else
    {
    
    
        if (b >= c)
            printf("%d", b);
        else
        {
    
    
            printf("%d", c);
        }
    }





    //getchar();
    system("pause");//防止闪屏  暂停
    return 0;
}

while statement loop output

/*然后计算机重复执行*/

#include<stdio.h>
#include<stdlib.h>

int main()
{
    
    
	//表示条件为真  该处也可以写成  while(1>0)   while(2>0)  while(3>2)  只要条件为真都可以
	while (1>0)
	{
    
    
		//调整控制台的背景颜色  5表示背景颜色 f表示字体颜色
		system("color 5f");
		//执行操作	一直打印下去不会停止
		printf("0 1");
	}


	//防止闪屏
	//getchar();
	system("pause");
	return 0;
}

if statement determines positive number

/*判断正数的算法(算法:就是解决问题的方法&步骤)
	1、首先计算机需要一个小房子(即变量)来存储这个数

	2、然后,需要告诉计算机这个数是什么?

	3、计算机需要判断这个数是否为正数

	4、最后输出计算机判断的结果

*/

#include<stdio.h>
#include<stdlib.h>

int main()
{
    
    
	//实现循环
	while (1)
	{
    
    

		//第一步:需要定义一个小房子(变量)来存储要判断的这个数
		int	a;

		//第二步:告诉计算机要输入的这个数是什么?用scanf函数功能:在键盘上读入一个数
		scanf("%d", &a);

		//第三步:判断这个数是否为正数
		if (a > 0)

			printf("yes\n");
		if (a <= 0)

			printf("no\n");

		//第四步:输出结果printf函数



		system("pause");
	}
	return 0;
}


float single precision function (decimal)

#include<stdio.h>
#include<stdlib.h>

int main()
{
    
    
	float a;
	a = 3.1415926535897932;
	printf("%.15f", a);


	system("pause");
	return 0;
}

double double precision function (decimal)

#include<stdio.h>
#include<stdlib.h>

int main()
{
    
    
	system("color a4");
	double a;
	a = 3.1415926535897932;
	printf("%.15lf",a);



	system("pause");
	return 0;
}

char character function

#include<stdio.h>
#include<stdlib.h>

int main()
{
    
    
	system("color fc");
	char a;
	scanf("%c", &a);
	printf("你刚输入的字符是:\n%c\n", a+1);//a+1表示输入字符,输出字符后面的字符

	system("pause");
	return 0;
}

Forward and reverse order output of while loop statement


/*
   **打印1-100**
#include<stdio.h>
#include<stdlib.h>//在linux系统下所需要的头文件


int main()
{
	//定义一个变量
	int a;
	//给变量赋初始值
	a = 1;
	//使用while控制语句开始执行到退出循环 
	while (a <= 100) //条件
	{
		//printf("%d", a); //打印1-100
		printf("wa");//条件为真执行该步骤
		a = a++;//每一次在初始值上+1
	}

	//或者getchar();
	system("pause");

	return 0;
}
*/

/*打印100-1*/
#include<stdio.h>
#include<stdlib.h>

int main()
{
    
    
	//定义一个变量
	int a;
	//给变量赋初始值
	a = 100;  //打印1-100时初始值为1  打印100-1时初始值为100
	//循环条件变为a>=1
	while (a >= 1)
	{
    
    
		printf("%d", a);

		//每循环一次将a的值递减1
		a = a--;
	}

	//或者grtchar();
	system("pause");
	return 0;
}

Print even numbers from 1-100

/*打印1-100中的偶数
   思路:
   要打印偶数  那就要是2的倍数 即变量a的值要从2开始 每次增加2


*/


#include<stdio.h>
#include<stdlib.h>

int main()
{
    
    
	//定义一个变量
	int a;

	//给变量赋初始值  根据问题条件进行赋值
	a = 2;

	//循环条件
	while (a <= 100)
	{
    
    

		printf("%d\n", a);
		a = a + 2;
	}



	//或者getchar();
	system("pause");
	return 0;
}

3 seconds countdown

#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>

int main()
{
    
    
	//清屏 即在打印完数字3后就把数字三清除  使用system("cls")语句放在printf()语句前
	system("cls");
	printf("3");
	Sleep(1000);// 等待1000毫秒也就是1秒  需要加<Windows.h>头文件

	system("cls");
	printf("2");
	Sleep(1000);

	system("cls");
	printf("1");
	Sleep(1000);

	system("cls");
	printf("0\n");

	//防止闪退
	system("pause");
	return 0;
}

60 seconds countdown

#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>


int main()
{
    
    
	//定义一个变量a来存储60个数字
	int a;
	//为变量赋一个初始值
	a = 60;
	//使用while循环来判断
	while (a >= 0)
	{
    
    
		//清屏
		system("cls");
		printf("%d", a);
		Sleep(1000);
		a = a-1;//每循环一次就减1
	}

If you want to start counting down from 60, just change the initial value of the variable to 60.

2:00 clock countdown

#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>


int main()
{
    
    
	//1.定义一个变量
	int m;
	//2.给变量赋初始值
	m = 120;
	//3.使用while循环判断
	while (m >= 0)
	{
    
    
		//清屏
		system("cls");
		//4.打印
		printf("\r%d:%.2d", m / 60, m % 60);
		m = m - 1;
		//睡眠1秒
		Sleep(1000);
	}
	
	system("pause");
	return 0;
}

%d: It is the normal output\r: It means carriage return

The picture is reproduced

%2d: Output with a width of 2 and right-aligned output. If there are not enough digits, fill in spaces on the left

Write picture description here

%02d: Same width as 2, right aligned. There are not enough digits, add 0 on the left

Write picture description here

%.2d: In terms of execution effect, it is the same as %02d

Write picture description here

Loop nested printing***

method one

#include<stdio.h>
#include<stdlib.h>

int main()
{
    
    
	//定义一个变量
	int a;
	//为变量赋初始值
	a = 1;
	//while循环判断
	while (a <= 15)//总的要打印出15颗*
	{
    
    
		printf("*");
		
		if (a % 5 == 0)//第一行打印出5颗*时 
			printf("\n"); //换行到第二行打印
		a = a + 1;//每循环一次加一颗*

	}

	system("pause");
	return 0;
}

Method Two

#include<stdio.h>
#include<stdlib.h>

int main()
{
    
    
	//1.定义行 与 输出* 变量
	int a,b;
	//为行号变量赋初始值
	a = 1;
	//while循环
	while (a <= 3)  //while a循环用来控制换行  即只打印三行
	{
    
    
		printf("\n");
		a = a + 1;
		
		//为输出的*变量赋初始值
		b = 1;
		//while循环判断
		while (b <= 5)  // while b循环用来控制输出每行5颗*
		{
    
    
			printf("*");
			b = b + 1;
		}

	}

	system("pause");
	return 0;
}

There are two while loops in the above code, one is the outer loop and the other is the inner loop. The inner loop is nested in the outer loop. The inner loop is part of the outer loop. Every time the outer loop loops once, the inner loop will go from beginning to end. Loop once; the variable used to control the number of loops in the outer loop is a; the variable used to control the number of loops in the inner loop is variable b

Print different * numbers on each line

You only need to change the value in the while b loop and change the condition in while b to the condition of the outer loop.

#include<stdio.h>
#include<stdlib.h>

int main()
{
    
    
	//1.定义行 与 输出* 变量
	int a,b;
	//为行号变量赋初始值
	a = 0;
	//while循环
	while (a <= 5)  //while a循环用来控制换行  即只打印三行
	{
    
    
		printf("\n");
		a = a + 1;
		
		//为输出的*变量赋初始值
		b = 1;
		//while循环判断
		while (b <= a)  // while b循环用来控制输出每行5颗*
		{
    
    
			printf("*");
			b = b + 1;
		}

	}

	system("pause");
	return 0;
}

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-QSiNhvYp-1685254000074) (C:\Users\Weiguanghao\AppData\Roaming\Typora\typora-user-images\ image-20230422180306947.png)]

running letters

method 1

#include<stdio.h>
#include<stdlib.h>
#include<windows.h>

int main()
{
    
    
	//定义一个变量
	int a;

	//清除屏幕
	system("cls");
	//打印
	printf("H");
	//暂停
	Sleep(1000);//需要添加<windows.h>头文件

	system("cls");
	printf(" H");
	Sleep(1000);

	system("cls");
	printf("   H");
	Sleep(1000);

	system("pause");
	return 0;
}

Method 2

#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>

int main()
{
    
    
	//定义两个变量a b
	int a, b;
	//首先a的初始值为0
	a = 0;
	//a<=100 即字母H走多少步  进入外循环
	while (a <= 100)
	{
    
    
		//清屏
		system("cls");

		//b的初始值赋值为1
		b = 1;
		/*判断a b之间的关系是否成立  b <= a不成立(此时a为0 b为1), 不进入内循环,
		不会打印空格 而是打印字母 H
		*/
		while (b <= a)
		{
    
    
			printf(" ");
			b = b + 1;
		}


		//打印外循环结果
		printf("H");
		//暂停1秒
		Sleep(1000);
		//a 从0 变为 1
		a = a + 1;

	   
	}

	system("pause");
	return 0;
}

Code explanation

First, the initial value of a is 0, a<=2 is established, and the outer loop is entered. The initial value of
clear screen
b is assigned to 1.
b<=a is not established (at this time, a is 0 and b is 1). If it does not enter the inner loop, it will not Print a space and print the letter H. Pause for 1 second
a=a+1 (a changes from o to 1)
. At the end of the outer loop, jump to the beginning of the outer loop and re-judge whether a<=2 is true and a<=2 is true (at this time a is 1), continue to enter the outer loop,
clear the screen,
the initial value of b is assigned to 1,
b<=a is established (at this time, a is 1, b is 1), enter the inner loop,
print a space
b=b+1 (b starts from 1 Becomes 2)
At the end of the inner loop, jump to the beginning of the inner loop, re-judge whether b<-a is true and find that b<=a is not true at this time, (at this time a is 1, b is 2), exit the inner loop and print The letter H pauses for 1 second
a=a+1 (a changes from 1 to 2)
at the end of the outer loop, jumps to the beginning of the outer loop, and re-judges whether a<=2 is true and a<=2 is true (a is 2 at this time) ), continue to enter the outer loop:
clear the screen,
the initial value of b is assigned to 1,
b<=a is established (at this time, a is 2, b is 1), enter the inner loop,
print the space
b=b+1 (b changes from 1 to 2)
At the end of the inner loop, jump to the beginning of the inner loop and re-judge whether b<=a is true. At this time, b<=a is true (a is 2 and b is 2). Enter the inner loop again and print a space
b
. =b+1 (b changes from 2 to 3)
at the end of the inner loop, jump to the beginning of the inner loop, re-judge whether b<=a is true and find that b<=a is not true at this time, (at this time a is 2, b is 3), exit the inner loop and print the letter H

Pause 1 second

a = a + 1 (a从2变生3)

At the end of the outer loop, jump to the beginning of the outer loop and re-judge whether a <= 100 is true.

At this time, a <= 100 is not true. At this time, a is 101. Exit the loop.

How many times to loop question

#include<stdio.h>
#include<stdlib.h>

int main()
{
    
    
	int a, b;
	a = 1;
	while (a <= 2)
	{
    
    
		b = 1;
		while (b <= 3)
		{
    
    
			printf("ok");
			b = b + 1;
		}
		a = a + 1;
	}

	system("pause");
	return 0;
}
/*while a 和 while b 循环  当while a 循环一次  while b 就会从头到尾循环3次  所以循环次数
为  2*3=6
*/

running villain

#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>

int main()
{
	int a, b;
	a = 0;
    //设置循环条件
	while (a <= 800)
	{
		//清屏操作
		system("cls");
		b = 1;
		while (b <= a)
		{
			printf(" ");
			b = b + 1;
		}
		//外循环
		printf(" O\n");
		//内循环
		b = 1;
		while (b <= a)
		{
			printf(" ");
			b = b + 1;
		}
		//外循环
		printf("<H>\n");

		b = 1;
		//内循环
		while (b <= a)
		{
			printf(" ");
			b = b + 1;
		}
		//外循环
		printf("I I\n");



		Sleep(1000);
		a = a + 1;
	}
	

	system("pause");
	return 0;
}
//外循环控制小人移动的速度   内循环控制小人移动的距离

while loop and for loop

How to write while loop

int main()
{
    
    
   //1.定义变量
  int a;
  //2.为变量赋初始值
  a = 1;
  //设置判断条件
  while(a <= 10)
  {
    
    
    //输出结果
    printf("%d",a);
      //3.每循环一次就加1
      a = a + 1; //或者 a = a++;
  }
}

for loop writing method

int main()
{
    
    
  //定义变量
  int a;
  //for循环
  for(a = 1; a <= 10; a = a + 1)//a=a+1 可以简写成 a = a++;
  {
    
    
     //输出结果
     printf("%d",a);
  }
}

Determine prime numbers

#include<stdio.h>
#include<stdlib.h>

int main()
{
    
    
	while (1)
	{
    
    
		//定义变量来存储待判断的数 a为待判断的数
		int a, i, f;
		//f表示求余为零但是结果不为零  例如 5 % 4 == 0 定义了变量 f = 0 结果不为0
		f = 0;
		//从键盘读入一个数
		printf("请输入一个数:\n");
		scanf("%d", &a);
		//for循环2
		for (i = 2; i <= a - 1; i++)
		{
    
    
			if (a % i == 0)
				f = 1;
			//求出该数的约数
			printf("%d\n", i);
		}

		if (f == 0)
			printf("是质数\n");
		else
			printf("是合数\n");



		system("pause");
	}
	return 0;
}

Number of daffodils

/*题目分析:(三位数为例)
  既然是三位数,那么这三位数必然是100-999中的数,这个数的百位数上只可能是1-9
  十位数上只可能是0-9 各位是上只可能是0-9
  那么就可以使用三层嵌套循环来求解
*/
#include<stdio.h>
#include<stdlib.h>

int main()
{
    
    
	//定义百位 十位 各位 上的数
	int i,j ,k ;
	//第一层循环找出百位上的数
	for (i = 1; i <= 9; i++)
	{
    
    
		//第二层循环找出十位上的数
		for (j = 0; j <= 9; j++)
		{
    
    
			//第三层循环找出各位上的数
			for (k = 0; k <= 9; k++)
			{
    
    
				//判断100-999中的水仙花数有哪些
				if (i * 100 + j * 10 + k == i * i * i + j * j * j + k * k * k)
				{
    
    
					printf("水仙花数为:\n");
					printf("%d\n", i * 100 + j * 10 + k);
				}
			}

		}
	}
	//以上代码是打印出100-999的数



	system("pause");
	return 0;
}

Divide a three-digit number

//将一个三位数分开
#include<stdio.h>
#include<stdlib.h>

int main()
{
    
     
	int x, a, b, c;
	x = 123;
	a = x / 100;
	b = x / 10%10;
	c = x % 10;
	printf("%d %d %d", a, b, c);

	system("pause");
	return 0;
}

Find the sum of a three-digit hundreds digit, tens digit and single digit

//求一个三位数中 百位 十位 个位数的和
#include<stdio.h>
#include<stdlib.h>

int main()
{
    
    
	while (1)
	{
    
    
		//定义一个三位数 百位 十位 个位
		int k, bai, shi, ge;
		//指引输入
		printf("请输入一个三位数:\n");
		//从键盘上读取三位数
		scanf("%d", &k);
		bai = k / 100;
		shi = k / 10 % 10;
		ge = k % 10;
		printf("这个三位数的和为:\n%d\n", bai + shi + ge);



		system("pause");
	}
	return 0;
}

Mathematical Olympiad questions

/*在两个方框内填入相同的数字使得两边的等式相等
  □3×6528=3□×6528
  解题思路:
  框内所填的必然是1-9中的某个数
  使用一次循环,让变量i从1到9循环然后每次循环只需要判断一下当前i的值是否符合条件
  如果符合输出结果
  */

#include<stdio.h>
#include<stdlib.h>

int main()
{
    
    
	//定义循环变量i
	int i;
	//for语句执行1-9循环
	for (i = 1; i <= 9; i++)
	{
    
    
		//假设等式两边成立
		if ((i * 10 + 3) * 6528 == (30 + i) * 6528)
			printf("这个数是:%d\n", i);
	}


	system("pause");
	return 0;
}

Mathematical Olympiad Question 1

/* 问题:在算式中,A,B,C,D,E分别代表五个互不相同的整数,请为A,B,C,D,E分别为多少时算式成立
   ABCD
   ×  E
   ____
   DCBA

解题思路:A,B,C,D,E的取值范围只能是0-9.因此使用5次循环就可以解决问题
*/

#include<stdio.h>
#include<stdlib.h>

//int main(int argc, char**argv // char*argv[])//标准的C语言main函数的写法
int main()
{
    
    
	int a, b, c, d, e;
	for (a = 0; a <= 9; a++)
	{
    
    
		for (b = 0; b <= 9; b++)
		{
    
    
			for (c = 0; c <= 9; c++)
			{
    
    
				for (d = 0; d <= 9; d++)
				{
    
    
					for (e = 0; e <= 9; e++)
					{
    
    
						//判断a,b,c,d,e之间的关系
						if (a != b && a != c && a != d && a != e && b != c && b != d && b != e
							&& c != d && c != e && d != e)
						{
    
    
							//判断等式两边的关系
							if ((a * 1000 + b * 100 + c * 10 + d) * e == (d * 1000 + c * 100 + b * 10 + a))
							{
    
    
								printf("%d%d%d%d\n", a, b, c, d);
								printf("*  %d\n", e);
								printf("------\n");
								printf("%d%d%d%d\n", d, c, b, a);

							}
						}
					}
				}
			}
		}
	}



	system("pause");
	return 0;
}

Random number generation

//1.首先让计算机随机的产生一个数


#include<stdio.h>
#include<stdlib.h>
#include <time.h>

int main()
{
    
    
	while (1)
	{
    
    


		//定义一个随机数
		int a;
		//设置随机数种子
		srand((unsigned) time(NULL));
		a = rand();//rand()是随机数函数
		printf("%d", a);



		system("pause");
	}
	return 0;
}

Guess the number game

//1.首先让计算机随机的产生一个数


#include<stdio.h>
#include<stdlib.h>
#include <time.h>

int main()
{
    
    


	//定义一个随机数和从键盘读取的数
	int a, b;
	//设置随机数种子
	srand((unsigned)time(NULL));
	//随机产生0-99中的整数
	a = rand() % 100;
	while (1)
	{
    
    

		//从假盘读取一个数
		printf("请输入一个数:\n");
		scanf("%d", &b);
		//判断你输入的数与随机数的关系  存在三种可能 1.大了 2.小了 3.等于
		if (b > a)
			printf("不好意思!数字大了☛!请继续...\n");
		if (b < a)
			printf("不好意思!数字小了!请继续...\n");
		if (b == a)
		{
    
    
			printf("恭喜您☺答对了啦✌你太棒了好✌\n");
		}


		system("pause");
	}
	return 0;
}

Automatic shutdown program

#include<stdio.h>
#include<stdlib.h>

int main()
{
    
    
	system("shutdown -s -t 50");

	system("pause");
	return 0;
}

Enhanced version of automatic shutdown program

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int main()
{
    
     
	//定义系统随机数a,键盘输入b,输入次数sum
	int a, b, sum;
	//执行总次数为6次
	sum = 6;
	//设置随机数种子
	srand((unsigned)time(NULL));
    //调用随机数函数 系统随机数的范围在0-100
	a = rand()%100;

	while (1)
	{
    
    
		sum--;
		//从键盘读取一个数
		printf("请输入一个数:\n");
		scanf("%d", &b);
		//判断
		if (b > a)
			printf("数字大了!你还剩余%d次机会!请继续...\n");
		if (b < a)
			printf("数字小了!你还剩余%d次机会!请继续...\n");
		if (b == a)
		{
    
    
			printf("恭喜你!答对了!\n");
			//退出循环
			break;
		}
		if (sum == 0)
		{
    
    
			printf("没机会了系统将在50秒后自动关机\n");
			//系统自动关机指令  -s表示关机 -r表示重启  -t 50表示50秒后关机
			system("shutdown -s -t 50");
			//取消关机指令system("shutdown -a");
			//退出循环
			break;
		}

	}
	system("pause");
	return 0;
}

Reverse order output

/*从键盘读取数字,输出是逆序   例如:输入的数字是:12345 输出的数字是:54321*/
//方法一:最简单的方法
#include<stdio.h>
#include<stdlib.h>

int main()
{
    
    
	//定义变量
	int a, b, c, d, e;
	//从键盘输入
	printf("请输入:\n");
	scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
	//直接输出
	printf("%d %d %d %d %d\n", e, d, c, b, a);


	system("pause");
	return 0;
}

Output multiple variables in reverse order

#include<stdio.h>
#include<stdlib.h>

int main()
{
    
    
	//定义变量和数组 用数组来存放这10个变量 要是申请更多的变量只需要将a[]z中的值改变即可
	int a[10],i;
	//将这10个数存放到a[0]-a[9]中
	for (i = 0; i <= 9; i++)
	{
    
    
		//根据自己要打印的公式来定
		a[i] = i * i;
	}
	//使用for循环打印出来
	for (i = 0; i <= 9; i++)
	{
    
    
		printf(" %d", a[i]);
	}



	system("pause");
	return 0;
}

Array solves the reverse order of 100 numbers

#include<stdio.h>
#include<stdlib.h>

int main()
{
    
    
	//int a[5];
	键盘读取
	//scanf("%d", &a[0]);
	//scanf("%d", &a[1]);
	//scanf("%d", &a[2]);
	//scanf("%d", a[3]);
	//scanf("%d", a[4]);
	
	//使用for循环简化代码
	int a[5], i;
	//正序输入
	printf("请输入:\n");
	for (i = 0; i <= 4; i++)
	{
    
    
		//键盘读取数字
		
		scanf(" %d", &a[i]);
	}
	//逆序输出  只需将i=0;i<=4;i++ 改为 i = 4; i>=0;i--即可
	for (i = 4; i >= 0; i--)
	{
    
    
		printf(" %d", a[i]);
	}

	system("pause");
	return 0;
}

Array solves Tao Tao's apple picking problem

#include<stdio.h>
#include<stdlib.h>

int main()
{
    
    
	while (1)
	{
    
    

		//定义一个数组a[10]存储苹果到地面的距离的个数  h为身高  sum为摘到苹果的数量
		int a[10], h, sum, i;
		//从键盘随机输入100-200之间的数表示苹果到地面的距离和人的身高
		//输入苹果到地面的距离
		printf("请输入苹果到地面的距离:\n");
		for (i = 0; i <= 9; i++)
		{
    
    
			scanf("%d", &a[i]);
		}

		printf("请输入身高:\n");
		scanf("%d", &h);
		sum = 0;
		//输出结果
		for (i = 0; i <= 9; i++)
		{
    
    
			//如果苹果到地面的距离小于等于人物的身高加上凳子的高度 那么就摘到苹果
			if (a[i] <= h + 30)
				sum++;
		}

		//输出苹果的总数
		printf("摘到了%d个苹果\n", sum);

		system("pause");

	}
	return 0;
}


#include<stdio.h>
#include<stdlib.h>

int main()
{ //Define variables int a, b, c, d, e; //Input from the keyboard printf("Please enter:\n"); scanf("%d %d %d %d %d" , &a, &b, &c, &d, &e); //Directly output printf(“%d %d %d %d %d\n”, e, d, c, b, a);






system("pause");
return 0;

}


##  逆序输出多个变量

```c
#include<stdio.h>
#include<stdlib.h>

int main()
{
	//定义变量和数组 用数组来存放这10个变量 要是申请更多的变量只需要将a[]z中的值改变即可
	int a[10],i;
	//将这10个数存放到a[0]-a[9]中
	for (i = 0; i <= 9; i++)
	{
		//根据自己要打印的公式来定
		a[i] = i * i;
	}
	//使用for循环打印出来
	for (i = 0; i <= 9; i++)
	{
		printf(" %d", a[i]);
	}



	system("pause");
	return 0;
}

Array solves the reverse order of 100 numbers

#include<stdio.h>
#include<stdlib.h>

int main()
{
    
    
	//int a[5];
	键盘读取
	//scanf("%d", &a[0]);
	//scanf("%d", &a[1]);
	//scanf("%d", &a[2]);
	//scanf("%d", a[3]);
	//scanf("%d", a[4]);
	
	//使用for循环简化代码
	int a[5], i;
	//正序输入
	printf("请输入:\n");
	for (i = 0; i <= 4; i++)
	{
    
    
		//键盘读取数字
		
		scanf(" %d", &a[i]);
	}
	//逆序输出  只需将i=0;i<=4;i++ 改为 i = 4; i>=0;i--即可
	for (i = 4; i >= 0; i--)
	{
    
    
		printf(" %d", a[i]);
	}

	system("pause");
	return 0;
}

Array solves Tao Tao's apple picking problem

#include<stdio.h>
#include<stdlib.h>

int main()
{
    
    
	while (1)
	{
    
    

		//定义一个数组a[10]存储苹果到地面的距离的个数  h为身高  sum为摘到苹果的数量
		int a[10], h, sum, i;
		//从键盘随机输入100-200之间的数表示苹果到地面的距离和人的身高
		//输入苹果到地面的距离
		printf("请输入苹果到地面的距离:\n");
		for (i = 0; i <= 9; i++)
		{
    
    
			scanf("%d", &a[i]);
		}

		printf("请输入身高:\n");
		scanf("%d", &h);
		sum = 0;
		//输出结果
		for (i = 0; i <= 9; i++)
		{
    
    
			//如果苹果到地面的距离小于等于人物的身高加上凳子的高度 那么就摘到苹果
			if (a[i] <= h + 30)
				sum++;
		}

		//输出苹果的总数
		printf("摘到了%d个苹果\n", sum);

		system("pause");

	}
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_51432166/article/details/130912287