显示标签为“Programming”的博文。显示所有博文
显示标签为“Programming”的博文。显示所有博文

2015年1月21日星期三

[转载]C++中稀疏矩阵的一种实现

Use Sparse Matrix in C++


The following code snippet is hosted on Github Gist.

C++中读写二进制与ASCII混合文本流测试

Read and Write Binary & Text Hybrid Content Stream using C++


The following code snippet is hosted on Github Gist.

Matlab program for LU Factorization with partial (row) pivoting


上述代码等价的MATLAB内置函数是[L U P]=lu(A),此时满足LU=PA。
上述代码若修改成输出P比较好。即对应MATLAB的[L U P]=lu(A,’vector’)。此时满足LU=A(p,:)。

求解AX=B的时候我们需要交换A的行,对应的也需要对B进行相同的行交换。如果写成矩阵乘法的形式的话,LU分解后满足LU=PA,原方程AX=B变为LUX=PB,注意到求解出来的X的行没有发生交换。

万一需要p行变换的逆变换,那么p_back(p)=1:n即可。也就是说p向量比P矩阵更好用。P矩阵意味着需要进行矩阵乘法。

Reference:  http://cis.poly.edu/~mleung/CS3734/s03/ch02/LU_pivot.htm

2015年1月18日星期日

NKPC3-1704 Bowling Ball 保龄球

本来这个是早在百度空间就写了的,然而后来百度空间改版,造成很多文章排版出现问题,之前的URL地址也会失效,另外百度发布文章会有敏感词问题,还有插入代码会很蛋疼,所以现在将此篇文章迁移到Blogger。

Bowling

Time Limit: 2 Sec  Memory Limit: 64 MB


南开大学ACM协会的一个元老毕业后,开了家保龄球馆。他需要为他的保龄球馆的计算机写一个记分的程序。
       一局(GAME)保龄球分为10格,每格里有两次投球机会,如在第一次投球时没能全中,就有需要投第二球。
        每一格可能出现三种情况:
1.失球(MISS)
        无论何种情况,在一格的两次投球时,未能击倒10个瓶,此格的分数为击倒的瓶数。如果一次击球中未击倒一个瓶,则用一个’-’标记。
2.补中(SPARE)
         要一次击倒十个瓶子并非那么容易的!如果在第一次掷球后,你还有一次机会来击倒该格第一球所留下的情致。当第二次投球击倒该格第一球余下的全部瓶子,称为补中,用一个‘/’符号表示。补中的记分是10分加上下一次投球击倒的瓶数。
3.全中(STRIKE)
       当每一格的第一次投球击倒全部竖立的十个瓶时,称为全中,用一个(×)符号表示。全中的记分是10分(击倒的瓶)加该球员下两次投球击倒的瓶数。
       在第十格中情况比较特殊:
(1)如第二次投球未补中,则第十格得分为第九格得分加上第十格所击倒瓶数。
(2)如第二次投球补中,则追加一次投球机会,第十格得分为第九格得他加上10加上追加一次投球击倒瓶数。
(3)如第一球为全中,则追上加二次投球机会,第十格得分为第九格得分加上10加追加二次投球击倒的瓶数。因此从第一格到第十格的两次追加投球,都为全中,则为12个全中,得分为满分300分。
Input
  输入包括多组测试数据,你应当处理到输入结束为止。
  每组输入数据中,都只有一行,包含一局的记分符号,相邻的两个符号之间以一个空格隔开。记分的符号仅包括‘-’(不含引号)、‘/’(不含引号)、’X’(不含引号)及阿拉伯数字1到9。
Output
  对于每组输入数据,输出两行。对于第i组输入数据,输出的第一行为”Case i:”,输出的第二行为10个整数,表示每格的累计得分。相邻的两个得分以一个空格隔开。
Sample Input
X X X X X 5 / 7 1 - - X X X X4 4 3 3 2 2 1 7 7 1 2 - 9 / 2 2 6 3 2 / 1
Sample Output
Case 1:
30 60 90 115 135 152 160 160 190 220
Case
2:8 14 18 26 34 36 48 52 61 72
Source


Description

A patriarch of Nankai University ACM Association operates a bowling alley after graduation. Now, he needs a program to keep the score for the bowling.

There are ten frames in one game. You have two chances to knock down the ten pins in each frame. There are three possible cases for each frame.

1.MISS
If you fail to knock down all of the ten pins during two delivers in a frame, the score you get from this frame will be the number of the pins that you have knocked down. If your deliver knocks down zero pin, your score board will be marked a "-".

2.SPARE
Getting all ten pins down with a single ball is not as easy as it seems! So, if you leave one or more pins standing after your first delivery, you get a second chance to knock all the pins down. This is your "spare" shot. If you knock all remaining pins down on the second shot you have made your spare. A spare is marked on the scoresheet with a "/".And the score you get will be 10 plus the number of pins you knock down during the next delivery.

