当前角色:竞赛选手
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;
}
2010
12.05
《程序设计方法(C\C++)》是北京理工大学的一门课程,该课通过北京理工大学网络教室评测的编程练习题计算平时成绩。

这次的程序里面用了一下C++的语法,如果同学看不太懂请只看核心思想即可,发的有点晚了,不好意思。

25 求最大公约数——递归

核心思想:即辗转相除法,利用每次互相取余数的方法保证方程一直是同余下等价的,具体的证明请参阅辗转相除法正确性的证明

#include <stdio.h>
#include <iostream>
using namespace std;

template <class Tp>
	Tp gcd(Tp a,Tp b) {
		if (a<b)
			swap(a,b);
		if (!b)
			return a;
		a=a%b;
		return gcd(a,b);
	}

int main() {
    int n,m;
    scanf("%d %d",&n,&m);
    printf("%d\n",gcd(n,m));
    //system("pause");
    return 0;
}

26 回文字符串——递归

核心思想:这个其实跟用循环一样,只是不断的把对一个字符串判断是否回文化归为判断其删去首尾是否回文的子问题。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

bool isPalindrome(char *__str,int __i) {
	if (__i>=(strlen(__str)>>1))
		return true;
	if (__str[__i]==__str[strlen(__str)-__i-1])
		return isPalindrome(__str,__i+1);
	else
		return false;
}
int main() {
	char theStr[256];
	int i;
	gets(theStr);
	//printf("%d\n",strlen(theStr));
	if (isPalindrome(theStr,0))
		printf("Yes\n");
	else
		printf("No\n");
	//system("pause");
	return 0;
}

27 求序列之和——递归

核心思想:其实就是拿递归当循环用,没有什么新的,需要避免的就是递归结束的逻辑,不要弄成无限递归了。

#include <stdlib.h>
#include <stdio.h>

int n;

float cal(int __i) {
	if (__i>n)
		return 0;
	float __ret=(float)1/__i;
	if (__i&1)
		__ret=-__ret;
	if (__i==1)
		__ret=-__ret;
	return cal(__i+1)+__ret;
}

int main() {
	float ret;
	scanf("%d",&n);
	if (n==1)
		printf("%d\n",1);
	else
		printf("%.6f\n",cal(1));
	//system("pause");
	return 0;
}

28 子串反向——递归

核心思想:这道题放了一个小小的烟幕,其实程序要过还是得写main()函数的。程序跟第26题一样,将问题不断化归成为子问题。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

template <class Tp>
	void swap(Tp& __a,Tp& __b) {
		Tp t;
		t=__a;
		__a=__b;
		__b=t;
	}
void reverse(char str[], int start, int end) {
	if (start>=end) {
		puts(str);
		return;
	}
	swap(str[start],str[end]);
	reverse(str,start+1,end-1);
}
int main() {
	char str[50];
	int a,b;
	gets(str);
	scanf("%d %d",&a,&b);
	reverse(str,a,b);
	//system("pause");
	return 0;
}
2010
11.27
《程序设计方法(C\C++)》是北京理工大学的一门课程,该课通过北京理工大学网络教室评测的编程练习题计算平时成绩。

21 合并字符串

核心思想:依次将两个字符串存储到数组中,并利用归并排序思想进行输出。也就是每次比较两个字符串中最靠前还没有被输出的字符,输出最小/大的,然后将对应字符串指向还没有输出字符的标记变量向后移动,一直到两个字符串都输出完为止。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main() {
	char str[2][256];
	int i,j;
	gets(str[0]);
	gets(str[1]);
	i=0;
	j=0;
	while (i<strlen(str[0])
		   || j<strlen(str[1])) {
		if (i==strlen(str[0])) {
			putchar(str[1][j++]);
			continue;
		}
		if (j==strlen(str[1])) {
			putchar(str[0][i++]);
			continue;
		}
		if (str[0][i]<str[1][j])
			putchar(str[0][i++]);
		else
			putchar(str[1][j++]);
	}
	if (strlen(str[0])+strlen(str[1]))
		printf("\n");
	return 0;
}

22 串的减法

