当前角色:电脑爱好者
2012
01.18

版本控制也是一种软件工程技巧,借此能在软件开发的过程中,确保由不同人所编辑的同一程式档案都得到同步。对于猴哥来说,主要用处是在与代码管理和多人协作。

过去猴哥一直使用SVN来实现版本控制,在一位不愿意公开姓名的同学的建议下,猴哥尝试使用Git。其主要原因在于Git可以离线提交,并且能够较好地处理冲突问题。

猴哥于是尝试把手头上正在做的NP-Carlo项目从SVN转换到Git,没想到遇到了不小的麻烦。之前正是由于SVN必须联网提交,NP-Carlo半年来一直没有提交,因此版本控制系统的很多信息都是不正确的,校验这些信息花了猴哥很长时间。尤其是其中将某个目录进行拷贝的过程,导致SVN认为他们是同一个目录的不同工作拷贝,但实际上猴哥是想在同一份工作拷贝中,将一个目录复制成两个——结果就出了这样的麻烦。

在删除了新的拷贝中所有的“.svn”文件以后,SVN的数据终于正确了,然后猴哥将数据最后一次提交,在从托管商那里dump下来了代码仓库,挂在本地的SVN服务器上。

按照教程来说,直接使用下面的语句就可以了。猴哥的电脑可能是因为上一次的程序没有运行完,执行下面的语句后一直没有反应。

$ git svn clone svn://localhost --no-meta -s carlo

重新启动以后,再运行上面的代码就可以了。TortoiseGit也有类似的Git Clone功能,但是如果从本地的工作拷贝导入,会出现一些奇怪的问题。所以猴哥使用了本地的SVN服务器导入。

猴哥其实使用版本控制系统也不多,SVN使用目录来区分trunk、branches、tags;可是Git不是,如果要修改某一个分支,则需要重新checkout来进行切换。刚刚完成导入以后,猴哥郁闷的发现工作目录里只有trunk里面的东西了。

经过猴哥的考虑,仅仅将NP-Carlo核心的一个目录作为trunk导入,其他的内容由于不是本项目的内容,而不再进行版本控制。

经过如此的调整,NP-Carlo项目的工作目录就整洁多了,但是版本号从13变回到了1,这还不包括由于前面说的网络问题,trunk里面一直没有提交的若干次修订。

Git进行软件开发是很不错的,因为其可以为每一段代码追溯来源、进行合并和拆分,但猴哥不打算把所有的版本控制目录都改为Git,特别是即将进行的美国数学建模竞赛中,猴哥依然打算使用SVN,因为Git似乎不适合非代码的东西。

在转换的过程中,猴哥发现了一些不错的书籍(ProGitSVNBook),打算有机会好好看一看,搞明白版本控制系统的用法。

Revision control, also known as version control and source control (and an aspect of software configuration management or SCM), is the management of changes to documents, programs, large web sites and other information stored as computer files. It is most commonly used in software development, where a teamof people may change the same files. For the Monkey, it is used to manage the codes and do the team-work.

The Monkey used to code with SVN, which is one of the popular revision control system. With the suggestion from a person who does not want to publish his name, the Monkey decided to  turn to Git, in which the Monkey can commit without the Internet connection.

The Monkey would like to switch the NP-Carlo project from SVN into Git. Because of the requirement of Internet connection when commit, the Monkey had not committed the modifications of NP-Carlo for quite a long time. So, the working copy came to very dirty. Especially the new folder, which was cloned by the monkey, was incorrectly treated as the original folder rather than a new folder. The Monkey spent a long time to solve the problem by deleting all the ".svn" meta-data.

According to the book, the following command would migrate the code into Git. However, the Monkey's computer might not finish its previous task. The command went to irresponsible.

$ git svn clone svn://localhost --no-meta -s carlo

After rebooting, the command above worked for the Monkey. But the cloned folder made the Monkey anxious, in which there was only files in the trunk of SVN working copy. The Monkey realized that Git does not use folders to divide the trunk and branches — the working copy should be re-checkout if the Monkey wants to switch into a branch!

To make the working copy tidy, the Monkey only imported the Carlo folder into Git, and deleted others, which were not in the project, but just for debugging, from the revision control.  After that, the reversion number turned back from 13 to 1, which were not included the uncommitted modifications because of the connection.

