zcmu-2009: Parity check(找规律&&模运算)

2009: Parity check

Time Limit: 2 Sec  Memory Limit: 128 MB
Submit: 41  Solved: 28
[Submit][Status][Web Board]

Description

Fascinated with the computer games, Gabriel even forgets to study. Now she needs to finish her homework, and there is an easy problem:

She is required to calculate f(n) mod 2 for each given n. Can you help her?

Input

Multiple test cases. Each test case is an integer n(0≤n≤101000) in a single line.

Output

For each test case, output the answer of f(n)mod2.

Sample Input

2

Sample Output

1

【分析】找规律,会发现结果为0,1,1循环,然后及时取模防止数据过大溢出

※注意取余的时候,由于(a + b) % p = (a % p + b % p) % p ,而a*10%3==a%3,所以直接从低位开始一直取余

 

#include<bits/stdc++.h>
using namespace std;
char s[1005];
int main()
{
	while(~scanf("%s",s))
	{
		int len=strlen(s),sum=0;
		for(int i=len-1;i>=0;i--)
			sum=(sum+(s[i]-'0'))%3;
		if(sum==1||sum==2)cout<<"1"<<endl;
		else if(sum==0) cout<<"0"<<endl;
	}

	return 0;
} 

猜你喜欢

转载自blog.csdn.net/qq_38735931/article/details/81609898