核心思想:对于被减的字符串s[],依次从头到尾遍历各个字符s[i],对于遍历到的每一个字符都在减数字符串t[]中寻找是否出现,如果没有出现则将s[i]输出出来。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
	char s[256],t[256];
	int i,j,k;
	gets(s);
	gets(t);
	for (i=0;i<strlen(s);i++) {
		for (j=0;j<strlen(t);j++)
			if (s[i]==t[j])
				break;
		if (j==strlen(t))
			putchar(s[i]);
	}
	printf("\n");
	return 0;
}

23 整数问题

有若干同学要看我的2行程序代码,我就把它贴了出来。其实这道题我当初做的时候用的也是最容易想到的枚举数字的想法,结果在n=8的时候就成功的超时了,可是该点在本地运行大约2秒就可以跑出解,再加上这道题数据量不大、我又不想重新写正经的算法了,于是就将所有的结果算出来,存入了程序中。既然是乱搞出来的程序,就干脆乱搞到底算了,于是我就把所有的回车都删掉了……大家不要学我。

#include <stdio.h>
int main() {int i,j,k,n,start,end,ret;int form[9]={0,9,22,24,16,7,7,1,0};scanf("%d",&n);ret=form[n];printf("%d\n",ret);return 0;}

这个是正经的程序。

核心思想:由于每前i位,都被要求能被i^2整除。而对n=k时的结果来说,前k-1位数字一定包含了当n=k-1时的结果们(该词语出《高等代数》课程上的“坐标们”),因此就可以由小的位数不断的推到高的位数。

下面的程序中,使用了递归的手段来实现上述递推的过程,其中theNumber是对于len位的数字的一个可以成为解的数,值得注意的是,这个数字在被传入函数了以后才被判断是否是可行的。当其可行的时候,再枚举下一位。len位的这个具体数字对应的解的个数,就是其枚举的各种下一位的解的个数和,也就是ret(theNumber)=\sum_{i=0}^9{ret(10theNumber+i)}.

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int calSum(int theNumber,int len,int n) {
    if (len&&theNumber%(len*len))
        return 0;
    if (len==n)
        return 1;
    int ret,i;
    ret=0;
    for (i=0;i<=9;i++)
        if (len||i)
            ret+=calSum(theNumber*10+i,len+1,n);
    return ret;
}

int main() {
	int i,j,k,n,start,end,ret;
	scanf("%d",&n);
    printf("%d\n",calSum(0,0,n));
    return 0;
}

24 科学记数法

核心思想:_strtol(char* str)函数是用来将传入的字符串的每一个字符转化为整型数字的函数,由于系统函数不是标准函数,在GCC评测环境下无法运行,所以自己写了一个。

程序的想法类似于我们学过的浮点数,利用_strtol()将前面的有效位数存入数组val[]中,将指数转入ex[]中,同时还将小数点位置存入了end变量中,之后将ex[]转换整型数字,然后与end做运算,算出结果的小数点位置。最后据此补零、加小数点并输出val[]内的数字。

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int _strtol(char* str) {
	int i,j=0;
	for (i=0;i<strlen(str);i++)
		if (str[i]>='0'&&str[i]<='9')
			j=j*10+(str[i]-'0');
	if (str[0]=='-')
		j=-j;
	return j;
}

int main() {
    int i,j,k,n;
    int flag=0;
    char theNumber[256],val[256],ex[256];
    int start=0,end=-1;
    double a;
    gets(theNumber);
    j=0;
    k=0;
    for (i=0;i<=strlen(theNumber);i++)
        switch (theNumber[i]) {
        	case '.':
				end=j;
				break;
			case 'E':
			case 'e':
				flag=1;
			case ' ':
				break;
			default:
				if (flag)
					ex[k++]=theNumber[i];
				else
					val[j++]=theNumber[i];
				break;
		}
	if (end==-1)
		end=j;
	ex[k]='\0';
	n=_strtol(ex);
	end+=n;
	start+=n;
	if (start>0)
		start=0;
	for (i=start;i<end+8;i++) {
		if (i==end)
			printf(".");
		if (i<j&&i>=0)
			printf("%c",val[i]);
		else
			printf("%d",0);
	}
	printf("\n");
    return 0;
}
2010
11.21
《程序设计方法(C\C++)》是北京理工大学的一门课程,该课通过北京理工大学网络教室评测的编程练习题计算平时成绩。