3. STRIKE
When the bowler knocks down all ten pins during the first delivery in a frame, it is called a strike. Clearly your score goes up by ten, but like a spare, you get a bonus – the number of pins you knock down during your next two deliveries, which will be added to the score. A strike is on the scoresheet with an "X".

Note that the tenth frame is a little different with others.
(1) If you don't knock down the ten pins, the score will be added by the numbers of pins you knock down.
(2) The tenth frame rewards you with a final bonus ball if you convert your spare. And your score will be added by 10 and the number of pins you knock down during your final bonus delivery.
(3) If you make a strike, you will get two bonus balls. The score will be added by 10 and the number of pins you knock down during your final two bonus deliveries.You can thus throw nine strikes in the first nine frames and, if you get another two in the tenth, the bonus ball means the most strikes you can have in one game is twelve. And the full mark is 300.


Input

Input contains several cases. The input is ended up with the end of file (EOF). Each case includes one line which contains several numbers and signs to record a bowling game. The signs are '-', '/' or 'X'. And the numbers range from 1 to 9. The adjacent signs and numbers are separated by one blank.

Output

For each case, please output two lines. The first line for the i-th input case should be "Case i:". The second line should be 10 integers, standing for the 10 accumulative scores of the frames in a bowling game. And each two adjacent integers should be separated by one blank.

Sample Input

X X X X X 5 / 7 1 - - X X X X
4 4 3 3 2 2 1 7 7 1 2 - 9 / 2 2 6 3 2 / 1

Sample Output

Case 1:
30 60 90 115 135 152 160 160 190 220
Case 2:
8 14 18 26 34 36 48 52 61 72

HINT


Source

NKPC3



本来NKPC3比赛时,我一直没有思路,总是想着要
按照回合数划分数组,
没有做出来,就一直搁置着。
现在突然来了灵感,
用(总计)击球次数划分数,
不论是存储,还是处理运算,都比较简单。
这样一来,回合数没有了实际作用,沦为主计算过程(cal(culate))执行次数的计数器。
{详见PASCAL源代码的"~"以及"~~"处}

以下是本人用GCC做的解答: 以下是本人用PASCAL做的解答: 以下是好友Liuhy用PASCAL做的解答: 原帖发布时间:2007年10月05日

2011年4月13日星期三

Karnaugh Map Minimizer exe. for up to 8 inputs | 最多八输入卡诺图化简程序

最近正在学数字逻辑设计《数字设计原理与实践》,有关卡诺图化简的问题,有时候不能看出来最简单的画法,尤其是对多输入(5输入以上),于是乎在网上搜索到了一个卡诺图化简器。


软件最多化简8输入的卡诺图,结果以文字和图形同时显示。
软件名称:Karnaugh Map Minimizer   Alpha 

界面语言:英语、克罗地亚语
授权形式:GPL
项目地址:http://k-map.sourceforge.net/
下载地址:http://sourceforge.net/projects/k-map/

2011年4月6日星期三

使用MATLAB计算北京到纽约的新旧航线里程

数学实验的PPT课件参见百度文库
http://wenku.baidu.com/view/2eda008884868762caaed5a6.html
本文给出了实验的示例程序以及输出结果。

以下是mlab21.m的代码,用于计算北京到纽约的航线例程:

执行mlab21后得到以下结果
Dmatrix =
             0       1144.90       2155.64       9608.53      10993.77
       1144.90             0       1766.78       9936.44      11870.27
       2155.64       1766.78             0       8283.06      10764.64
       9608.53       9936.44       8283.06             0       4061.47
      10993.77      11870.27      10764.64       4061.47             0
可以清楚的看到,北京直飞纽约的航线例程为10993.77km,
而旧航线里程为1144.90+1766.78+8283.06+4061.47= 15256.21 (km).
假设飞机按照统计的平均时速980km/h匀速前进,那么新航线比旧航线至少节约了4.35小时,这尚未包含在各个中转站花费的候机时间。

以下是mlab22.m的代码,用于在球面上绘制新旧航线:


这一段程序调用了skyway(p1,p2,color)函数,代码如下:


执行mlab.22之后,可以在Figure(1)窗口中看到绘制的新旧航线图:
视角一(示太平洋)
视角二(示北极)

2011年3月29日星期二

简单C语言快速计算真值表!

正在学数字逻辑的我,实在厌倦了那些纯粹推演真值表的题目,太无聊了!于是为了偷懒一下,编写了一下小程序……

The follwing code snippet is hosted on Github Gist.
在程序中,使用到了几个逻辑算符,取反INVERT『~』、逻辑与AND『&』、逻辑或OR『|』,它们跟 非『!』、逻辑与『&&』、逻辑或『||』的区别是,前者是对二进制操作数的每一位进行操作,而后者则是把操作数当作一个整体进行运算。
如果你如要更多或者更少的变量,或者你需要计算其他的表达式,简单地按照这个模板进行更改即可,这并不困难~
之所以选择C-FREE编程环境,是因为他比Turbo C++或者Borland C要友好的多,而且是免费的。如果你对编程环境|编译器了解不多,那建议你使用C-FREE。