博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

ACM Steps Chapter One

Posted on 2011-04-25 11:02  ¥忘%风  阅读(1608)  评论(0编辑  收藏  举报

ACM Steps @ HDOJ

HDOJ最近新推出的ACM Steps功能,支持一下。

虽然已经不再参加ACM/ICPC竞赛了,平时也不太做题了。

  :现在就偶尔逛一下HDOJ,偶尔有兴致找几道简单易读的题写一下,练一下,

    老菜鸟一个,不再以参赛为目的。

ACM Steps很好的一个功能,打算从Chapter One开始记录一下自己的ACM Steps路程。

记录一些代码,这样也能方便自己查看。

  :pass 因为不经常做了,所以可能会比较长一段时间才记录一次,呵呵。

 

ACM Steps题库是基础题库+随机题库, 所以不同ID见到的题库可能不一样(随机题库部分)。

所以我记录的只是我个人看到的题库。

  :pass 最近一段时间OJ咋经常上不去yo。


昨天参加了腾讯实习生杭州站搜索开发的第一轮面试,唉,基础弱,各种不会,饿补ing。

也是第一次参加面试,处女面就这样献给扣扣了。攒经验为主,希望有个好的开始呗。 

---------------------------------------------------------------------------------

Forget-Wind's Chapter One: 

第一章节,基本是比较简单的一些题,所以本章节基本只记录代码,不做描述性的解题报告。

1.1.1 - 1.1.8 基本输入输出

1.2.1 Elevator    -----------  HDOJ 1008 (对应的外部题库链接,下面直接链接到对应的外部题库)

 

#include <stdio.h>
int main() {
int n, pref, floor, sum;
while (scanf("%d", &n) == 1) {
if (n == 0) break;
pref
= 0;
sum
= 0;
for (int i = 0; i < n; i++) {
scanf(
"%d", &floor);
if (floor > pref) {
sum
+= 6 * (floor - pref);
}
else {
sum
+= 4 * (pref - floor);
}
pref
= floor;
sum
+= 5;
}
printf(
"%d\n", sum);
}
}


1.2.2 Biker's Trip Odometer

 

#include <stdio.h>
#define pi 3.1415927
#define c_mile (5280 * 12.0)
#define c_ti (60 * 60)
int main() {
double dia, rev, ti;
int cas = 0;
while (scanf("%lf %lf %lf", &dia, &rev, &ti) == 3) {
if (rev == 0) break;
double total = pi * dia * rev / c_mile;
ti
/= c_ti;
double ave = total / ti;
printf(
"Trip #%d: ", ++cas);
printf(
"%.2lf %.2lf\n", total, ave);
}
return 0;
}



1.2.3 Climbing Worm

 

#include <stdio.h>
int main() {
int n, u, d;
while (scanf("%d %d %d", &n, &u, &d) != EOF) {
if (n == 0 && u == 0 && d == 0) break;
if ((n - u) % (u - d) == 0)
printf(
"%d\n", 2 * (n - u) / (u - d) + 1);
else
printf(
"%d\n", 2 * ((n - u) / (u - d)) + 3);
}
return 0;
}


1.2.4 Rightmost Digit

(二分求幂,很久没写了,所以非递归形式的忘了怎么写,就循着思路写了个递归形式的二分加速。

当然,这题也可以通过找规律去做。)

 

#include <stdio.h>
int binary_exp(int base, int exp) {
if (exp == 0)
return 1;
if (exp & 1) {
return (base * binary_exp(base, exp - 1)) % 10;
}
else {
exp
>>= 1;
int tmp = binary_exp(base, exp) % 10;
return (tmp * tmp) % 10;
}
}

int main() {
int test, n;
scanf(
"%d", &test);
while (test--) {
scanf(
"%d", &n);
int base = n % 10, exp = n;
printf(
"%d\n", binary_exp(base, exp));
}
return 0;
}

 
1.2.5 Quicksum

 

#include <stdio.h>
int main() {
char str[300];
while (gets(str)) {
if (str[0] == '#') break;
int sum = 0;
for (int i = 0; str[i]; i++) {
if (str[i] != ' ')
sum
+= (str[i] - 'A' + 1) * (i + 1);
}
printf(
"%d\n", sum);
}
return 0;
}


1.2.6 Balloon Comes!

 

#include <stdio.h>
void Print(double a, double b, char op) {
double sum = 0.0;
if (op == '+') {
sum
= a + b;
}
else if (op == '-') {
sum
= a - b;
}
else if (op == '*') {
sum
= a * b;
}
else {
sum
= a / b;
}
if (sum == (int)sum) {
printf(
"%d\n", (int)sum);
}
else {
printf(
"%.2lf\n", sum);
}
}

int main() {
int test;
scanf(
"%d", &test);
while (test--) {
char op[2];
double a, b;
scanf(
"%s %lf %lf", op, &a, &b);
Print(a, b, op[
0]);
}
return 0;
}


1.2.7 Identity Card

 

#include <cstdio>
#include
<cstring>
#include
<string>
using namespace std;
string name[] = {"Zhejiang", "Beijing", "Taiwan", "Hong Kong",
"Macao", "Tibet", "Liaoning", "Shanghai"};
string id[] = {"33", "11", "71", "81", "82", "54", "21", "31"};

int main() {
int test;
char str[50];
scanf(
"%d", &test);
while (test--) {
scanf(
"%s", str);
printf(
"He/She is from ");
for (int i = 0; i < 8; i++) {
if (strncmp(str, id[i].c_str(), 2) == 0) {
printf(
"%s", name[i].c_str());
}
}
printf(
",and his/her birthday is on ");
printf(
"%c%c,%c%c,%c%c%c%c",
str[
10], str[11], str[12], str[13], str[6], str[7], str[8], str[9]);
printf(
" based on the table.\n");
}
return 0;
}


1.2.8 AC Me

 

#include <cstdio>
#include
<cstring>
using namespace std;
char str[100010];
int num[250];

int main() {
while (gets(str)) {
memset(num,
0, sizeof(num));
for (int i = 0; str[i]; i++) {
num[str[i]]
++;
}
for (int i = 'a'; i <= 'z'; i++) {
printf(
"%c:%d\n", i, num[i]);
}
puts(
"");
}
return 0;
}

---------------------------------------------------------------------------------

1.3 之后开始接触C++标准程序库自修教程这么书,所以可能开始去用到一些STL。

  以前自己做ACM基本是不会用也不去用。1.3小节基本是一些简单的贪心题。


1.3.1 FatMouse' Trade (贪心)

 

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

struct FatMouse {
int javaBean;
int catFood;
double perFood;

FatMouse(
int a, int b, double c)
: javaBean(a), catFood(b), perFood(c) {}
bool friend operator < (const FatMouse &a, const FatMouse &b) {
return a.perFood > b.perFood;
}
};
vector
<FatMouse> fatmouse;

int main() {
int m, n, a, b;

while (cin >> m >> n) {
if (m < 0 && n < 0)
break;

for (int i = 0; i < n; ++i) {
cin
>> a >> b;
fatmouse.push_back(FatMouse(a, b, a
* 1.0 / b));
}
sort(fatmouse.begin(), fatmouse.end());
double ret = 0.0, leftFood = m;
for (int i = 0; i < n; ++i) {
if (leftFood >= fatmouse[i].catFood) {
ret
+= fatmouse[i].javaBean;
leftFood
-= fatmouse[i].catFood;
}
else {
ret
+= leftFood * fatmouse[i].perFood;
break;
}
}
cout
<< setiosflags(ios::fixed) << setprecision(3) << ret << endl;
fatmouse.clear();
}
return 0;
}


1.3.2 Moving Tables (找最大重叠数)

#include <iostream>
#include
<vector>
#include
<algorithm>
using namespace std;
#define RR(x) (((x) + 1) >> 1)
struct Room {
int corridor1;
int corridor2;
Room(
int a, int b) : corridor1(RR(a)), corridor2(RR(b)) {
if (corridor1 > corridor2) {
corridor1
^= corridor2;
corridor2
^= corridor1;
corridor1
^= corridor2;
}
}
};

int main() {
int test, n,a , b;
cin
>> test;
while (test--) {
cin
>> n;
vector
<int> hash(201);
for (int i = 0; i < n; ++i) {
cin
>> a >> b;
Room tmp(a, b);
for (int j = tmp.corridor1; j <= tmp.corridor2; ++j) {
hash[j]
++;
}
}
vector
<int>::iterator pos;
pos
= max_element(hash.begin(), hash.end());
cout
<< *pos * 10 << endl;
}
return 0;
}


1.3.3 今年暑假不AC (贪心,按结束时间排序,仿函数做排序准则)

#include <iostream>
#include
<vector>
#include
<algorithm>
using namespace std;

struct TV {
int st;
int ed;
TV(
int a, int b) : st(a), ed(b) {}
};

class cmp {
public:
bool operator() (const TV &a, const TV &b) const {
return a.ed < b.ed;
}
};
vector
<TV> show;

int main() {
int n, a, b;
while (cin >> n, n) {
for (int i = 0; i < n; i++) {
cin
>> a >> b;
show.push_back(TV(a, b));
}
sort(show.begin(), show.end(), cmp());
int cnt = 1;
vector
<TV>::const_iterator pre = show.begin();
vector
<TV>::const_iterator pos = show.begin() + 1;
for ( ; pos != show.end(); ++pos) {
if (pos->st >= pre->ed) {
cnt
++;
pre
= pos;
}
}
cout
<< cnt << endl;
show.clear();
}
return 0;
}

 
1.3.4 百步穿杨(简单模拟)

#include <iostream>
#include
<vector>
#include
<algorithm>
using namespace std;

struct Arrow {
int length;
int cnt;
Arrow(
int a, int b) : length(a), cnt(b) {}
};

class cmp {
public:
bool operator() (const Arrow &a, const Arrow &b) const {
return a.length < b.length;
}
};

vector
<Arrow> arrow;

int main() {
int test, n, a, b;
cin
>> test;
while (test--) {
cin
>> n;
for (int i = 0; i < n; i++) {
cin
>> a >> b;
arrow.push_back(Arrow(a, b));
}
sort(arrow.begin(), arrow.end(), cmp());
vector
<Arrow>::const_iterator pos;
for (pos = arrow.begin(); pos != arrow.end(); ++pos) {
for (int i = 0; i < pos->cnt; i++) {
cout
<< ">+";
for (int j = 0; j < pos->length - 2; j++) {
cout
<< "-";
}
cout
<< "+>" << endl;
}
cout
<< endl;
}
arrow.clear();
}
}


1.3.5 第二小整数 (用了stl的nth_element)

#include <iostream>
#include
<vector>
#include
<algorithm>
using namespace std;

int main() {
int test, n, a;
vector
<int> arr;
cin
>> test;
while (test--) {
cin
>> n;
for (int i = 0; i < n; i++) {
cin
>> a;
arr.push_back(a);
}

nth_element(arr.begin(), arr.begin()
+ 1, arr.end());
cout
<< *(arr.begin() + 1) << endl;
arr.clear();
}
return 0;
}


1.3.6 考试排名 (排序)

#include <iostream>
#include
<cstdio>
#include
<vector>
#include
<algorithm>
using namespace std;

struct Rank {
char name[10];
int cnt;
int ti;
void clear() {
cnt
= 0;
ti
= 0;
}
};
vector
<Rank> stu;

class cmp {
public:
bool operator() (const Rank &a, const Rank &b) const {
if (a.cnt > b.cnt) {
return true;
}
else if (a.cnt == b.cnt) {
if (a.ti < b.ti) {
return true;
}
else if (a.ti == b.ti) {
return strcmp(a.name, b.name) < 0;
}
return false;
}
return false;
}
};

int main() {
int n, m, a, b;
char str[10];
scanf(
"%d %d", &n, &m);
Rank current;
while (scanf("%s", current.name) != EOF) {
current.clear();
for (int j = 0; j < n; ++j) {
scanf(
"%s", str);
int num = sscanf(str, "%d(%d)", &a, &b);
if (num == 1) {
if (a > 0) {
current.cnt
++;
current.ti
+= a;
}
}
else {
current.cnt
++;
current.ti
+= a + b * m;
}
}
stu.push_back(current);
}
sort(stu.begin(), stu.end(), cmp());
vector
<Rank>::const_iterator pos;
for (pos = stu.begin(); pos != stu.end(); ++pos) {
printf(
"%-10s %2d %4d\n", pos->name, pos->cnt, pos->ti);
}
stu.clear();
return 0;
}


1.3.7 Saving HDU (贪心)

#include <iostream>
#include
<vector>
#include
<algorithm>
using namespace std;

struct Treasure {
int per_money;
int volume;
Treasure(
int a, int b) : per_money(a), volume(b) {}
};

class cmp {
public:
bool operator() (const Treasure &a, const Treasure &b) const {
return a.per_money > b.per_money;
}
};
vector
<Treasure> tsure;

int main() {
int v, n, a, b;
while (cin >> v, v) {
cin
>> n;
for (int i = 0; i < n; ++i) {
cin
>> a >> b;
tsure.push_back(Treasure(a, b));
}
sort(tsure.begin(), tsure.end(), cmp());
vector
<Treasure>::iterator pos;
int ret = 0;
for (pos = tsure.begin(); pos != tsure.end(); ++pos) {
if (v >= pos->volume) {
v
-= pos->volume;
ret
+= pos->volume * pos->per_money;
}
else {
ret
+= v * pos->per_money;
break;
}
}
cout
<< ret << endl;
tsure.clear();
}
return 0;
}

 
1.3.8 Wooden Sticks (贪心)

#include <iostream>
#include
<vector>
#include
<algorithm>
using namespace std;

struct Wood {
int length;
int weight;
Wood(
int a, int b) : length(a), weight(b) {}
bool friend operator <= (const Wood &a, const Wood &b) {
if (a.length <= b.length && a.weight <= b.weight) {
return true;
}
return false;
}
};

class cmp {
public:
bool operator() (const Wood &a, const Wood &b) const {
if (a.length < b.length) {
return true;
}
else if (a.length == b.length) {
return a.weight < b.weight;
}
return false;
}
};
vector
<Wood> wood;
vector
<bool> hash;

int main() {
int test, n, a, b;
cin
>> test;
while (test--) {
cin
>> n;
hash.resize(n);
for (int i = 0; i < n; ++i) {
cin
>> a >> b;
wood.push_back(Wood(a, b));
}
sort(wood.begin(), wood.end(), cmp());
int cnt = 0;
for (int i = 0; i < n; ++i) {
if (!hash[i]) {
Wood tmp
= wood[i];
for (int j = i; j < n; ++j) {
if (!hash[j] && tmp <= wood[j]) {
tmp
= wood[j];
hash[j]
= true;
}
}
cnt
++;
}
}
cout
<< cnt << endl;
wood.clear();
hash.clear();
}
return 0;
}

原创文章如转载请注明:转自¥忘%风 {http://www.cnblogs.com/slave_wc}

本文地址: ACM Steps Chapter One

     {http://www.cnblogs.com/slave_wc/archive/2011/04/25/2026612.html }