25求奇数和
#include<stdio.h>
int main()
{
int n,total=0;
scanf("%d ", &n);
while (n > 0)
{
if(n%2!=0)
total += n;
scanf("%d", &n);
}
printf("%d", total);
return 0;
}
26求最大公约数和最小公倍数
#include<stdio.h>
int main()
{
int M,N;
scanf("%d %d", &M,&N);
int max, min,f;
f = M * N;
while(max = M % N)//辗转相除法求最大公约数
{
M = N;
N = max;
}
min = f / N;//注意f
//最小公倍数=两个数乘积除以最大公约数
printf("%d %d", N,min);//最后的N是最大公约数
return 0;
}
27兔子繁衍
#include <stdio.h>
int main() {
int N;
scanf("%d", &N);
int yes=0, total=1, temple;//yes表示生的,total表示总的,temple表示交换
int month = 1;
while (total < N) {
month++;
temple = yes;
yes = total;
total+=temple;
}
printf("%d", month);
return 0;
}
月份 | yes(生) | 不生 | total(总) |
1 | 0 | 1 | 1 |
2 | 0 | 1 | 1 |
3 | 1 | 1 | 2 |
4 | 1 | 2 | 3 |
5 | 1 | 3 | 4 |
6 | 3 | 4 | 7 |
7 | 3 | 6 | 10 |