关于十二生肖的数学发现(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
最新文章
- Drawing in Singapore
- lintcode最长回文子串(Manacher算法)
- 记一次【求n以内的素数个数】的优化记录
- .propertie文件注释
- Delphi-仿vb里的IIF函数
- z-index的理解 z-index 属性仅在节点的 position 属性为 relative, absolute 或者 fixed 时生效.
- 从UIImage的矩阵变换看矩阵运算的原理
- MongDb添加嵌套文档
- ServiceStack.Redis 使用链接池方法
- 打造颠覆你想象中的高性能,轻量级的webform框架---无刷新提交后台并返回参数(第五天)
- React入门教程
- 详解EBS接口开发之库存事务处理采购接收--补充
- iOS -- Effective Objective-C 阅读笔记 (7)
- ArrayList迭代器源码分析
- The server time zone value &#39;&#214;&#208;&#185;&#250;&#177;&#234;&#215;&#188;&#202;&#177;&#188;&#228;&#39; is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration
- linux command ------ dmesg
- spring框架学习(四)AOP思想
- android 流行框架的使用
- 一个高效的敏感词过滤方法(PHP)
- updatepanel 和 visibility 有一定冲突