Git is good, but the Monkey does not want to switch everything into Git, especially the coming MCM paper. Because the Git may not suit for the non-code stuff.

During the converting, the Monkey found some good books (ProGitSVNBook), which are worth reading to understand the revision control.

2011
07.16

The Monkey was busy in his Monkey Sync these days, which can update his micro-blog and blog in several Facebook-like websites automatically.

After last change, Monkey Sync had worked very well for a long time. It posted the posts in Monkey's House to Facebook, Renren, Qzone and Baidu, and sent the statuses from Twitter to Renren, Qzone and Baidu.

There was still a problem that Monkey Sync 's code was a little bit messy because it was modified on a open-source code, which had some failures at its very beginning. So, Monkey Sync was being slower. Because of those failures, the system might be obstructed by some errors raised by network status.

In this modification, the Monkey added the module for Sina Weibo, the Chinese Twitter, and solved a main problem about Twitter 's Rate Limitation, which was not noticed and considered by the first author.

To reduce unnecessary calculations, the Monkey uses MemCached. To get access to Twitter without limitation, the Monkey uses OAuth Libraries, which makes his work much more easier than before.

The Monkey changed the outline of the whole system, which will make it stronger. In this project, the Monkey learnt MemCached and a lot of libraries he had not known.

Thanks to Google App Engine, he could practice Python and learnt a lot.

这几天猴哥在为他的同步系统而忙碌,这个系统能够自动更新他的微博们。

在上一次改动之后,猴哥的同步系统曾经正常工作了很长时间,它把“猴子的小窝”里的帖子转发到Facebook、人人网、QQ空间和百度空间,把Twitter状态转发到人人网、QQ空间和百度空间中。

但是这里仍然有一个问题,由于这个系统是基于一个开源代码修改而来的,而这个代码最最开始的时候就有一些缺陷,所以猴哥的这个系统代码比较凌乱。因此同步系统越来越慢,有的时候还会因为网络状况的一些小错误而被阻碍运行。

在这次修改中,猴哥增加了针对新浪微博的模块,解决了Twitter“速率限制”这个未被原作者注意和考虑到的问题。

为了减少不必要的计算量,猴哥使用了MemCached;为了获得不受限制的Twitter数据,猴哥使用了OAuth库,大大节省了他的工作量。

猴哥改变了整个系统的结构,以便使这个系统更加健壮。在这次的项目中,猴哥学习了很多。

2011
07.09

