C语言经典算法100例(一)

从网上找到很多C语言入门练习100题,本小白就决定来试试啦!

程序采用visual studio 2017,因为编程不熟练,就仅保证正确,不保证高效啦!

=======================================================================

【程序1】

题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

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

int main()
{
	int i,j,k,m;
	int s = 0;
	int S, N;
	int num[100][3] = {};

	for (i = 1; i < 5; i++) {
		for (j = 1; j < 5; j++) {
			for (k = 1; k < 5; k++) {
				num[s][0] = i;
				num[s][1] = j;
				num[s][2] = k;
				s = s + 1;
			}
		}
	}
	
	S = s;
	N = S;
	for (m = 0; m < S; m++) {
		if (num[m][0] == num[m][1] || num[m][0] == num[m][2] || num[m][1] == num[m][2]) {
			num[m][0] = 0;
			N = N - 1;
		}
	}

	printf("There are %d numbers and they are\n", N);
	for (m = 0; m < S; m++) {
		if (num[m][0] != 0) {
			printf("%d%d%d\n", num[m][0], num[m][1], num[m][2]);
		}
	}
	

	system("pause");
	return 0;
}

=======================================================================

【程序2】

题目:企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?

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

void main()
{
	double l, r;

	scanf_s("%lf", &l);

	l = l / 10000;

	if (l <= 0.0) {
		printf("Please input the right profit!");
	}
	else if(l > 0.0 & l <= 10.0){
		r = l * 0.1;
	}
	else if (l > 10.0 & l <= 20.0) {
		r = 10 * 0.1 + (l - 10) * 0.075;
	}
	else if (l > 20.0 & l <= 40.0) {
		r = 10 * 0.1 + 10 * 0.075 + (l - 20) * 0.05;
	}
	else if(l > 40.0 & l <= 60.0){
	    r = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + (l - 40) * 0.03;
	}
	else if (l > 60.0 & l <= 100.0) {
		r = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 40 * 0.03 + (l - 60) * 0.015;
	}
	else {
		r = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 40 * 0.03 + 40 * 0.015 + (l - 100) * 0.01;
	}

	printf("Your reward is %.2f", r * 10000);

	system("pause");
	return;
}

=======================================================================

【程序3】

题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?

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

void main() {
	int x, y;
	int n;

	for (x = 10; x < 1000; x++) {
		for (y = 13; y < 1000; y++) {
			n = 0;
			while (n < 1000) {
				if (n + 100 == x * x & n + 268 == y * y) {
					printf("%d %d %d\n", x, y, n);
					break;
				}
				else {
					n = n + 1;
				}
			}
		}
	}

	system("pause");
	return;
}

=======================================================================

【程序4】

题目:输入某年某月某日,判断这一天是这一年的第几天?

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

int main()
{
	int d, m, y;
	int month[12] = {};
	int n;
	int i;

	scanf_s("%d%d%d", &d, &m, &y);

	n = d;
	month[0] = month[2] = month[4] = month[6] = month[7] = month[9] = month[11] = 31;
	month[1] = 29;
	month[3] = month[5] = month[8] = month[10];

	if ((y % 4 == 0 && y % 100 != 0) ||  y % 400 == 0) {
		for (i = 0; i < m - 1; i++) {
			n = n + month[i];
		}
	}
	else {
		month[1] = 28;
		for (i = 0; i < m - 1; i++) {
			n = n + month[i];
		}
	}

	printf("This day is the %d day of the year.", n);

	system("pause");
	return 0;
}

=======================================================================

【程序7】

题目:输出特殊图案,请在c环境中运行,看一看,Very Beautiful!

这是什么鬼题目!题目不完整啊!哼!

=======================================================================

【程序8】

题目:输出9*9口诀。

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

int main()
{
	int i, j;

	for (i = 1; i < 10; i++) {
		for (j = 1; j <= i; j++) {
			printf("%d x %d = %d  ", i, j, i * j);
		}
		printf("\n");
	}
	system("pause");
	return 0;
}

=======================================================================

【程序9】

题目:要求输出国际象棋棋盘。

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


void main()
{
	int num[8][8] = {0};
	int i, j;
	
	for (i = 0; i < 8; i++) {
		for (j = 0; j < 8; j++) {
			if ((i + j) % 2 == 1) {
				num[i][j] = 1;
			}
		}
	}


	for (i = 0; i < 8; i++) {
		for (j = 0; j < 8; j++) {
			if (num[i][j] == 1) {
				printf("■");
			}
			else {
				printf("  ");
			}
		}
		printf("\n");
	}
	
	system("pause");
	return;
}

=======================================================================

【程序10】

题目:打印楼梯,同时在楼梯上方打印两个笑脸。

这又是什么!!!!!

=======================================================================

每次更新10个程序,近期会很快更新结束的。

不知道有没有人会看到这里,比心心❤

猜你喜欢

转载自blog.csdn.net/lxh1002/article/details/81031510