C语言代码比较18ba/16

the kata:
题目描述:

In a small town the population is p0 = 1000 at the beginning of a
year. The population regularly increases by 2 percent per year and
moreover 50 new inhabitants per year come to live in the town. How
many years does the town need to see its population greater or equal
to p = 1200 inhabitants?

More generally given parameters:

p0, percent, aug (inhabitants coming or leaving each year), p (population to surpass)

the function nb_year should return n number of entire years needed to
get a population greater or equal to p.
aug is an integer, percent a positive or null number, p0 and p are positive integers (> 0)

我的程序1
Growth of a population;

在这里插入图片描述递归代替循环:
在这里插入图片描述

一个简单的递归
出口条件的p0>=p,
不符合则renturn 0+1;
再不符合则return 0+1+1;

大佬的程序:
在这里插入图片描述

ratio 比例
static inline int function();
static 是声明一个静态变量,作用范围是一个traslation unit,或称一个编译单元,就是一个源代码包括include ,macro…
inline 指显式提示编译器在调用函数时不要进行一般的调用,而是将函数的body代码加到调用处,差不多是include 的意思。

但从written differently:后的>= 这个符号就没看懂了。

但是出现了对数学公式的运用,看起来很nb

另一题:

details:
Given two integers a and b, which can be positive or negative, find the sum of all the numbers between including them too and return it. If the two numbers are equal return a or b.
Note: a and b are not ordered!

我的程序,简单的交换值,累加:
在这里插入图片描述

进一步可以运用三目运算符来代替if:
在这里插入图片描述

最好的程序:
在这里插入图片描述

先是用异或代替声明新变量来交换值
再用求和公式来代替累加

介绍一下按位异或:
原理是 1:满足交换律
2.与自身异或得0
3.与0异或得自身

a = a^b;
b = a^b = a ^ b ^ b=a(把a= a^b代入)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值