Parity check
Time Limit: 2000 ms Memory Limit: 524288 KiB
Problem Description
Fascinated with the computer games, Gabriel even forgets to study. Now she needs to finish her homework, and there is an easy problem:
f(n)=
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≤) in a single line.
Output
For each test case, output the answer of f(n)mod2.
Sample Input
2
Sample Output
1
1.数据太大,不可以用整形的来解决的时候,可以考虑字符串,这个已经说了好多次了。
2.还有就是,当循环次数太多,递归思路太多的时候,可以考虑,先把一些结果打印出来,观察观察是否有什么规律。
#include<stdio.h>#include<algorithm>
#include<string.h>
using namespace std;
char str[1005];
int main()
{
int n, i, j, k, sum;
while (scanf("%s", str) != EOF)
{
sum = 0;
for (int i = 0;str[i] != '\0';i++)
sum += (str[i] - '0');
if (sum % 3 == 0) printf("0\n");
else printf("1\n");
}
return 0;
}