博文
赋值语句作为循环条件的问题(2005-08-20 20:12:00)
摘要:吃惊中,我以为这个应该是无限的原来表达式 x = y 的值就是 x 的值啊(赋值成功,若赋值不成功呢?)---------------------------------#include void main(){ int x = 3, y = 2; while(x = y){ printf("x = %d\ty = %d\t(x = y) = %d", x, y, x = y); y--; printf("#\n"); continue; }}该程序输出结果为:x = 2 y = 2 (x = y) = 2#x = 1 y = 1 (x = y) = 1#说明循环体执行了两次......
计算某一天在该年中的天数(2005-08-20 19:03:00)
摘要:int dayChange(int year, int month, int day){ int days[12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; int d = days[month - 1] + day; //润年2月以后多加一天 if(month > 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))d++; return d;}......
(转)满足任意精度的概率事件发生器(2005-08-20 16:43:00)
摘要:rickone 的 BLOG
http://programfan.com/blog/article.asp?id=3860
#include<iostream.h>
#include<time.h>
#include<stdlib.h>
#define MinProb 3.05185094759972e-5
bool happened(double probability)//probability 0~1
{
if(probability<0)return false;
if(probability<MinProb)
return rand()==0&&happened(probability/MinProb);
if(rand()<=probability*RAND_MAX)
return true;
return false;
}
MinProb是理论上的最小发生概率,为什么会这样,因为计算机是离散的,它永远只能得到有限范围内的随机离散数。
bool happened(double probability);
是按概率probability发生的随机事件发生器,如当probability=0.5时,将等概率得到真或者假,当为其它值时,将按probability的概率得到真,以1-probability的概率得到假。
如果probability比较大,MinProb是足够精确的,如0.5和0.5000001是有足够精确相等的,它相当于一个大概率加一个小概率,小概率被‘吃’掉了。
而小概率事件单独发生能不能突破MinProb的极限呢?
我昨天睡觉的时候想出来了这样的算法,从理论上是可以无限精确的,即任意小的小概率事件都可以得到。
其实原理非常简单,即独立事件同时发生的概率等于各事件的概率之积。举个例子:......
产生任意范围内的等概率随机数(2005-08-20 16:43:00)
摘要:C提供的标准随机函数是 rand(),返回 0 ~ RAND_MAX,共 RAND_MAX + 1 个随机数,如果我需要更大的随机数范围应如何做?
而且 rand() 返回的是伪随机数,就是说不管运行多少次结果都是一样。用srand() 解决。
试设计:
long random(long n);返回 0 ~ n-1 间的等概率随机数
分析思路:
以 0 ~ 1的等概率为中介,先将 rand() 返回的概率数转化到 0 ~ 1 之间,在转化到 0 ~ n 之间。
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
long random(long n)
{
double r = (double)rand() / RAND_MAX;
return (long)(r * n);
}
int main()
{
long n, t1 = 0, t2 = 0;
while(true){
printf("输入 n 值:");
scanf("%ld", &n);
srand(time(NULL));
for(int i = 0;i < n * 100;i++){
long t = random(n);
if(t == 1)t1++;
else if(t == n - 1)t2++;
}
printf("出现次数\n");
p......
一道算法题(2005-08-20 20:17:00)
摘要:ilpr 8月17日 发布 http://programfan.com/club/showbbs.asp?id=98465&page=1
例如整数121,如何实现1-10十个数字,在不重复使用的情况下,分成5组,两两一组相乘,然后等到的五个积相加,和等于121!用什么算法求出这五组组合!
--------------------------------------
rickone 的算法(8月18日)
#include#includeint s[10];int v[11];void dfs(int c, int ob){ int i; if(c == 10){ int sum = 0; for(i = 0;i < 10;i += 2) sum += s[i]*s[i+1];
if(sum == ob){ //输出组合 for(i = 0;i < 10; ++i) cout<......
数的拆分问题(2005-08-17 18:39:00)
摘要:把任意一个整数n>3,如何拆分才能让(不能重复)而且分开后所有数的和等于原数,而且能组合出任意一个数例如把100拆分,拆分后的数和等于100,而且能组成1~100之间的任意一个数 以下是我的程序#include using namespace std;int main(){ int m = k = 1, n; cin >> n; while(m < n){ cout << k << " "; k *= 2; m += k; } cout << n - k + 1; system("pause");}......
键盘代码表(2005-08-15 21:01:00)
摘要:bios.hint bioskey(int cmd)本函数用来执行各种键盘操作,由cmd确定操作。 cmd可为以下值: 0 返回敲键盘上的下一个键。若低8位为非0,即为ASCII字符;若低8位为0, 则返回扩充了的键盘代码。 1 测试键盘是否可用于读。返回0表示没有键可用;否则返回下一次敲键之值。 敲键本身一直保持由下次调用具的cmd值为0的bioskey所返回的值。 2 返回当前的键盘状态,由返回整数的每一个位表示,见下表: ┌──┬───────────┬───────────┐ │ 位 │为0时意义 │为1时意义 │ ├──┼───────────┼───────────┤ │ 7 │插入状态 &nb......
%*的又一应用(2005-08-15 12:46:00)
摘要:用*组成X型图案,如下:1* *7 2* *6 3* *5 4*4 5* *3 6* *27* *1 程序由林杰杰发布http://programfan.com/club/showbbs.asp?id=97897#include int main(void){ int i; int k; for (i = 1;i <= 7;i++) { k = (i <= 4 ? i : 8 - i); printf("%*s",k,"*"); printf("%*s\n",8 - 2 * k,"*" + (k == 4)); } return 0;} 运行结果:* ** * * * * * **  ......
Jos问题(2005-08-14 16:25:00)
摘要:Jos问题n个人围成一圈,从1开始顺序报数,报到m的退出,再从1开始报数,直到仅剩一个人为止。输出出列序列。#include<stdio.h>#include<malloc.h>
struct Man{ int num; struct Man *next;};
void Jos(int n,int m = 3){ struct Man *head, *p, *b, *t; int i; head=(struct Man*)malloc(sizeof head); p = head; b = head; for(i = 1;i < n;i++){ //创建循环单链表 p->num = i; p->next = (struct Man*)malloc(sizeof p->next); p = p->next; } p->num = n; p->next = head;//创建完毕
//开始报数 p = head; i = 0; do{ if(++i == m){ //报到3 i = 0; //重新......
解析:菱形的高效率代码(2005-08-15 12:28:00)
摘要:解析:菱形的高效率代码输出菱形的高效率代码如下:#include int main(int _){ while(_!=10) { printf("%*s\n",_<=5?4+_:14-_,"*********"+(_<=5?10-2*_:2*_-10)); _++; } return 0;}运行结果如下: * *** **************************** ***** *** *(注:第4、6行显示有误。应为一个菱形。下同)解析:1.printf("%*s", int, char*);%*表示右对齐方式显示,int为从左起到右对齐处的字符数例如:printf("%*d", 4, 345);输出结果如下:3452._<=5?4+_:14-_该表达式即是计算菱形右边界3."*********"+(_<=5?10-2*_:2*_-10)该表达式是计算显示*的个数,事实上是地址运算。printf("%p\n", "*********"+(_<=5?10-2*_:2*_-10));注:%p为输出16进制32位地址。则输出:00420FA000420F9E00420F9C00420F9A00420F9800420F9A00420F9C00420F9E00420FA0若改为printf("%*s\n",_<=5?4+_:14-_,"123456789"+(_<=5?10-2*_:2*_-10));则输出结果如下: 9 789 5678934567891234567893456789  ......
