浙大版《C语言程序设计(第3版)》题目集_5-3~~~6-6

目录

5-3

5-1

5-2

5-3

5-4

5-5

5-6

5-7

6-1

6-2

6-3

6-4

6-5

6-6


5-3

void pyramid(int n) {
	for (int i = 1; i <= n; i++) {
		//打印空格
		for (int k = 0; k != n - i; k++) {
			cout << " ";
		}
		//打印数字
		for (int j = 0; j != i; j++) {
			cout << i << " ";

		}
		//换行
		cout << endl;
	}
}

5-1

int sign(int x) {
	if (x > 0) {
		return 1;
	}
	else if(x = 0){
		return 0;
	}
	else {
		return -1;
	}
}

5-2

int even(int n) {
	if (n % 2 == 0) {
		return 1;
	}
	else {
		return 0;
	}
	
}
int oddSum(int list[], int n) {
	int sum = 0;
	for (int i = 0; i < n; i++) {
		if (even(list[i]) == 0) {
			//cout << list[i] << " ";
			sum += list[i];
		}
	}
	return sum;
}

5-3

double dist(double x1, double y1, double x2, double y2) {
	double temp = 0;
	temp = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
	return temp;
}

5-4

int prime(int p) {
	if (p <=1)return 0;
	if (p == 2)return 1;
	for (int i = 2; i != p; i++) {
		if (p % i == 0)return 0;
	}
	return 1;
}
int primeSum(int m, int n) {
	int sum=0;
	for (int i = m; i <= n; i++) {
		if (prime(i) == 1)sum += i;
	}
	return sum;
}

5-5

int countDigit(int number, int digit) {
	int count = 0;
	if (number < 0)number *= (-1);
	while (number > 0) {
		if (number % 10 == digit)count++;
		number /= 10;
	}
	return count;
}

5-6

int flower(int p) {
	int sum = 0;
	int p1 = p;
	while (p > 0) {
		sum += ((p % 10)* (p % 10)* (p % 10));
		p /= 10;
	}
	if (sum == p1)return 1;
	return 0;
}
void flowerPrint(int m, int n) {
	for (int i = m; i <= n; i++) {
		if (flower(i) == 1)cout << i << endl;
	}
}

5-7

double cf(double p,int n) {//次方--ci-fang
	double sum = 1.0;
	for (int i = 0; i != n; i++) {
		sum *= p;
	}
	return sum;
}
int jc(int n) {//阶乘--jie-cheng
	if (n == 1)return 1;
	if (n ==0)return 1;
	return n * jc(n - 1);
}
double cosFun(double e, double x) {//数学上的泰勒公式很简单编程就好麻烦啊
	double res = 0;
	int i = 0;
	double tem;
	
	int count = 1;
	while (1) {
		if (count % 2 != 0) {
			tem = cf(x, i) / jc(i);
			
		}
		else {
			tem = (-1) * cf(x, i) / jc(i);
		}
		if (fabs(tem) < e)break;
		res += tem;
		i += 2;
		count++;
	}
	return res;
}

6-1

void cn_string(char a[]) {
	int i = 0;
	int letter = 0, blank = 0, digit = 0, other = 0;
	while (a[i] != '\0') {
		if (a[i] == ' ')blank++;
		else if ((a[i] >= '0') && (a[i] <= '9'))digit++;
		else if ((a[i] >= 'a') && (a[i] <= 'z'))letter++;
		else other++;
		i++;
	}
	cout << "letter is " << letter << endl
		<< "digit is " << digit << endl
		<< "blank is " << blank << endl
		<< "other is " << other << endl;
}

6-2

int fn(int a, int n) {
	int res = 0;
	int i = 0;
	while (1) {
		res += a;
		i++;
		if (i == n)break;
		res *= 10;
	}
	return res;
}
int sumA(int a, int n) {
	int res=0;
	for (int i = 0; i != n; i++) {
		res += fn(a, i + 1);
	}
	return res;
}

6-3

int wan(int n) {
	int yz[30];
	yz[0] = 1;
	int count = 1;
	for (int i = 2; i <= (n / 2); i++) {
		if (n % i == 0) {
			yz[count] = i;
			count++;
		}
	}
	int res = 0;
	for (int i = 0; i <= count - 1; i++) {
		res += yz[i];
	}
	if (res == n)return *yz;
	else return 0;




	
}

void showwan(int n) {
	int yz[30];
	yz[0] = 1;
	int count = 1;
	for (int i = 2; i <= (n / 2); i++) {
		if (n % i == 0) {
			yz[count] = i;
			count++;
		}
	}
	cout << n << " = ";
	for (int i = 0; i < count - 1; i++) {
		cout << yz[i] << "+";
	}
	cout << yz[count - 1] << endl;
}

6-4

int fb(int n) {
	if (n <= 0)return 0;
	else if (n == 1)return 1;
	else if (n == 2)return 1;
	else return fb(n - 1) + fb(n - 2);
}
void printFb(int m, int n) {
	int count = 1;
	int fbs[20]; int p = 0;
	while (1) {
		if (fb(count) < m) {
			count++; continue;
		}
		if (fb(count) > n)break;
		fbs[p] = fb(count);
		count++;
		p++;
	}
	for (int i = 0; i != p-1; i++) {
		cout << fbs[i] << " ";
	}
	cout << fbs[p - 1] << endl;
}

6-5

int prime(int n) {//素数
	if (n <= 1)return 0;
	if (n == 2)return 1;
	for (int i = 2; i != n; i++) {
		if (n % i == 0)return 0;
	}
	return 1;
}

void gdbh(int n) {//哥德巴赫猜想:任何一个大于6的偶数都可以分解为两个奇素数之和
	for (int i = 3; i < n; i++) {
		if ((i % 2 != 0) && (prime(i))) {
			int j = n - i;
			if ((j % 2 != 0) && (prime(j))) {
				cout << n << "=" << i << "+" << j << endl; break;
			}
		}
	}
}

6-6

int nx(int n) {
	if (n == 0)return 0;
	int sign = 1;
	if (n < 0) {
		sign = 0; n = n * (-1);
	}

	int res = 0;
	while (1) {
		res += (n % 10);
		if (n < 10)break;
		res *= 10;
		n /= 10;

	}

	if (sign == 1) {
		return res;
	}
	else {
		return res * (-1);
	}
}

 

你可能感兴趣的:(练习)