ZOJ 十六届浙江省赛 I:Fibonacci in the Pocket

I:Fibonacci in the Pocket

思路:找规律的题,先打表,找规律,会很容易发现,每三项一循环,奇数 奇数 偶数,不过题目数据很大,直接用字符串数组,写一个大数取余运算即可。不过题目又来给定区间[m,n],先算出前m项是奇数还是偶数,再算出前n项是奇数还是偶数,这里有个小技巧,奇数-偶数=奇数,奇数-奇数=偶数,偶数-偶数=偶数,偶数-奇数=奇数。

#include<iostream>
#include<stdio.h>
#include<string>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<stack>
#include<set>
#include<vector>
using namespace std;
int a[10000];
//int fib(int n)
//{
//	if (n <= 1)return n;
//	if (a[n] != 0)return a[n];
//	return a[n] = fib(n - 1) + fib(n - 2);
//}
int xxx(string str, int mod)
{
	int ans = 0;
	unsigned int i;
	for (i = 0; i<str.length(); i++)
	{
		//cout << str[i] << " ";
		ans = ans * 10 + str[i] - '0';
		ans = ans%mod;
	}
	return ans;
}
int main()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		string s1;
		string s2;
		cin >> s1>>s2;
		int x1 = xxx(s1, 3);
		int x2 = xxx(s2, 3);
		if (x2 == 0)
		{
			if (x1 == 0)cout << "0\n";
			if (x1 == 1)cout << "0\n";
			if (x1 == 2)cout << "1\n";
		}
		else if (x2 == 2)
		{
			if (x1 == 0)cout << "0\n";
			if (x1 == 1)cout << "0\n";
			if (x1 == 2)cout << "1\n";
		}
		else
		{
			if (x1 == 0)cout << "1\n";
			if (x1 == 1)cout << "1\n";
			if (x1 == 2)cout << "0\n";
		}
	}
	return 0;
}

发布了10 篇原创文章 · 获赞 6 · 访问量 1798

猜你喜欢

转载自blog.csdn.net/Kris_Kris/article/details/89647399