一的个数:
#include
int ones(int num)
{
int one_num = 0;
while (num != 0)
{
if ((num & 0x0001) == 1)
{
one_num++;
}
num = num>>1;
}
return one_num;
}
int main()
{
int T, num, i;
int out[10];
scanf("%d", &T);
for (i = 0; i < T; i++)
{
scanf("%d", &num);
out[i] = ones(num);
}
for (i = 0; i < T; i++)
printf("%d\n", out[i]);
return 0;
}
calc the sum:
#include
void calc_sum(char *str, int *sum)
{
if (*str == '\0')
return;
*sum += *str - '0';
calc_sum(str+1, sum);
}
int main()
{
char num[1000001];
int T, i, sum = 0;
int out[10];
scanf("%d", &T);
for (i = 0; i < T; i++)
{
scanf("%s", num);
calc_sum(num, &sum);
out[i] = sum;
sum = 0;
}
for (i = 0; i < T; i++)
{
printf("%d\n", out[i]);
}
return 0;
菲波那契:
#include
int fib(int n)
{
if (n == 1 || n == 2)
return 1;
return fib(n-1)+fib(n-2);
}
int main()
{
int T, n, i, out[10];
scanf("%d", &T);
for (i = 0; i < T; i++)
{
scanf("%d", &n);
out[i] = fib(n);
}
for (i = 0; i < T; i++)
{
printf("%d\n", out[i]);
}
return 0;
}
函数求值:
#include
#include
double fun(int n, double x)
{
if (n == 1)
{
return sqrt(1+x);
}
return sqrt(n+fun(n-1,x));
}
int main()
{
int T, n, i;
double x, out[20];
scanf("%d", &T);
for (i = 0; i < T; i++)
{
scanf("%d %lf", &n, &x);
out[i] = fun(n, x);
}
for (i = 0; i < T; i++)
{
printf("%.3lf\n", out[i]);
}
return 0;
}
均在gcc下编译通过,并且运行符合示例结果~
望采纳,谢谢~
只会第一题,递归还没教
#include
int main(){
int T,i,n;
scanf("%d",&T);
while(T--){
int count=0;
scanf("%d",&n);
for(;;n=n/2){
if(n%2==1)
count++;
if(n==1)
break;
}
printf("%d\n",count);
}
return 0;
}