關於十二生肖的數學發現(C++)


把數字看成是年份,然后根據生肖賦值。這里把鼠年賦值為1,牛年賦值為2,虎年賦值為3,兔年賦值為4,龍年賦值為5,蛇年賦值為6,馬年賦值為7,羊年賦值為8,猴年賦值為9,雞年賦值為10,狗年賦值為11,豬年賦值為12。比如數字1,我們把它看成是公元1年,公元1年是雞年,賦值10,用1和10做差得絕對值,答案為9。

所有的答案都會落到0,3,6,9這四個數中(如果數字的值大於12,做差的時候則把數字的每一位相加,直到數字的大小歸到1至12之中,再與原數字對應的生效所賦的值進行做差)。

代碼如下:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cmath>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int s(int n) {
	int sum = 0, t;

	while (n != 0) // 當 n 不等於 0 的時候就執行循環體或者用 n>0 作為條件
	{
		t = n % 10; // n 對 10 求余算出個位數 t
		sum += t; // sum = sum + t // 把求出的 t 值累加到 sum 中
		n = n / 10; // n 除以 10 去除個位上的值
	}
	return sum; // 輸出累加的值
} 
string zodiac(int n) {
	int a=4, t;
    t = (n - a)%12;
    if(n > a)
        switch(t){
            case 0: return "鼠年"; 
            case 1: return "牛年"; 
            case 2: return "虎年";
            case 3: return "兔年";
            case 4: return "龍年";
            case 5: return "蛇年";
            case 6: return "馬年";
            case 7: return "羊年";
            case 8: return "猴年";
            case 9: return "雞年";
            case 10: return "狗年";
            case 11: return "豬年";
            default: return 0;
        }
    else{
        t = -t;

        switch(t){
            case 0: return "鼠年"; 
            case 1: return "豬年"; 
            case 2: return "狗年";
            case 3: return "雞年";
            case 4: return "猴年";
            case 5: return "羊年";
            case 6: return "馬年";
            case 7: return "蛇年";
            case 8: return "龍年";
            case 9: return "兔年";
            case 10: return "虎年";
            case 11: return "牛年";
            default: return 0;
        }
    }
}
int nb(string year) {
	if(year == "鼠年") {
		return 1;
	}
	else if(year == "牛年"){
		return 2;
	} 
	else if(year == "虎年"){
		return 3;
	}
	else if(year == "兔年"){
		return 4;
	}
	else if(year == "龍年"){
		return 5;
	}
	else if(year == "蛇年"){
		return 6; 
	}
	else if(year == "馬年"){
		return 7;
	}
	else if(year == "羊年"){
		return 8;
	}
	else if(year == "猴年"){
		return 9;
	}
	else if(year == "雞年"){
		return 10;
	}
	else if(year == "狗年"){
		return 11;
	}
	else{
		return 12;
	}
}
int main(int argc, char** argv) {
	ofstream fout;
    fout.open("data.txt");//將fout對象和文件綁定起來()
    int temp;
    string year;
    int count;
    for(int i = 1;i <= 25000;++i) {
        temp = i;
    	while(temp > 12) {
    		temp = s(temp);
		}
		year = zodiac(i);
		count = nb(year);
		fout << temp << "   " << i << "   " << year << "   " << count << "   " << abs(temp - count) << endl;
	}
	return 0;
}

  

運行結果:

1 1 雞年 10 9

2 2 狗年 11 9

3 3 豬年 12 9

4 4 鼠年 1 3

5 5 牛年 2 3

6 6 虎年 3 3

7 7 兔年 4 3

8 8 龍年 5 3

9 9 蛇年 6 3

10 10 馬年 7 3

11 11 羊年 8 3

12 12 猴年 9 3

4 13 雞年 10 6

5 14 狗年 11 6

6 15 豬年 12 6

7 16 鼠年 1 6

8 17 牛年 2 6

9 18 虎年 3 6

10 19 兔年 4 6

2 20 龍年 5 3

3 21 蛇年 6 3

4 22 馬年 7 3

5 23 羊年 8 3

6 24 猴年 9 3

7 25 雞年 10 3

8 26 狗年 11 3

9 27 豬年 12 3

10 28 鼠年 1 9

11 29 牛年 2 9

3 30 虎年 3 0

4 31 兔年 4 0

5 32 龍年 5 0

6 33 蛇年 6 0

7 34 馬年 7 0

8 35 羊年 8 0

9 36 猴年 9 0

10 37 雞年 10 0

11 38 狗年 11 0

12 39 豬年 12 0

4 40 鼠年 1 3

5 41 牛年 2 3

6 42 虎年 3 3

7 43 兔年 4 3

8 44 龍年 5 3

9 45 蛇年 6 3

10 46 馬年 7 3

11 47 羊年 8 3

12 48 猴年 9 3

4 49 雞年 10 6

5 50 狗年 11 6

6 51 豬年 12 6

7 52 鼠年 1 6

8 53 牛年 2 6

9 54 虎年 3 6

10 55 兔年 4 6

11 56 龍年 5 6

12 57 蛇年 6 6

4 58 馬年 7 3

5 59 羊年 8 3

6 60 猴年 9 3

7 61 雞年 10 3

8 62 狗年 11 3

9 63 豬年 12 3

10 64 鼠年 1 9

11 65 牛年 2 9

12 66 虎年 3 9

4 67 兔年 4 0

5 68 龍年 5 0

6 69 蛇年 6 0

7 70 馬年 7 0

8 71 羊年 8 0

9 72 猴年 9 0

10 73 雞年 10 0

11 74 狗年 11 0

12 75 豬年 12 0

4 76 鼠年 1 3

5 77 牛年 2 3

6 78 虎年 3 3

7 79 兔年 4 3

8 80 龍年 5 3

9 81 蛇年 6 3

10 82 馬年 7 3

11 83 羊年 8 3

12 84 猴年 9 3

4 85 雞年 10 6

5 86 狗年 11 6

6 87 豬年 12 6

7 88 鼠年 1 6

8 89 牛年 2 6

9 90 虎年 3 6

10 91 兔年 4 6

11 92 龍年 5 6

12 93 蛇年 6 6

4 94 馬年 7 3

5 95 羊年 8 3

6 96 猴年 9 3

7 97 雞年 10 3

8 98 狗年 11 3


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM