Description
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
如果用递归方法做的话,时间会超时
这个题是 f(n) 对 2 求余,判断余数是多少(就是判断奇偶性)
可以发现规律 n 从 0 开始 一直增加 ,对 2 求余结果为
0 1 1 0 1 1 0 1 1 0 1 1.....
就是 每三个一循环
由于 n(0≤n≤101000 ) n 太大了 用 int 实现不了 就想到用 char 型的数组 把输入的几位数当作是每一个字符
例如
25
sum=(2%3)+(5%3)=4
sum%3=1; b[1]=1 当 n=25 的时候 ,结果是奇数
代码如下
#include<stdio.h>
#include<math.h>
#include<string.h>
int main()
{
int n,i,m,sum=0;
char s[10001];
int b[]={0,1,1}; 有规律的
while(scanf("%s",s)!=EOF){ //把输入的数字当作字符串
m=strlen(s);
sum=0;
for(i=0;i<=m-1;i++)
sum+=(s[i]-'0')%3;// 计算每一位对 3 求余是多少
sum%=3;// 判断 sum 对应的是 b 数组里边的 第几个
printf("%d\n",b[sum]);
}
}