示例代码:
#define PERIMTER(X,Y) 2*X+2*Y
int main()
{
int length = 5;
int width = 2;
int high = 8;
int result = 0;
result = PERIMTER(length,width)*high;
printf("result = %d \n" , result);
}
问题分析:
上述代码是实现计算长方体体积,先通过宏计算出矩形周长,再乘以高。但实际结果为42,计算错误,原因是,宏定义只是文本替换,替换后的语句为:
result = 2*length + 2*width*high;
因此,用于表达式的宏,最好在定义时在整体语句上加个括号。
正确代码:
#define PERIMTER(X,Y) (2*X+2*Y)
int main()
{
......
阅读全文
转载自 http://www.lslnet.com/linux/books/resource/gmake.html
GNU make 指南
翻译: 哈少
译者按: 本文是一篇介绍 GNU Make 的文章,读完后读者应该基本掌握了 make 的用法。而 make 是所有想在
Unix (当然也包括 Linux )系统上编程的用户必须掌握的工具。如果你写的程序中没有用到 make
,则说明你写的程序只是个人的练习程序,不具有任何实用的价值。也许这么说有点 儿偏激,但 make
实在是应该用在任何稍具规模的程序中的。希望本文可以为中国的 Unix 编程初学者提供一点儿有用的资料。中国的 Linux
用户除了学会......
阅读全文
Android编程获取网络连接状态(3G/Wifi)及调用网络配置界面
获取网络连接状态
随着3G和Wifi的推广,越来越多的Android应用程序需要调用网络资源,检测网络连接状态也就成为网络应用程序所必备的功能。
Android平台提供了ConnectivityManager 类,用于网络连接状态的检测。
Android开发文档这样描述ConnectivityManager 的作用:
Class that answers queries about thestate of network connectivity. It also notifies applications when networkconnectivity changes. Get an instance of this class by callingContext.getSyst......
阅读全文
树状数组或线段树都可以
树状数组写法
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;
typedef long long ll;
int const MAXN = 50010;
int c[MAXN * 4],a[MAXN * 4];
int n;
int Lowit(int x){
return x & (-x);
}
int Add(int x,int d){
while(x <= n){
c[x] += d;
x += Lowit(x);
}
}
int Sum(int x){
int ret = 0;
while(x >......
阅读全文
思路:
要注意的几点:
(1)必须要在N种动漫里面选择M种,所以初始化时dp[L][M]时,除当m=0,dp[L][M]=0外,其他的初始化为负的无穷大;(见背包九讲关于初始化得方法)
(2)最后输出时要满足dp[L][M]不小于0,因为当dp[L][M]<0时,表明不可能在N种动漫里面选择M种(无法完全满足),也是题意:“If DuoDuo can’t watch all of the movies that her uncle had bought for her, please output 0”
AC代码:
#include<stdio.h>
#include<string.h>
#define inf 0x......
阅读全文