C言語古典アルゴリズム100例(一)

5844 ワード

ネット上でたくさんのC言語の入門練習100題を見つけて、本白は試してみることにしました!
プログラムはvisual studio 2017を採用しているが、プログラミングが熟練していないため、正確さだけを保証し、効率を保証しない.
=======================================================================
【手順1】
テーマ:1、2、3、4つの数字があって、何個の互いに異なってしかも重複する数字の3桁の数がないことを構成することができますか?どれくらいですか.

# include 
# include 

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); for (m = 0; m < S; m++) { if (num[m][0] != 0) { printf("%d%d%d
", 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 
# include 

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 
# include 

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
", x, y, n); break; } else { n = n + 1; } } } } system("pause"); return; }

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

【 4】

: , ?


# include 
# include 

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 
#include 

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("
"); } system("pause"); return 0; }

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

【 9】

: 。


#include 
#include 


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("
"); } system("pause"); return; } 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("
"); } system("pause"); return; }

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

【 10】

: , 。

!!!!!

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

10 , 。

, ❤