18 删除重复字符

核心思想:从前往后扫描已知字符串,对于每一个字符再在其以前寻找是否存在重复。如果不存在,则将此字符输出,否则不输出。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
    char str[101];
    int i,j;
    gets(str);
    for (i=0;i<strlen(str);i++) {
        for (j=0;j<i;j++)
            if (str[i]==str[j])
               break;
        if (i==j)
           printf("%c",str[i]);
    }
    if (strlen(str))
       printf("\n");
    //system("pause");
    return 0;
}

19 单词排序

核心思想:冒泡排序,判断的时候只用字符串比较函数strcmp(str1,str2).

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
    char str[5][256];
    int t[5]={0,1,2,3,4};
    int i,j,k;
    for (i=0;i<5;i++)
        gets(str[i]);
    for (i=0;i<5;i++)
        for (j=i;j<5;j++)
            if (strcmp(str[t[i]],str[t[j]])<0) {
               k=t[i];
               t[i]=t[j];
               t[j]=k;
            }
    for (i=0;i<5;i++)
        printf("%s\n",str[t[i]]);
    //system("pause");
    return 0;
}

20 大数分解

核心思想:2开始,从小到大测试是否能够整除已知数,如果能够整除则下一次还试此数,否则换下一个数。如果测试的因子不是质因子,则其质因子一定比其小,因此一定在前面就没测试出来,故测试出来的所有因子都是质因子。

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main() {
    int i,j,k;
    int n;
    scanf("%d",&n);
    i=2;
    while(n!=1) {
		if (n%i)
			i++;
		else {
			printf("%d\n",i);
			n/=i;
		}
	}
	//system("pause");
	return 0;
}
2010
11.13
《程序设计方法(C\C++)》是北京理工大学的一门课程,该课通过北京理工大学网络教室评测的编程练习题计算平时成绩。

15 谁能出线

核心思想:搜索最大的数字,输出下标。


#include <stdio.h>

int main() {
    int scores[10];
    int maxScore,i;
    maxScore=0;
    for (i=0;i<10;i++) {
        scanf("%d",&scores[i]);
        if (maxScore<scores[i])
            maxScore=scores[i];
    }
    for (i=0;i<10;i++)
        if (scores[i]==maxScore)
            printf("%d\n",i);
    return 0;
}

16 等值数列段

核心思想:枚举起点,对于每个起点找尽可能远的等值数列,然后与已知最大的比较。

#include <stdio.h>

int main() {
    int i,j,n;
    int start,end,len;
    len=0;
    scanf("%d",&n);
    int seq[n];
    for (i=0;i<n;i++)
        scanf("%d",&seq[i]);
    for (i=0;i<n;i++) {
        for (j=i+1;j<n;j++)
            if (seq[i]!=seq[j])
                break;
        if (len<j-i) {
            start=i;
            end=j-1;
            len=j-i;
        }
    }
    if (len>1)
        printf("The longest equal number list is from %d to %d.\n",start,end);
    else
        printf("No equal number list.\n");
    return 0;
}

17 大家一起做游戏

核心思想:模拟题中叙述的过程,使用一个变量不断地轮流地扫描这N的人,使用一个数组记录其是否被踢出,并用另一个变量统计其在每一轮中实际的序号(即此变量只在扫到没被踢出的元素是自增)。

#include <stdio.h>

int main() {
    int i,j,k,n,number;
    scanf("%d %d",&n,&number);
    int children[n];
    for (i=0;i<n;i++)
        children[i]=i+1;
    k=n-1;
    for (i=1;i<n;i++) {
        j=0;
        while (j<number) {
            k++;
            k%=n;
            if (children[k])
                j++;
        }
        children[k]=0;
    }
    for (i=0;i<n;i++)
        if (children[i])
            printf("The left child is NO %d.\n",i+1);
    return 0;
}