As I said in last post, we got a big problem in the mysqldump double encoding bug (Bug #28969).

Now, if I just have a SQL file, which is double encoded by mysqldump, and the original database was destroyed, how can I restore the database?

The way is quite sample, but always get confusion because of the expression.

  1. Restore the SQL file into MySQL, and you will get a messy database.
  2. Use mysqldump again to export a SQL file, which is decoded.
     mysqldump -h "$DB_HOST" -u "$DB_USER" -p"$DB_PASSWORD" \
    --opt --quote-names  --skip-set-charset \
    --default-character-set=latin1 "$DB_NAME"   > /tmp/temp.sql
  3. Restore the decoded SQL file into MySQL.

Your database now comes back!

References

正如我在上一篇日志说的那样,我们因为mysqldump(Bug #28969)的重复编码漏洞,遇到了一个大问题。

现在,加入我有一个被mysqldump重复编码的SQL文件,而原始数据库已经被摧毁,如何恢复原始数据库呢?

方法非常简单,只是它过去的表述比较容易造成误解。

  1. 恢复SQL文件到MySQL,得到充斥着乱码的数据库;
  2. 再次使用mysqldump输出一个解码的SQL文件;
     mysqldump -h "$DB_HOST" -u "$DB_USER" -p"$DB_PASSWORD" \
    --opt --quote-names  --skip-set-charset \
    --default-character-set=latin1 "$DB_NAME"   > /tmp/temp.sql
  3. 恢复这个新的SQL文件到MySQL中。

你的数据库现在回来了!

参考文献
2011
02.22

经过几天的编程、调试,我的微博、博客同步系统终于基本完成了。

这个架设在Google App Engine上的系统能够将我的推特(Twitter)上的微博同步到人人网百度空间腾讯QQ空间及QQ签名上。过去虽然通过第三方网站,我能够将微博同步到人人上,但是其非常不稳定,再加上前些日子又有一小撮人在大街上遛弯,导致第三方网站的同步渠道开始维护;现在使用自己的系统,再也不用担心别人出门运动对我的微博产生的影响了。

这个系统还能够将猴子的小窝的文章同步到人人网百度空间腾讯QQ空间以及脸谱网(Facebook)上。

这一套系统的难点主要是在于如何避过百度、腾讯这两个不喜欢开放平台的网站的图灵测试。绕过百度系统的方法比较成功,绕腾讯的有些烦琐,有效性尚需一些时日来观察。

对比代码,发现脸谱网的开放平台做的最好,其安全性最好,而且实现的代码长度也很短;相比之下,一直号称开放的人人网似乎并不友好,至少我看了很长时间技术文档,也不明白它的那个API除了PHP下直接调用官方库以外,怎么用;最最恶心的是腾讯的系统,光一个验证就来来回回地折腾了好几次,估计腾讯不愁服务器负载问题。

等到下面技术成熟了,我可以考虑小范围内测。

The Monkey has coded his Monkey Sync System, which can update his micro-blog and blog in several SNS websites automatically.

This application in Google App Engine can skip the Turing Test of Baidu and Tencent, the websites who are not open-socially.

Comparing with the most open API, Facebook's Graph API, the Renren, a Chinese Facebook, which is always claimed open social system, is not friendly for developers, at least non-PHP ones.

2011
02.21

正如健牛牛在人人网上说的一样,16日的聚会是一段“奇葩”的回忆。

动议缘自健牛牛子鉴牛微博上的一段评论,之后小何宫牛牛的加入让这次聚会变得更加丰满。与此同时,我正和朱牛牛商量我们的信息学兴趣小组是否要再趁大家都还没开学搞一次聚会。

我们这一届信息学兴趣小组主要由高一以前搞过信息学竞赛的健牛牛等和高一时候才开始搞竞赛的朱牛牛等组成,两拨人马平时培训不常在一起,前者多与小何宫牛牛等其他届别的选手有垂直联系,后者则更多的是本年级的平行联系。我作为高一以前搞过信息学竞赛的同时又经常参与后者培训的选手,对这两拨人接触都比较多。

为了让搞过竞赛的这些有共同兴趣爱好的同学多多认识,我向朱牛牛健牛牛提议将这两场聚会合并起来,这样同一届的和学长、学弟都可以互相认识。

不能不说这一次聚会的创意甚好,可惜的是老师和宫牛牛剡牛牛安牛牛因故未能参加。

这一次的聚会集合地点在四中正门口,这一天四中礼堂正在举行民进党的活动,本来我们是可以混进学校的(至少是礼堂前的公共区域),结果传达室的老师一眼就把我认了出来……(谁让我当年老去找他取信的)

后来人来的差不多了,在子鉴牛的帮助下,我们顺利地进入了食堂内,子鉴牛给我们发竞赛奖学金,然后我惊奇的发现食堂里面通用技术那个什么比赛的海报竟然还贴着……

人齐了以后,我们去了京味楼,这个让我过去做梦都不敢想的奢侈所在。

大家聊得很快乐,从四中的近况到信息学竞赛北京队的那些新人和旧人,再到大学生活、学习,甚至各种奇闻轶事,我太激动以至于把嗓子说哑了。

我还很荣幸的认识了道元班的驰牛牛(音),这孩子虽然年龄小,但是兴趣倒是挺广泛,在四中绝对会有好的发展;另外他说话跟在场的嵩牛牛和我们班的化学大牛小禹小朋友很像,果然是牛呀!

回来的路上,我和朱牛牛嵩牛牛戴牛牛又聊了一段时间,这时才意识到——匆忙之中忘记合影了,把相机又原样背回了家中。

希望还有机会与四中的、清华的、南大的这些高材生们相聚。

This post is talking about a party of the Informatics Group of Beijing High School 4.

Some friends who had this party are now in Tsinghua University, and some are in Nanjing University, while some are still in Beijing High School 4. Some of them had taken part in the Olympic of Informatics before Senior 1, and some had after Senior 1, who are both my best friends.

We talked about everything, from the today of Beijing High School 4 and Olympics of Informatics, to the life in universities. We were in craze of happiness. I almost had my throat hurt.

I made a friend to Ox Chi, who are in the Honor Daoyuan Class of the high school and are like my classmate in high school.

I wish we can gather again for nothing but fun, as we share the common interest to Computer Science, Mathematics and everything...

2011
02.20

Abstract

This post is about story during the recent COMAP MCM 2011 from Wed. Feb. 9 to Tue. Feb. 15.

摘 要

本帖是关于刚刚过去的2011年美国大学生数学建模比赛期间(从二月九日星期三到二月十五日星期二)发生的故事的。

1 Introduction

This paper is taking about the experience of the Monkey's first MCM racing. The Monkey and his partners are freshmen, whose team was the only freshmen team in BIT for this competition. Without knowledge and experience, they eventually took part in the competition.

2 Preliminaries

COMAP, the Consortium for Mathematics and Its Applications, is an award-winning non-profit organization whose mission is to improve mathematics education for students of all ages. Since 1980, COMAP has worked with teachers, students, and business people to create learning environments where mathematics is used to investigate and model real issues in our world[1].

Mathematical Contest in Modeling (MCM), challenges teams of students to clarify, analyze, and propose solutions to open-ended problems. The contest attracts diverse students and faculty advisors from over 500 institutions around the world[2].

Normally, MCM is running in 4 days from a 8PM EST to an another 8PM EST. BIT always provides extra opening of dormitories, classrooms and library for participators resting, discussing and searching. The questions will be published by COMAP on their website at the beginning of the competition. The final answer should be provided by a paper.

3 the Story

The Monkey and his partners were gathered on Wed. Feb. 9 in the Dorm 8 of Zhongguancun Campus of BIT. After check-in they went into their dormitory. As the Monkey learnt before, the dormitory was very old and bad. Thinking about the exciting competition, the Monkey accepted it.

Time flied awfully quickly. After the conference on Thu. Feb. 10 about something should be taken more care about during the competition, the beginning eventually came to the team.

3.1 Comprehension

Fetching the problems immediately, the team started the comprehension section of the competition. Thanks to the Monkey's partner Wang, the whole team got the problem understood very soon. Problem A asked to give a design of half-pipe[3] course to make a skilled snowboarder produce highest vertical air. Problem B was about organizing telecom repeaters to work in a given area and serve an amount of users. Problem C was a very boring question, which was excluded by the team at first.

3.2 Decision

When other teams were still in the comprehension, the team went to the decision section. Team mates were trying to search materials about each problems and then made decision by the amount of materials and the ideas. A blip made in this section was that the team did not use the source of library as the library opened in that afternoon, when they finished the problem decision.

3.3  Pondering

Finally, they chose Problem A and went to the Pondering section. The monkey had not learnt Differential Equation and some required Physics knowledge. His partners decided to figure out the exact shape of the course by calculation. As the experiences of the Monkey's four-years Informatics Olympics, the Monkey predicted this problem might not be solved as an exact answer.

The Monkey was trying to find more materials while his partners were trying to calculate. In the evening of Feb. 11, the Monkey went to the National Library of China to fetch more information. Also in that evening, his partners finished their first differential equation and tried to get it solved.

3.4 Writing

It happened as the Monkey predicted. The differential equation can not be solved maybe for their limitation of the knowledge in that field. Plan B had been made as a guarantee.

After another attempt, the team finally decided to run Plan B. The writing section had been started on Feb. 12 evening and finished on Feb. 13. The Monkey's partners decided to go home for their Valentine's Day.

3.5 Modifying

The Monkey then modified the paper on Feb. 14 and 15. After the first-time work all night, the final edition of the paper had been sent and printed in the morning of Feb. 15 before the deadline. During the modifying section, the Monkey saw another team's great argue because of a highly experienced team mate. The Monkey found one person of that team was very good in programing, but finally lost the chance to get into touch to him.

4 Conclusion

This is the first time of the Monkey to take part in the Modeling competition. Everything including blip is worth being remembered.

The Monkey has not joined the discussion at first, which may cause the direction mistake and the argue on the third day. But mostly, low experience in modeling, teamwork and English paper writing cause the imperfect performance.

References

[1] COMAP: Mathematics Instructional Resources for Innovative Educators.http://www.comap.com/, 2011. [Online; accessed 18-February-2011].

[2] COMAP: Mathematics Instructional Resources for Innovative Educators.http://www.comap.com/undergraduate/contests/, 2011. [Online; accessed 18-February-2011].

[3] Wikipedia, Half-pipehttp://en.wikipedia.org/wiki/Half-pipe.

2011
02.02

读程序写结果(100分)

#include <string.h>
char* f1(char* a) {
  strcat(a,"乐");
  return a;
}
Imports System.Text
Function f2 (Byval a As String) As String
  f2=a & "快"
End Function
def f3(a)
  return "%s春"%a
#include <iostream>
int main() {
  cout<<f1(f2(f3("新")));
  return 0;
}
2010
12.29
《程序设计方法(C\C++)》是北京理工大学的一门课程,该课通过北京理工大学网络教室评测的编程练习题计算平时成绩。

38 显示学校信息

#include <iostream>
using namespace std;

class person {
	private:
		char name[256];
		int id;
	public:
		person() {
			name[0]='\0';
			id=0;
		}
		virtual void put() {
			cout<<"Name:"<<name<<endl;
			cout<<"Number:"<<id<<endl;
		}
		virtual void get() {
			cin>>name>>id;
		}
};
class student : person {
	private:
		char course[256];
		double grade;
	public:
		student() {
			course[0]='\0';
			grade=0;
		}
		virtual void put() {
			cout<<"Student information:"<<endl;
			person::put();
			cout<<"Course:"<<course<<endl;
			cout<<"Grade:"<<grade<<endl;
		}
		virtual void get() {
			person::get();
			cin>>course>>grade;
		}
};
class teacher : person {
	private:
		char pro[256];
		double paper;
	public:
		teacher() {
			pro[0]='\0';
			paper=0;
		}
		virtual void put() {
			cout<<"Teacher information:"<<endl;
			person::put();
			cout<<"pro:"<<pro<<endl;
			cout<<"paper:"<<paper<<endl;
		}
		virtual void get() {
			person::get();
			cin>>pro>>paper;
		}
};

int main() {
	person p;
	student p1;
	teacher p2;
	p1.get();
	p2.get();
	p1.put();
	cout<<endl;
	p2.put();
	system("pause");
	return 0;
}

39 图书系统

#include<iostream>
#include<string>
using namespace std;
class person {
	private:
		char name[256];
		int age;
	public:
		void setPerson(char* __name,int __age) {
			strcpy(name,__name);
			age=__age;
		}
		char* getName() {
			return name;
		}
		int getAge() {
			return age;
		}
};
class book {
	private:
		int id;
		char name[256];
		char press[256];
		double price;
		person author;
		char print[256];
	public:
		book(int __id,char* __name,char* __press,double __price,person __author,char* __print) {
			id=__id;
			strcpy(name,__name);
			strcpy(press,__press);
			price=__price;
			author=__author;
			strcpy(print,__print);
		}
		void showCard() {
			cout<<"Num:"<<id<<endl;
			cout<<"BookName:"<<name<<endl;
			cout<<"BookConcern:"<<press<<endl;
			cout<<"Price:"<<price<<endl;
			cout<<"AuthorName:"<<author.getName()<<endl;
			cout<<"AuthorAge:"<<author.getAge()<<endl;
			cout<<"PrintTime:"<<print<<endl;
		}
};

int main() {
	char sysname[256];
	cin>>sysname;
	cout<<"SysName:"<<sysname<<endl;
	char name[256],press[256],auname[256],Print[256]="\0",d[256],m[256],y[256];
	int age,id;
	double price;
	cin>>id>>name>>press>>price>>auname>>age;
	cin>>y>>m>>d;
	strcat(Print,y);
	strcat(Print,"-");
	strcat(Print,m);
	strcat(Print,"-");
	strcat(Print,d);
	person author;
	author.setPerson(auname,age);
	book card(id,name,press,price,author,Print);
	card.showCard();
	system("pause");
	return 0;
}

40 求图形面积

注:这道题费了点劲,因为虚函数定义的时候没写函数体,结果出了一堆莫名其妙的错误。

#include <iostream>
#include <cmath>
#define PI 3.14
using namespace std;

class shape {
	protected:
		double x,y;
	public:
		shape(double __x=0,double __y=0) {
			x=__x;
			y=__y;
		}
		virtual double getSquare() {
			return 0;
		};
		virtual char* getType() {
			return "shape";
			return 0;
		};
		friend ostream & operator<<(ostream & output,shape * obj);
};
class triangle : public shape {
	public:
		triangle(double __x=0,double __y=0):shape(__x,__y) { }
		double getSquare() {
			return x*y/2;
		}
		char* getType(){
			return "triangle";
		}
};
class rectangle : public shape {
	public:
		rectangle(double __x=0,double __y=0):shape(__x,__y){ }
		virtual double getSquare() {
			return x*y;
		}
		char* getType(){
			return "rectangle";
		}
};
class circle : public shape {
	public:
		circle(double __x=0,double __y=0):shape(__x,__y){ }
		virtual double getSquare() {
			return pow(x,2)*PI;
		}
		char* getType(){
			return "cirole";
		}
};

ostream & operator<<(ostream & output,shape * obj) {
	output<<obj->getType()<<"Area=";
	output<<obj->getSquare()<<endl;
	return output;
}

int main() {
	double x,y;
	cin>>x>>y;
	shape * s1=new triangle(x,y);
	shape * s2=new rectangle(x,y);
	shape * s3=new circle(x,y);
	cout<<s1<<s2<<s3;
	system("pause");
	return 0;
}
2010
12.27
《程序设计方法(C\C++)》是北京理工大学的一门课程,该课通过北京理工大学网络教室评测的编程练习题计算平时成绩。

34 求两点间距离

注:最开始的时候我在getDistance(...)这个友元函数的定义上费了很大的劲,我想把一个模板函数定义成一个模板类的友元,但是似乎不行,只好在定义函数的时候把它实例化。

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

template <class Tp>
	class CPoint {
		private:
			Tp x,y;
		public:
            CPoint(Tp _x,Tp _y) {
                x=_x;
                y=_y;
            }
			Tp getX() {
				return x;
			}
			Tp getY() {
				return y;
			}
			friend Tp getDistance(CPoint<Tp> a,CPoint<Tp> b);
	};

double getDistance(CPoint<double> a,CPoint<double> b) {
    return sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2));
}

int main() {
	double x,y;
	cin>>x>>y;
	CPoint <double>a(x,y);
	cin>>x>>y;
	CPoint <double>b(x,y);
	cout<<"Distance="<<getDistance(a,b)<<endl;
	return 0;
}

35 统计单词个数

注:这道题我考虑得挺多的,结果交了才发现数据给的很简单,我考虑了多重空格、标点前后的空格(通过真正地鉴别单词的个数而不是通过空格),其实还没考虑缩音词。

#include <iostream>
#include <iomanip>

using namespace std;

class Counter{
    private:
        int _c;
        char lastChar,thisChar;
        bool isChar(char c) {
            return (c<='Z'&&c>='A')||(c<='z'&&c>='a')||c=='\'';
        }
    public:
        Counter() {
            _c=0;
            lastChar=' ';
        }
        void countWord() {
            while (lastChar!='\n') {
                thisChar=cin.get();
                if (isChar(thisChar)&&!isChar(lastChar))
                    _c++;
                lastChar=thisChar;
            }
        }
        void show() {
            cout<<"Words="<<_c<<endl;
        }
};

int main() {
    Counter counter;
    counter.countWord();
    counter.show();
    return 0;
}

36 显示时间

注:这个题要容错,于是我就专门写了个容错函数,谁让我懒呢。

#include <iostream>
using namespace std;
template <class Tp>
	class Time {
		private:
			Tp hour,minute,second;
			Tp fixByRange(Tp __data,Tp __floor,Tp __ceil) {
				if (__data<=__ceil&&__data>=__floor)
					return __data;
				else
					return 0;
			}
		public:
			Time() {
				hour=0;
				minute=0;
				second=0;
			}
			void setHour(Tp __hour) {
				hour=fixByRange(__hour,0,23);
			}
			void setMinute(Tp __minute) {
				minute=fixByRange(__minute,0,59);
			}
			void setSecond(Tp __second) {
				second=fixByRange(__second,0,59);
			}
			void setTime() {
				Tp tmpC;
				cin>>tmpC;
				setHour(tmpC);
				cin>>tmpC;
				setMinute(tmpC);
				cin>>tmpC;
				setSecond(tmpC);
			}
			void print() {
				cout<<hour<<":"<<minute<<":"<<second<<endl;
			}
		};

int main() {
	Time<int> objTime;
	objTime.setTime();
	objTime.print();
	system("pause");
	return 0;
}

37 整数集合运算

核心思想:这道题写得有点累,重载其实在外面也可以,只是后来懒得再写了。

#include <iostream>
using namespace std;
template <class Tpi,class Tp>
	class Set {
		private:
			Tp array[1000];
			Tpi length;
		public:
			Set(Tp __length=0) {
				//array[__length];
				length=__length;
				Tpi i;
				for (i=0;i<__length;i++)
					array[i]=0;
			}
			void get() {
				cin>>length;
				Tpi i;
				for (i=0;i<length;i++)
					cin>>array[i];
			}
			void put() {
				Tpi i;
				for (i=0;i<length;i++)
					cout<<array[i]<<" ";
				cout<<endl;
			}
			Tpi getLength() {
				return length;
			}
			/*Set<Tpi,Tp> operator+(Set<Tpi,Tp> & __Set) {
				Tpi i,j;
				Set<int,int> ret=this;
				for (j=0;j<__Set.length;j++) {
					for (i=0;i<ret.length;i++)
						if (ret.array[i]!=__Set.array[j])
							break;
					ret.array[length++]=__Set.array[j];
				}
				return ret;
			}*/
			friend Set<Tpi,Tp> operator+(Set<Tpi,Tp> & _s1,Set<Tpi,Tp> & _s2) {
				int i,j;
				Set<Tpi,Tp> ret;
				for (j=0;j<_s1.length;j++) {
					for (i=0;i<ret.length;i++)
						if (ret.array[i]==_s1.array[j])
							break;
					if (i==ret.length)
						ret.array[ret.length++]=_s1.array[j];
				}
				for (j=0;j<_s2.length;j++) {
					for (i=0;i<ret.length;i++)
						if (ret.array[i]==_s2.array[j])
							break;
					if (i==ret.length)
						ret.array[ret.length++]=_s2.array[j];
				}
				return ret;
			}
			friend Set<Tpi,Tp> operator-(Set<Tpi,Tp> & _s1,Set<Tpi,Tp> & _s2) {
				int i,j;
				Set<Tpi,Tp> ret;
				for (i=0;i<_s1.length;i++) {
					for (j=0;j<_s2.length;j++)
						if (_s1.array[i]==_s2.array[j])
							break;
					if (j==_s2.length)
						ret.array[ret.length++]=_s1.array[i];
				}
				return ret;
			}
	};
/*template <class Tpi,class Tp>
	Set<Tpi,Tp> operator+(Set<Tpi,Tp> & _s1,Set<Tpi,Tp> & _s2) {
		int i,j;
		Set<Tpi,Tp> ret();
		for (j=0;j<_s2.length;j++) {
			for (i=0;i<_s1.length;i++)
				if (_s1.array[i]!=_s2.array[j])
					break;
			ret.array[ret.length++]=_s2.array[j];
		}
		return ret;
	}
template <class Tpi,class Tp>
	Set<Tpi,Tp> operator-(Set<Tpi,Tp> & _s1,Set<Tpi,Tp> & _s2) {
		int i,j;
		Set<Tpi,Tp> ret();
		for (i=0;i<_s1.length;i++) {
			for (j=0;j<_s2.length;j++)
				if (_s1.array[i]==_s2.array[j])
					break;
			ret.array[ret.length++]=_s1.array[i];
		}
		return ret;
	}*/

int main() {
	Set<int,int> s1,s2;
	s1.get();
	s2.get();
	Set<int,int> ret1=s1+s2;
	Set<int,int> ret2=s1-s2;
	cout<<"int1+int2=";
	ret1.put();
	cout<<"int1-int2=";
	ret2.put();
	system("pause");
	return 0;
}
2010
12.26
《程序设计方法(C\C++)》是北京理工大学的一门课程,该课通过北京理工大学网络教室评测的编程练习题计算平时成绩。

这次不涉及什么算法,都是模拟策略。

29 显示时间

注:这道题应该是出错了,时间都是显示成0:00:00这样的形式的,可以在测试数据里竟然有0:0:0这样的答案,导致我多提交了一次。


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

template<class Tp>
	class typTime {
		private:
			Tp hour,minute,second;
		public:
			typTime() {
				hour=0;
				minute=0;
				second=0;
			}
			typTime(typTime<Tp> &_t) {
				hour=_t.hour;
				minute=_t.minute;
				second=_t.second;
			}
			void settime(Tp _hour,Tp _minute,Tp _second) {
				hour=_hour;
				minute=_minute;
				second=_second;
			}
			void show() {
				cout<<hour<<':'<<setfill('0')/*<<setw(2)*/<<minute<<':'/*<<setw(2)*/<<second<<endl;
			}
	};

int main() {
	int h,m,s;
	cin>>h>>m>>s;
	typTime<int> Time;
	Time.settime(h,m,s);
	Time.show();
	//system("pause");
	return 0;
}

30 求三个数中的量大值

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

template<class Tp>
	Tp max(Tp _a,Tp _b,Tp _c) {
		Tp _t=_a;
		if (_t<_b)
			_t=_b;
		if (_t<_c)
			_t=_c;
		return _t;
	}

int main() {
	int a,b,c;
	cin>>a>>b>>c;
	cout<<"Maximum:"<<max(a,b,c)/*<<"."*/<<endl;
	//system("pause");
	return 0;
}

31 分解5位整数

注:这道题本来我是写成了边读边输出的形式,结果不知道为什么输出结果反序了,可能是由于"<<"运算是右结合吧。 还有更囧的,就是题上说五位整数,我就真当成了五位整数,结果其实它是低于五位的整数。

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

int main() {
	int i;
	char c;
	for (i=0;i<5;i++) {
		c=cin.get();
		if (c=='\n')
			break;
		else {
			if (i)
				cout<<" ";
			cout<<(char)c;
		}
	}
	cout<<endl;
	return 0;
}

32 求圆柱体体积

#include <iostream>
#include <iomanip>
#define PI 3.14
using namespace std;

template<class Tp,class Tp2>
	class theStuff{
		private:
			Tp _r,_h;
			Tp2 _s,_v;
		public:
			theStuff(Tp __r,Tp __h){
				set(__r,__h);
			}
			void set(Tp __r,Tp __h) {
				_r=__r;
				_h=__h;
				_s=_r*_r*PI;
				_v=_s*_h;
			}
			Tp volume() {
				return _v;
			}
			Tp Square() {
				return _s;
			}
	};

int main() {
	double r,h;
	cin>>r>>h;
	theStuff<double,double> stuff(r,h);
	cout<<"v="<<stuff.volume()<<endl;
	system("pause");
	return 0;
}

33 设置、输出日期

注:这道题更恶心,输出的日期竟然是1900/1/1/形式的……

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

template <class Tp>
	class Date{
		private:
			Tp year,month,day;
		public:
			Date(){
				year=1900;
				month=1;
				day=1;
			}
			Date(Tp y,Tp m,Tp d) {
				SetDate(y,m,d);
			}
			void SetDate(Tp y,Tp m,Tp d) {
				year=y;
				month=m;
				day=d;
			}
			Tp GetDay() {
				return day;
			}
			Tp GetMonth() {
				return month;
			}
			Tp GetYear() {
				return year;
			}
			void ShowDate() {
				cout<<year<<"/"<<month<<"/"<<day<<"/"<<endl;
			}
	};

int main() {
	int y,m,d;
	cin>>y>>m>>d;
	Date<int> date(y,m,d);
	date.ShowDate();
	return 0;
}