随笔-8  评论-0  文章-0  trackbacks-0
  2012年5月10日

一些杂项:

 

1. 获取命令行参数:

>ruby xxx.rb x y z

全局变量ARGV将保存x y z三个参数

可以用下面代码访问

ARGV.each do |arg|

  puts arg

end

 

2. 字符串转换成整形

str.to_i(base 10)

posted @ 2012-05-10 16:00 月神夜 阅读(2) 评论(0) 编辑
  2012年5月7日
摘要: Ruby入门笔记一切皆为对象“Hello”.length方法定义:def开头 end结尾命名一般采用下划线分隔单词字符串中可以嵌入表达式返回值:a)return+返回值 b) 函数最后一行代码的值为返回值(太神奇了)类定义:class 开头 end结尾Initialize是构造方法@开头的变量是实例变量(一个实例对应一个变量)to_s 方法可重载self. 定义类方法(用类名调用,类似类静态方法,不需实例化对象)@@开头是类变量(类似静态变量)类的继承<表示继承class Popsong < Song def initialize(name, artist, duration, l阅读全文
posted @ 2012-05-07 16:26 月神夜 阅读(47) 评论(0) 编辑
  2012年2月27日

导师让我多看一些ML的模型,最近打算先下学习LDA模型

先找一些资料

建议学习LDA模型的步骤

http://www.xuwenhao.com/2011/03/20/suggestions-for-programmers-to-learn-lda/

计划学习步骤:

1.paper:

Probabilistic Topic Models

Gibbs Sampling for the Uninitiated

2. source code

 

Probabilistic Topic Model

 

 

posted @ 2012-02-27 09:38 月神夜 阅读(70) 评论(0) 编辑
  2012年2月26日

Financial Management

就是个求平均数的

#include <iostream>
#include <iomanip>
using namespace std;

int main(){

const int k=12;
float month,sum=0;
int i;
for(i=0;i<k;i++){
cin>>month;
sum+=month;
}
cout<<"$"<<setiosflags(ios::fixed)<<setprecision(2)<<sum/(float)k;

return 0;
}



posted @ 2012-02-26 19:33 月神夜 阅读(11) 评论(0) 编辑

Background

For years, computer scientists have been trying to find efficient solutions to different computing problems. For some of them efficient algorithms are already available, these are the "easy" problems like sorting, evaluating a polynomial or finding the shortest path in a graph. For the "hard" ones only exponential-time algorithms are known. The traveling-salesman problem belongs to this latter group. Given a set of N towns and roads between these towns, the problem is to compute the shortest path allowing a salesman to visit each of the towns once and only once and return to the starting point.


Problem

The president of Gridland has hired you to design a program that calculates the length of the shortest traveling-salesman tour for the towns in the country. In Gridland, there is one town at each of the points of a rectangular grid. Roads run from every town in the directions North, Northwest, West, Southwest, South, Southeast, East, and Northeast, provided that there is a neighbouring town in that direction. The distance between neighbouring towns in directions North-South or East-West is 1 unit. The length of the roads is measured by the Euclidean distance. For example, Figure 7 shows 2 * 3-Gridland, i.e., a rectangular grid of dimensions 2 by 3. In 2 * 3-Gridland, the shortest tour has length 6.


Figure 7: A traveling-salesman tour in 2 * 3-Gridland.

Input

The first line contains the number of scenarios.

For each scenario, the grid dimensions m and n will be given as two integer numbers in a single line, separated by a single blank, satisfying 1 < m < 50 and 1 < n < 50.


Output

The output for each scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. In the next line, print the length of the shortest traveling-salesman tour rounded to two decimal digits. The output for every scenario ends with a blank line.


Sample Input

2
2 2
2 3


Sample Output

Scenario #1:
4.00

Scenario #2:
6.00

 

我提交的代码

#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;
int main(){
int n;
cin>>n;
int i;
float result;
for(i=1;i<=n;i++)
{
int m,n;
cin>>m>>n;
cout<<"Scenario #"<<i<<":"<<endl;
if((n%2!=0)&&(m%2!=0)){
result=m*n-1+sqrt(2.0);
cout<<setiosflags(ios::fixed)<<setprecision(2)<<result<<endl;
}
else {
result=m*n;
cout<<setiosflags(ios::fixed)<<setprecision(2)<<result<<endl;
}
cout<<endl;
}
return 0;
}

归纳了几次得到的结果,如:

2*2   4

2*3   6

3*3   8+根号2

3*4   12

3*5   14+根号2

但其实对问题的实质还是没有太了解。

几次出错,原因如下:

1. 对于ACM的输入输出格式不太懂。

2. 对cout的格式化输出不熟悉,没注意题目输出要求。要学习浮点数格式化输出的用法(iomanip.h中的setprecision和setiosflags)



posted @ 2012-02-26 16:55 月神夜 阅读(45) 评论(0) 编辑
  2012年2月24日

初看C++ primer,const关键字的内容林林总总,记录一下。

 

1. 定义const对象:把一个对象定义为常量

const int bufSize=512;
bufSize=0;//错误

2. 被const修饰的对象默认为文件的局部变量:只能在定义该对象的文件中使用,其他文件不可访问。除非显示的指定此const对象为extern。

//file_1.cc
extern const int bufSize=fcn();
//file_2.cc
extern const int bufSize;//使用file_1里的bufSize

3. const引用:指向const对象的引用,即对于const对象,其引用也是const修饰的

const int ival=1024;
const int &refval=ival;

因为任何对ival的赋值都是不合法的,所以只读的refval是合理的。

另,用const引用可以灵活的初始化。

int i=42;
const int &r1=42;
const int &r2=r+i;
//以上对于非const引用都是不合法的

4. const与指针

4.1 指向const对象的指针

const int ival=10;
int *p=&ival;//×
const int *p=&ival;//
const void *p=&ival;//

4.2 const指针

int ival=0;
int *const p=&ival;

p是指向int型变量的const指针,p不能指向其他对象。

4.3 指向const对象的const指针

const int *const p=&ival;

5. const形参

bool isShorter(const string &s1, const string &s2);

我的理解是,利用const形参来避免复制形参。在结果上与复制形参一样(不改变实参的值)但可以避免复制对象的是时空消耗。

另外,可以使函数调用更灵活:把不修改相应实参的形参都改为const形参,可以灵活的用常量、字面值来调用函数。

6. const数组形参:不需要修改数组形参元素

void f(const int*);

 

To be continue...








posted @ 2012-02-24 10:46 月神夜 阅读(25) 评论(0) 编辑
  2012年2月15日
摘要: 刚刚结束了Drexel的电话面试好几个老师一起,好像很欢乐,互相大笑着问了几个简单常规的问题,都是导师之前交代过的1. Research Interest2. Describe your work in detail3. Why you want to pursue a PhD?4. Why you choose Drexel5. Which professor you would like to study with然后就问了any question总之就是很自由简单的几个问题,15分钟左右,就over了期待结果啊阅读全文
posted @ 2012-02-15 23:28 月神夜 阅读(33) 评论(0) 编辑
  2012年2月10日
摘要: 温习算法知识,学学C++,打算从排序算法开始书目包括:《计算机算法设计与分析》王晓东《数据结构》严蔚敏暂时写这么多,等代码写好贴出来~~VS2005出现: 无法找到“XXX.exe”的调试信息,或者调试信息不匹配。未使用调试信息生成二进制文件。这一错误,在网上找到的解决方案为:如果创建空项目,需要对项目属性进行如下配置:首先打开菜单 项目->项目属性页 1。选择 配置属性->链接器->调试->生成调试信息 改为 是 2。选择 配置属性->C/C++ ->常规->调试信息格式 改为 用于“编辑并继续”的程序数据库(/ZI) 3。选择 配置属性->C阅读全文
posted @ 2012-02-10 17:30 月神夜 阅读(301) 评论(0) 编辑
仅列出标题