第6章课后习题

语言: CN / TW / HK

为了把所有代码打在一个工程里所以我用#if #endif 装题目

以下下代码头文件有

#include<stdio.h>
#include<string.h>

1 储存并且打印 26字母

#if(1)
void main() {
	char XD[26] = {'a'};
	for (int i = 1; i < 26; i++) {
		XD[i] = 'a' + i;
	}
	printf("\n");
	for (int i = 0; i < 26; i++) {
		printf("  %c  ", XD[i]);
	}
}
#endif

2 嵌套打印

#if(1)
void main() {
	int i = 0;
	for (; i < 6; i++) {
		for (int j = 0; j < i; j++) {
			printf("$");
		}
		printf("\n");
	}
}
#endif

3 嵌套打印

# if 1
void main() {
	int i = 0;
	for (; i < 6; i++) {
		for (int j = 0; j < i; j++) {
			printf("%c",'a'+j);
		}
		printf("\n");
	}
}
#endif

4循环打印

#if(0)
void main() {
	int i = 0;
	int k = 0;
	for (; i < 6; i++) {
		for (int j = 0; j < i; j++) {
			printf("%c", 'A' + k);
			k++;
		}
		printf("\n");
	}
}
#endif

5循环打印

//6.5
#if(0)
void main() {
	int i = 1,k=0;
	char input,temp[20];
	scanf_s("%c", &input);
	for (; i <= input-'A'+1; i++) {
		for (int L = 0; L < 4 - i+1; L++)
		{
			printf(" ");
		}
		for (int j = 0; j < i; j++) {
			
			printf("%c", 'A'+j);
			temp[j] = 'A' + j;
			k = j;
		}
		for (int j = 0; j < k; j++) {
			printf("%c", temp[k - j-1]);
		}
		
		printf("\n");
	}
}
#endif

7倒叙打印

//6.7
#if (0)
void main() {
	char XD[10];
	scanf("%s",&XD);
	for (int i = 0; i < strlen(XD)+1; i++) {
		printf("%c", XD[strlen(XD) - i]);
	}
}
#endif 

8计算浮点

//6.8
#if(0)
void main() {
	float put1,put2;
	scanf("%f %f",& put1, &put2);
	if( (put1 >= 'A' && put1 <= 'z') || (put2 >= 'A' && put2 <= 'z')) {
		printf("重新输入\n");
		scanf("%f %f",& put1,& put2);
	}
	printf("%f", (put1 - put2) / (put1 * put2));
}

#endif 

9 8的函数版

//6.9
#if 0
float XD();
void main() {
	printf("%f",XD());

}

float XD() {
	float put1, put2;
	scanf("%f %f", &put1, &put2);
	for (; (put1 >= 'A' && put1 <= 'z') || (put2 >= 'A' && put2 <= 'z');) {
		printf("重新输入\n");
		scanf("%f %f", put1, put2);
	}
return ("%f", (put1 - put2) / (put1 * put2));
}


#endif 

10 计算程序

//6.10

#if(1)
void main() {
	while(1)
	{
		int max, min, sum = 0;
		printf("输入上限和下线:");
		scanf("%d %d", &max, &min);
		while (min == max) {
			goto XD;
		}
		for (int i = min; i < max; i++) {
			sum += i*i;
		}
		printf("和为 %d\n", sum);
	}
XD:
	printf("结束");
}

#endif

11倒数打印整数版

#if (0)
void main() {
	char XD[10];
	scanf("%s",&XD);
	for (int i = 0; i < strlen(XD)+1; i++) {
		printf("%c", XD[strlen(XD) - i]);
	}
}
#endif 

12计算题 算东西

//6.12
#if 0

void main() {
	int in,PX=1,sum=1;
	scanf("%d", &in);
	for (int i = 1; i <= in; i++) {
		sum += (1 / i) * PX;
		PX *= -1;
	}
	printf("\n\n%d", sum);
}

#endif 

收敛于2

13 两个数组 第二个存前面的每个个数幂

// 6.13
#if 0
	  
void main() {
	int a[8],sum=1;
	int l = 0;
	for (int i = 1; i <=8; i++) {
		for (int j=0; j < i; j++) {
			sum *= 2;
		}
		a[i - 1] = sum;
	}
	do
	{
		printf(" %d \n", a[l]);
		l++;
	} while (l<7);
}

#endif 

14两个数组 第二个存前面的每个个数和

//6.14

#if 1
void main() {
	double A[8], B[8];
	for (int i = 0; i < 8; i++) {
		printf(" A[%d]=  ", i);
		scanf("%lf", &A[i]);
	}
	B[0] = A[0];
	for (int i = 1; i < 8; i++) {
		B[i] = A[i] + B[i - 1];
	}
	for (int i = 0; i < 8; i++) {
		printf(" %.2lf ", A[i]);
	}
	printf("\n");
	for (int i = 0; i < 8; i++) {
		printf(" %.2lf ", B[i]);
	}
}
#endif // 1

15 倒叙打印

//6.15

#if 0

void main() {
	char XD[30];
	scanf("%s", &XD);
	for (int i = 0; i < strlen(XD) + 1; i++) {
		printf("%c", XD[strlen(XD) - i]);
	}
}

#endif // 1

16投资计算

//6.16
#if 0

void main() {
	float qian1=100, qian2=100,lixi2;
	int year = 0;
	lixi2 = 10;
	while (1) {
		qian1 =qian1*1.05;
		qian2 += lixi2;
		if (qian1 > qian2) {
			goto XD;
		}
		year++;
	}
XD:
	printf("%d年\n%.2f\n%.2f ",year, qian1, qian2);
}

#endif 

17什么时候取完钱

//6.17

#if 1

void main() {
	float qian = 100;
	int year=0;
	for (int i = 1;; i++) {
		qian = qian * 1.08 - 10;
		year++;
		if (qian<=0) {
			break;
		}
	}
	printf("\n\n%d", year);
}
#endif 

21年后取完钱

18 什么时候超过150个朋友

//6.18

#if 1

void main() {
	int person=5,year=0;
	while (1)
	{
		person = (person - 1) * 2;
		year++;
		if (person > 150) {
			printf("\n%d周超过150人", year);
			break;
		}
	}
}

#endif 

分享到: