昨天又做了一些題目,權是爲了練手,蠻有意思的!

//測試這個程序的運行結果爲 7 28
#include <iostream>
using namespace std;

int main() {
	int x = 2, y = 0, z;
	x += 3 + 2; printf("%d", x);
	x *= y = z = 4; printf("%d", x);
}
//三元運算符的一個典型例子
#include <iostream>
using namespace std;

int main() {
	int x = 0;
	cout << (x <= 0 ? (x == 0 ? 0 : 1 ): -1) << endl;
	cout << (x <= 0 ? x == 0 ? 0 : 1 : -1) << endl << endl;
	x = -100;
	cout << (x <= 0 ? (x == 0 ? 0 : 1 ): -1) << endl;
	cout << (x <= 0 ? x == 0 ? 0 : 1 : -1) << endl << endl;
	x = 100;
	cout << (x <= 0 ? (x == 0 ? 0 : 1 ): -1) << endl;
	cout << (x <= 0 ? x == 0 ? 0 : 1 : -1) << endl << endl;
}
//據說是用泰勒級數求出自然常數(歐拉常數)e的近似值
// e = 1 + 1/1! + 1/2! + 1/3! + ...
#include <stdio.h>
#define EPS 1e-8

int main(void) {
	double e = 0, fact = 1;
	long n = 0;
	do {
		n++;
		fact *= n;
		e += 1 / fact;
	} while (1 / fact >= EPS);
	e++;
	printf("e = %0.8f\n", e);
	return 0;
}
//挑出每一位的階乘之和等於該數的數, 這個沒法演算似乎有問題。
#include <iostream>
using namespace std;

unsigned int fact(unsigned int n) {
	unsigned int fact = 1;
	unsigned int i = 1;
	if(!n) {
		return 0;
	}
	while(i <= n) {
		fact *= i;
		i++;
	}
	return fact;
}

unsigned int _fact(unsigned int n) {
	switch(n) {
		case 0:
			return 0;
		case 1:
			return 1;
		case 2:
			return 2;
		case 3:
			return 6;
		case 4:
			return 24;
		case 5:
			return 120;
		case 6:
			return 720;
		case 7:
			return 5040;
		case 8:
			return 40320;
		case 9:
			return 362880;
		default:
			return 0;
	}
}

unsigned int sumoffact(unsigned int n) {
	unsigned int sum = 0;
	unsigned int a[0xff], i = 0, j = 0;
	while(n) {
		a[i++] = n % 10;
		n /= 10;
	}
	while(j < i) {
		sum += _fact(a[j++]);
	}
	return sum;
}

int main() {
	for(int i = 0; i < 10; i++) {
		cout << fact(i) << endl;
	}
	for(int i = 1; i < 20000000; i++) {
		if(i == sumoffact(i)) {
			cout << i << "  ";
		}
	}
}

 

//挑出即是迴文又是素數的數
#include <iostream>
using namespace std;

int is_prime(unsigned int n) {
	for(int i = 2; i < n; i++) {
		if(!(n % i)) {
			return 0;
		}
	}
	return 1;
}

unsigned int _strlen(char* s) {
	unsigned int len = 0;
	while(*s++) {
		len++;
	}
	return len;
}

int is_palindrome(unsigned int n) {
	char s[0xff], *sp = s;
	unsigned int len;
	while(n) {
		*sp++ = n % 10 + '0';
		n /= 10;
	}
	*sp = '\0';
	len = _strlen(s);
	for(int i = 0, j = len - 1; (i < len / 2) && (j >= len / 2); i++ , j--) {
		if(s[i] != s[j]) {
			return 0;
		}
	} 
	return 1;
}

int main() {
	for(int i = 1; i < 100000000; i++) {
		if(is_palindrome(i) && is_prime(i)) {
			printf("%d  ", i);
		}
	}
}

 

//字符串中挑出數字並反向輸出
#include <iostream>
using namespace std;

unsigned int _strlen(const char* s) {
	unsigned int len = 0;
	while(*s++) {
		len++;
	}
	return len;
}

int main() {
	char s[0xff], *sp = s, st[0xff], *stp = st, _st[0xff];
	cin >> s;
	while(*sp) {
		if(*sp >= '0' && *sp <= '9') {
			*stp++ = *sp++;
		} else {
			sp++;
		}
	}
	*stp = '\0';
	stp = st;
	for(stp += _strlen(st) - 1, sp = _st; stp >= st; stp--, sp++) {
		*sp = *stp;
	}
	*sp = '\0';
	cout << st << endl;
	cout << _st << endl;
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章