求俩数的平均值(不用公式)

//求两个数平均值的方法:Avg = (ValueA & ValueB) + (ValueA ^ ValueB) >>1
#include <stdio.h>
int bit(int x, int y)
{
 return (x & y) + ((x ^ y) >> 1);
}
int main()
{
 int a = 0;
 int b = 0;
 int ret;
 printf("请输入俩数:\n");
 scanf("%d %d", &a, &b);
 ret = bit(a, b);
 printf("%d\n", ret);
 return 0;
}
时间: 2024-08-06 20:04:11

求俩数的平均值(不用公式)的相关文章

一个正整数N,不用sqrt求开方数

#include<stdio.h> int main() { int n; // 正整数n int i; // for循环 int myR = 1; // 保证myR^2 <= n < (myR+1)^2 double temp; // 临时变量 while(scanf("%d", &n) != EOF) { if (n <= 0) continue; // 寻找myR for (i=1; i<=n/2; i++) { if ((i * i

【c语言】不用大与小与号,求两数最大值

// 不用大与小与号,求两数最大值 #include <stdio.h> int max(int a, int b) { int c = a - b; int d = 1 << 31; if ((c&d) == 0) { return a; } else { return b; } } int main() { printf("%d是大数\n", max(0, 2)); printf("%d是大数\n", max(3, 4)); pr

ZOJ3574(归并排序求逆数对)

Under Attack II Time Limit: 5 Seconds      Memory Limit: 65536 KB Because of the sucessfully calculation in Under Attack I, Doctor is awarded with Courage Cross and promoted to lieutenant. But the war seems to end in never, now Doctor has a new order

求Fibonacci数的三种方法和时间复杂度解析

题目: 定义Fibonacci数列如下: f(0)=1 f(1)=1 f(n)=f(n-1)+f(n-2), n>=2 输入n,用最快的方法求该数列的第n项. 解答一: 直接用公式写递归函数.很简单,很低效,就不写了.时间复杂度T(N) = T(N-1) + T(N-2); 也是f(n)本身,2^(n/2)<f(n)<2^n. 解答二: 用循环求,也很直接,效率很高了,时间复杂度是O(n). int f(int n) { if(n <= 1) return 1; int f0=1,

一天一个算法:求俩个数的最大公约数和最小公倍数

求俩个数的最大公约数和最小公倍数 解答: 当较大数除以较小数余数等于0时,较小数为最大公约数. 两数相乘结果除以它们的最大公约数为最小公倍数. int cdivisor(int x1,int y1) { int r,temp; if (x1<y1) { temp =x1; x1 = y1; y1 = temp; } while(x1%y1)//当较大数除以较小数余数等于0时,较小数为最大公约数 { r=x1%y1; x1=y1; y1=r; } return y1; } int cmultipl

算法怎么就这么难?----使用欧几里得算法求两数的最大公约数

本人菜鸟一枚,上午在看书的时候突然看到了求最大公约数的一个例题,突然就想到以前好像看过一个欧几里得算法,故又上网仔细找了一下欧几里得算法的原理.可能是本人时间长没看算法,脑子都生锈了. 看了几个讲解欧几里得算法的文章,大都只给公式,然后说了一大堆因为....在我还没看懂的时候,突然来了个所以...然后公式就这样推出来的.⊙﹏⊙b汗! 经过我这令人捉急的小脑袋转了半天,最后有了点眉目,所以拿出来和大家分享一下! 1.首先说一下:欧几里得算法是求两个数的最大公约数的,你可能会问:什么是最大公约数?

Codeforces 717A Festival Organization(组合数学:斯特林数+Fibonacci数列+推公式)

Codeforces 717A Festival Organization(组合数学:斯特林数+Fibonacci数列+推公式) 牛逼题.....推公式非常的爽...虽然我是看了别人的博客才推出来的... 0.1 斯特林数 下面要用到的是带符号的第一类斯特林数. \(x^{n\downarrow}=\prod_{i=0}^{n-1}(x-i)=\sum_{k=0}^ns(n,k)x^k\) 有递推公式\(s(n,m)=s(n-1,m-1)-(n-1)*s(n-1,m)\) 0.2 斐波那契数列的

POJ2407---Relatives(求单个数的欧拉函数)

Description Given n, a positive integer, how many positive integers less than n are relatively prime to n? Two integers a and b are relatively prime if there are no integers x > 1, y > 0, z > 0 such that a = xy and b = xz. Input There are several

求两数最小公倍数

输入代码: /* * Copyright (c) 2014, 烟台大学计算机学院 * All rights reserved. * 文件名称:sum123.cpp * 作 者:林海云 * 完成日期:2015年1月10日 * 版 本 号:v2.0 * * 问题描述:求两数的最小公倍数 * 输入描述:两个整数 * 程序输出:最小公倍数 */ #include<iostream> using namespace std; int gcd(int x,int y); int gad(int x,int