一些杂项:
1. 获取命令行参数:
>ruby xxx.rb x y z
全局变量ARGV将保存x y z三个参数
可以用下面代码访问
ARGV.each do |arg|
puts arg
end
2. 字符串转换成整形
str.to_i(base 10)
导师让我多看一些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
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;
}
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)
初看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...