??xml version="1.0" encoding="utf-8" standalone="yes"?>久久综合综合久久97色,色婷婷综合久久久久中文一区二区,亚洲精品国产第一综合99久久http://cppblog.com/sixleaves/category/20814.html重剑无锋 大y不工zh-cnMon, 13 Apr 2015 03:44:39 GMTMon, 13 Apr 2015 03:44:39 GMT60UVA11988 Broken KeyBoardhttp://www.shnenglu.com/sixleaves/archive/2015/04/12/210311.htmlswpswpSat, 11 Apr 2015 16:10:00 GMThttp://www.shnenglu.com/sixleaves/archive/2015/04/12/210311.htmlhttp://www.shnenglu.com/sixleaves/comments/210311.htmlhttp://www.shnenglu.com/sixleaves/archive/2015/04/12/210311.html#Feedback0http://www.shnenglu.com/sixleaves/comments/commentRss/210311.htmlhttp://www.shnenglu.com/sixleaves/services/trackbacks/210311.html链表~码要理清细节性思\Q最好是单的M图,正如攚w的链表,本质上是循环链表。last指向最后一个节炏V其next指针一定指向头节点。我们把s[0]当做头节炏V?br />
 1 
 2 #include <cstdio>
 3 #include <cstring>
 4 const int maxn = 100000 + 5;
 5 int last, cur, next[maxn];
 6 char s[maxn];
 7 int main() {
 8     
 9     while ( scanf("%s", s + 1) == 1) {
10         
11         int n = strlen(s + 1);
12         last = cur = 0;
13         next[0] = 0; // 头结点初始化指向自n
14         
15         for (int i = 1; i <=n; i++) {
16             
17             char ch = s[i];
18             if ('[' == ch ) cur = 0;
19             else if (']' == ch) cur = last;
20             else {
21                 
22                 next[i] = next[cur];  // next[cur]为当前光标的前一个字W,而next[i]是当前光标要输入的字符Q接着Q光标还要蟩C个字W。所以我们要把当前字Wnext指向光标输入的字W。也是next[cur] = i;但要先保存其next指针l输入字W。其next指针其实是真相头结炏V?/span>
23                 next[cur] = i;
24                 if (cur == last) last = i;
25                 cur = i; // 指向下一个节?/span>
26             }
27             
28         }
29         
30         for (int i = next[0];i != 0; i = next[i]) {
31             printf("%c", s[i]);
32         }
33         puts("");
34     }
35     
36     return 0;
37 }

2015/4/12上午12:10:12


swp 2015-04-12 00:10 发表评论
]]>
UVA442 Matrix Chain Multiplicationhttp://www.shnenglu.com/sixleaves/archive/2015/04/11/210305.htmlswpswpFri, 10 Apr 2015 18:33:00 GMThttp://www.shnenglu.com/sixleaves/archive/2015/04/11/210305.htmlhttp://www.shnenglu.com/sixleaves/comments/210305.htmlhttp://www.shnenglu.com/sixleaves/archive/2015/04/11/210305.html#Feedback0http://www.shnenglu.com/sixleaves/comments/commentRss/210305.htmlhttp://www.shnenglu.com/sixleaves/services/trackbacks/210305.html 1 #include <cstdio>
 2 #include <iostream>
 3 #include <string>
 4 #include <stack>
 5 
 6 using namespace std;
 7 
 8 struct Matrix {
 9     int r,c;
10     Matrix(int a = 0, int b = 0) : r(a), c(b) {};
11 }m[26];
12 
13 stack<Matrix> s;
14 
15 int main()
16 {
17     
18     int n;
19     cin >> n;
20     for (int i = 0; i < n; i++) {
21         string name;
22         cin >> name;
23         int k = name[0] - 'A';
24         cin >> m[k].r >> m[k].c;
25         
26     }
27     
28     string expr;
29     while (cin >> expr) {
30         
31         int len = expr.size(); // 获取字符串的大小Q因整个字符串都没有I格
32         bool error = false;
33         int ans = 0;
34         for (int i = 0; i < len; i++) { // 循环整个表达式、思\是遇到右括号出栈两个Matrixq行计算Q算完在压回?br />35             // 其实你可以发现带括号的表辑ּ十分W合栈,而不仅仅是该题目Q首先左括号深
36             // 其计的有先U别高Q而右括号又和左括号配对,如果我们从左到右d一个表辑ּ
37             // 每次一遇到xP其前面到左括L部分肯定是q个表达式最优先的部分?/span>
38             if (isalpha(expr[i])) { s.push(m[expr[i] - 'A']); }
39             else if (')' == expr[i]) {
40                 Matrix m2 = s.top(); s.pop();
41                 Matrix m1 = s.top(); s.pop();
42                 if (m1.c != m2.r) { error = truebreak; }
43                 ans += m1.r * m1.c * m2.c;
44                 s.push(Matrix(m1.r, m2.c));
45             }
46             
47         }
48         
49         if (error) printf("error\n"); else printf("%d\n", ans);
50         
51     }
52     
53     return 0;
54 }
55 

2015/4/10下午6:32:37
By sixleaves

swp 2015-04-11 02:33 发表评论
]]>
UVA514 Railshttp://www.shnenglu.com/sixleaves/archive/2015/03/30/210195.htmlswpswpMon, 30 Mar 2015 08:17:00 GMThttp://www.shnenglu.com/sixleaves/archive/2015/03/30/210195.htmlhttp://www.shnenglu.com/sixleaves/comments/210195.htmlhttp://www.shnenglu.com/sixleaves/archive/2015/03/30/210195.html#Feedback0http://www.shnenglu.com/sixleaves/comments/commentRss/210195.htmlhttp://www.shnenglu.com/sixleaves/services/trackbacks/210195.html 铁轨

PopPush城市有一座著名的火R站。这个国家到处都是丘c而这个火车站是徏于上一个世U。不q的是,那时的资金有限。所以只能徏立v一条\面铁轨。而且Q这Dq个火R站在同一个时d能一个轨道投入用,因ؓ它缺空_两列火R无路可走。具体看下图?/strong>

当地的惯例是每一列火车从A方向驶向B方向时候,会用某种方式R厢重l。假讄车将要到达A方向Q拥有N个R?N<=1000),q些车厢按照递增序标记?到N。负责从lR厢的领导,必须知道是否能从lR厢让它驶出BQ而这个重l的序列是a1\a2\a3...aN.帮组他ƈ且写一个程序来判断是否可能按照所要求的R厢顺序。你可以假设Q单个的车厢可以从列车上分离出来Q在他们q入站台之前。ƈ且他们可以自q动,知道它们上了B轨道。你也可以假讑֜L时候站台可以放下无数的车厢。但是只要一个R厢进入站収ͼ它就不能q回A轨道Q同时如果它d了站台驶向B轨道Q它׃能返回站台?/strong>

输入Q?/strong>
q个输入文g由多个行块组成。每一个块描述的是多个要求的重lR厢的序列。在q每个块中的W一行是一个整数NQ被用来说明上面每行的R厢个数。这个快的最后一行仅仅是一个数?要来标记该快的结?/strong>
最后一个块仅仅是一?独占一行?/strong>

输出Q?/strong>
q个输出文g包含多行Q这些行和排列R厢的行数一一对应。日q该排列可行Q则输出YesQ否则输出No。另外存在一个空行在每个相对应的块后面。输出文件中不存在于最后一个什么数据都没有的响应输出?/strong>

输出
5
1 2 3 4 5
5 4 1 2 3
0
6
6 5 4 3 2 1
0
0

Output
Yes
No

Yes

 1 /*
 2 ׃stationW合后进先出规则Q所以可以用一个stack<int>来表Cstation?br /> 3 然后采用模拟的规则来写,因ؓq入station是按照编号递增q入Q所以可以用aId变量表示?br /> 4 接下来就是模拟时候应该注意的条gQ我们知道有以下两种情况Q一U是一q来stationQ就出station
 5 也就?nbsp;aId == coaches[lenB]; 一U是q来时还不出站,那么q时候就要s.push(aId),但是q一U的条g呢?
 6 我们如果可以排除掉第一U,那么无非是W二U了。但是我们要知道Q在q站之前Q如果站台里有R它是可以先出站的
 7 Q这U状态就条g是 !s.empty() && s.top() == coaches[lenB].所以只要按照顺序判断这几个条g
 8 可以写个模拟程序了?br /> 9 
10 
11 ȝQ?br />12 解决问题的关键点Q在于构建问题的模型、大部分都是可以用现有的基础数据l构。构造完对应的数据结构,特别是对于模拟题
13 无非是状态见的{Ud理,q是p在基于模型的基础上、先写出映射C个状态的唯一条gQ然后按照问题逻辑一一先后判断
14 卛_?br />15 */
16 #include <iostream>
17 #include <stack>
18 
19 using namespace std;
20 const int len = 1024;
21 int coaches[len];
22 
23 int main() {
24 
25     int n;
26     while (cin >> n, n) { 
27         stack<int> s;
28         // read the required permutaion
29 
30         while (    cin >> coaches[0], coaches[0]) {
31 
32             for (int i = 1; i < n; i++) {
33                 cin >> coaches[i];
34             }
35             int lenB = 0, aId = 1;
36             bool ok = true;
37             while (lenB < n) {
38 
39                 if (aId == coaches[lenB]) {  aId++; lenB++; }
40                 else if(!s.empty() && s.top() == coaches[lenB]) { s.pop(); lenB++; }
41                 else if(aId <= n) s.push(aId++);
42                 else { ok = falsebreak; }
43 
44             }
45 
46             cout << (ok ? "Yes" : "No") << endl;
47         }
48         cout << endl;
49     }
50     
51     return 0;
52 }
2015/3/30下午3:03:52


swp 2015-03-30 16:17 发表评论
]]>
UVA 400 Unix ls by sixleaveshttp://www.shnenglu.com/sixleaves/archive/2015/03/25/210160.htmlswpswpWed, 25 Mar 2015 06:32:00 GMThttp://www.shnenglu.com/sixleaves/archive/2015/03/25/210160.htmlhttp://www.shnenglu.com/sixleaves/comments/210160.htmlhttp://www.shnenglu.com/sixleaves/archive/2015/03/25/210160.html#Feedback0http://www.shnenglu.com/sixleaves/comments/commentRss/210160.htmlhttp://www.shnenglu.com/sixleaves/services/trackbacks/210160.html主要学习的技能要点就是如何用偏移位移法来按列输出。如果一个矩阵n行m列,要按照列输出。但是我们知道,~程时候,只能是for(?在列。所以必要有一个偏U量来计这U偏UdpRx = rows * c + r;

 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 #include <vector>
 5 #include <cmath>
 6 
 7 using namespace std;
 8 
 9 vector<string> words;
10 void print(const string &s, int len, char extra);
11 int main() {
12     
13     
14     int n;
15     string w;
16     
17     while (cin >> n) {
18         
19         int max = 0;
20         words.clear();
21         for (int i = 0; i < n; i++) {
22             cin >> w;
23             string::size_type str_size = w.size();
24             if (str_size > max) max = str_size;
25             words.push_back(w);
26         }
27         int cols = (60 - max) / (max + 2) + 1;
28         int rows = ceil(double(n) / cols);
29         sort(words.begin(), words.end());
30         print("", 60, '-');
31         cout << endl;
32         for(int r = 0; r < rows; r++) {
33             
34             for (int c = 0; c < cols; c++) {
35                 
36                 int idx = c * rows + r;
37                 if (idx < n) print(words[idx],(c == cols - 1? max: max + 2), ' ');
38                 
39             }
40             
41             cout << endl;
42             
43         }
44         
45         
46     }
47     
48     return 0;
49     
50 }
51 
52 void print(const string &s, int len, char extra) {
53     
54     cout << s;
55     string::size_type str_size = s.size();
56     for (int i = 0; i < len - str_size; i++) {
57         cout << extra;
58     }
59 }


swp 2015-03-25 14:32 发表评论
]]>
uva156 By sixleaveshttp://www.shnenglu.com/sixleaves/archive/2015/03/24/210152.htmlswpswpTue, 24 Mar 2015 09:13:00 GMThttp://www.shnenglu.com/sixleaves/archive/2015/03/24/210152.htmlhttp://www.shnenglu.com/sixleaves/comments/210152.htmlhttp://www.shnenglu.com/sixleaves/archive/2015/03/24/210152.html#Feedback0http://www.shnenglu.com/sixleaves/comments/commentRss/210152.htmlhttp://www.shnenglu.com/sixleaves/services/trackbacks/210152.html 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 #include <map>
 5 #include <vector>
 6 using namespace std;
 7 
 8 map<stringint> cnt;
 9 vector<string> words;
10 
11 string formatWord(const string &s);
12 
13 int main() {
14     
15     string s;
16     while(cin >> s) {
17         
18         if (s[0] == '#') break;
19         words.push_back(s);
20         
21         string fw = formatWord(s);
22         if (!cnt.count(fw)) cnt[fw] = 0;
23         cnt[fw]++;
24     }
25     
26     vector<string> ans;
27     for (int i = 0; i < words.size(); i++) {
28         
29         if ( cnt[formatWord( words[i] ) ] == 1) ans.push_back(words[i]);
30     }
31     
32     sort(ans.begin(), ans.end());
33     for (int i = 0; i < ans.size(); i++) {
34         cout << ans[i] << endl;
35     }
36     return 0;
37 }
38 
39 string formatWord(const string &s) {
40     
41     string ans = s;
42     int str_size = ans.size();
43     for (int i = 0; i <str_size; i++) {
44         
45         ans[i] = tolower(ans[i]);
46         
47     }
48     sort(ans.begin(), ans.end());
49     return ans;
50 
51 }

swp 2015-03-24 17:13 发表评论
]]>
uva540 Team Queue by sixleaveshttp://www.shnenglu.com/sixleaves/archive/2015/03/24/210144.htmlswpswpTue, 24 Mar 2015 00:31:00 GMThttp://www.shnenglu.com/sixleaves/archive/2015/03/24/210144.htmlhttp://www.shnenglu.com/sixleaves/comments/210144.htmlhttp://www.shnenglu.com/sixleaves/archive/2015/03/24/210144.html#Feedback0http://www.shnenglu.com/sixleaves/comments/commentRss/210144.htmlhttp://www.shnenglu.com/sixleaves/services/trackbacks/210144.html题目的具体要求是Q每ơ入队里前,先从队列头扫描到队列,如果队列里有队友Q就排在队友们的最后面(不是该队友的后面Q是整队队友的最后面)。如果没有队友则Q直接排在队列的最后面。出队列的没什么特别的了?br />
Keys:其实我们可以通过单的模拟、发现。由W一个队员到最后一个队员入队列Q或者中间有其他出队列。该队列始终可以看成是两个队列的队列。又因ؓ题目要求常数的时间?br />所以我们不可能把时间浪费在某个队员属于哪一个队里,所以可以用映射、也是map来解册个问题。map<int, int>q个l构刚好能映这U关pR接下去是要有一个q2[maxn]来表C所有初始化的队列。一个q来表C新队列Q这个q其实是队列的队列?br />
 1 #include <queue>
 2 #include <string>
 3 #include <map>
 4 #include <iostream>
 5 using namespace std;
 6 
 7 const int maxn = 1024;
 8 
 9 
10 int main() {
11     
12     int t, n, k = 0;
13     while (cin >> t, t) {
14         // 列队~号
15         cout << "Scenario #" << ++k << endl;
16         int qid = 1;
17         map<intint> team;
18         while (t--) {
19             cin >> n;
20             for (int i = 0; i < n; i++) {
21                 int uid;
22                 cin >> uid;
23                 team[uid] = qid;
24                 
25             }
26             qid++;
27         }
28         
29         // uid是队员编受?nbsp;tid是那一寚w的编?/span>
30         queue<int> q,q2[maxn];
31         string op;
32         int uid;
33         
34         while (cin >> op, op[0] != 'S') {
35             
36             // 入队?/span>
37             if ('E' == op[0]) {
38                 cin >> uid;
39                 int tid = team[uid];
40                 if (q2[tid].empty()) q.push(tid);
41                 q2[tid].push(uid);
42 
43             }
44             // 出队?/span>
45             if ('D' == op[0]) {
46                 
47                 int tid = q.front();
48                 int x = q2[tid].front();
49                 q2[tid].pop();
50                 cout << x << endl;
51                 if (q2[tid].empty()) q.pop();
52                 
53             }
54             
55         }
56         
57         cout << endl;
58         
59     
60     }
61     
62 }


swp 2015-03-24 08:31 发表评论
]]>
uva12096 The SetStack Computer By sixleaveshttp://www.shnenglu.com/sixleaves/archive/2015/03/23/210143.htmlswpswpMon, 23 Mar 2015 14:29:00 GMThttp://www.shnenglu.com/sixleaves/archive/2015/03/23/210143.htmlhttp://www.shnenglu.com/sixleaves/comments/210143.htmlhttp://www.shnenglu.com/sixleaves/archive/2015/03/23/210143.html#Feedback0http://www.shnenglu.com/sixleaves/comments/commentRss/210143.htmlhttp://www.shnenglu.com/sixleaves/services/trackbacks/210143.html 1 #include <set>
 2 #include <string>
 3 #include <vector>
 4 #include <map>
 5 #include <stack>
 6 #include <iostream>
 7 #include <algorithm>
 8 #define ALL(x) x.begin(), x.end()
 9 #define INS(x) inserter(x, x.begin())
10 
11 using namespace std;
12 
13 typedef set<int> Set;
14 map<Set, int> IDCache;
15 vector<Set> Setcache;
16 // 主要的想法是能想到用map<set<int>, int>q种数据l构来把集合映射成整?br />17 // 关键实现在ID函数Q对于给定的set<int>都能q回一个唯一~号、vector虽然不能保证元素的唯一性?br />18 // 但是我们可以先对mapq行查来保证vector中元素的唯一性,q样每个元素p唯一~号Q刚好可以利用他们的整数索引?br />19 // 其中set_union、set_intersection中得实现原理不是重点Q先学会怎么用才是重炏V?br />20 // ID函数实现了对新的集合存储Qƈ?/span>
21 int ID(Set x);
22 int main() {
23 
24     
25     stack<int> s;
26     int t, n;
27     string op;
28     cin >> t;
29     while (t--) {
30         cin >> n;
31         IDCache.clear();
32         Setcache.clear();
33         for (int i = 0; i < n; i++) {
34 
35             cin >> op;
36             if (op[0] == 'P') s.push(ID(Set())); // Set()是I集
37             else if(op[0] == 'D') s.push(s.top());
38             else {
39                 
40                 Set x1 = Setcache[s.top()]; s.pop();
41                 Set x2 = Setcache[s.top()]; s.pop();
42                 Set x;
43                 if (op[0] == 'U') set_union (ALL(x1), ALL(x2), INS(x));
44                 if (op[0] == 'I') set_intersection (ALL(x1), ALL(x2), INS(x));
45                 if (op[0] == 'A') { x = x2; x.insert(ID(x1)); }
46                 s.push(ID(x));
47                 
48             }
49             
50             cout << Setcache[s.top()].size() << endl;
51         }
52         cout << "***" << endl;
53     
54     }
55     return 0;
56 }
57 
58 // 相当于数据库中得auto_increment, q回一个唯一的ID?/span>
59 int ID(Set x) {
60     
61     if (IDCache.count(x)) return IDCache[x];
62     Setcache.push_back(x);
63     return IDCache[x] = Setcache.size() - 1;
64 
65 }

swp 2015-03-23 22:29 发表评论
]]>
uva10815 by sixleaveshttp://www.shnenglu.com/sixleaves/archive/2015/03/16/210057.htmlswpswpMon, 16 Mar 2015 10:28:00 GMThttp://www.shnenglu.com/sixleaves/archive/2015/03/16/210057.htmlhttp://www.shnenglu.com/sixleaves/comments/210057.htmlhttp://www.shnenglu.com/sixleaves/archive/2015/03/16/210057.html#Feedback0http://www.shnenglu.com/sixleaves/comments/commentRss/210057.htmlhttp://www.shnenglu.com/sixleaves/services/trackbacks/210057.html前者用于把stringstream的返回string~存Q后者是把string对象包装成stringstream对象。stringstreamcd在于sstream头文件中。其是独立于标准I/O但有cM
功能的类。clear主要是ؓ了多ơ用stringstreamQ因为stringstream每次的构造都十分耗时Q所以最后能多次Q反复用,如同面向对象里面的单例模式?br />代码如下Q?br />
 1 #include <iostream>
 2 #include <string>
 3 #include <set>
 4 #include <sstream>
 5 
 6 
 7 using namespace std;
 8 
 9 set<string> dict;
10 stringstream ss;
11 
12 /*
13  主要学习两个重点stringstream的用法与set容器的insert、P代器的用法?br />14  */
15 int main() {
16 
17     string s,buf;
18     
19     while (cin >> s) {
20         int len = s.size();
21         for (int i = 0; i < len; i++) {
22             if (isalpha(s[i])) s[i] = tolower(s[i]); else s[i] = ' ';
23         }
24         ss.clear();
25         ss.str(s);
26         while (ss >> buf) dict.insert(buf);
27     }
28     
29     for (set<string>::iterator it = dict.begin(); it != dict.end(); ++it)
30         cout << *it << endl;
31     return 0;
32 }


swp 2015-03-16 18:28 发表评论
]]>
uva 101 by sixleaveshttp://www.shnenglu.com/sixleaves/archive/2015/03/16/210052.htmlswpswpMon, 16 Mar 2015 07:03:00 GMThttp://www.shnenglu.com/sixleaves/archive/2015/03/16/210052.htmlhttp://www.shnenglu.com/sixleaves/comments/210052.htmlhttp://www.shnenglu.com/sixleaves/archive/2015/03/16/210052.html#Feedback0http://www.shnenglu.com/sixleaves/comments/commentRss/210052.htmlhttp://www.shnenglu.com/sixleaves/services/trackbacks/210052.html接下来得思\是自向下的方式Q逐步完善E序。首先根据提议列Z表?br />1.move a onto b
clear_above(a) && clear_above(b);
insert a above b;

2.move a over b
clear(a)
insert a above bs

3.pile a onto b
clear(b)
insert as above b

4.pile a over b
insert as above bs

观察可以提取出move的话必有clear_above(a)、onto必有clear_above(b).
而insert 动作不管b是在p[b]的顶部还是外部。都是p[b].push_back(a或a以上的木?
所以可以抽取成pile_into(pa, ha, pb);

考虑完这些,开始写框架。如?br />
 1 
 2 #include <cstdio>
 3 #include <iostream>
 4 #include <string>
 5 #include <vector>
 6 using namespace std;
 7 
 8 const int maxn = 64;
 9 int n;
10 vector<int> a[maxn];
11 int find_block(int a, int& h);
12 void clear_above(int p, int h);
13 void pile_into(int pa, int ha, int pb);
14 void print();
15 int main() {
16 
17     
18     
19     string s1, s2;
20     scanf("%d", &n);
21     for (int i = 0; i < n ; i++) {
22         a[i].push_back(i);
23     }
24     
25     while (cin >> s1, s1 != "quit") {
26         int ba,bb;
27         cin >> ba >> s2 >> bb;
28         int ha = 0,hb = 0;
29         int pa = find_block(ba, ha);
30         int pb = find_block(bb, hb);
31         if (pa == pb) continue;
32         
33         if (s1 == "move") clear_above(pa, ha);
34         if (s2 == "onto") clear_above(pb, hb);
35         pile_into(pa, ha, pb);
36     }
37     
38     print();
39     return 0;
40 }
 
接下来,完成E序其它部分Q按照框架的意思,逐步完善。如?br />
 1 void print() {
 2     
 3     for (int i = 0; i < n; i++) {
 4         printf("%d:",i);
 5         for (int h = 0; h < a[i].size(); h++) {
 6             printf(" %d", a[i][h]);
 7         }
 8         printf("\n");
 9     }
10 }
11 
12 int find_block(int ba, int& h) { 
13     
14     for (int i = 0; i < n; i++) {
15         
16         int vec_size = a[i].size();
17         for (h = 0; h < vec_size; h++) {
18             if (ba == a[i][h]) return i;
19         }
20         
21     }
22     return -1;
23 }
24 
25 void clear_above(int p, int h) {
26     int vec_size = a[p].size();
27     for (int i = h + 1; i < vec_size; i++) {
28         
29         int b = a[p][i];
30         a[b].push_back(b);
31         
32     }
33     a[p].resize(h + 1);
34 }
35 
36 void pile_into(int pa, int ha, int pb) {
37     
38     int vec_size = a[pa].size();
39     
40     for (int i = ha; i < vec_size; i++) {
41         a[pb].push_back(a[pa][i]);
42     }
43     a[pa].resize(ha);
44 }


swp 2015-03-16 15:03 发表评论
]]>
uva 1589 by sixleaveshttp://www.shnenglu.com/sixleaves/archive/2015/03/15/210044.htmlswpswpSun, 15 Mar 2015 08:42:00 GMThttp://www.shnenglu.com/sixleaves/archive/2015/03/15/210044.htmlhttp://www.shnenglu.com/sixleaves/comments/210044.htmlhttp://www.shnenglu.com/sixleaves/archive/2015/03/15/210044.html#Feedback0http://www.shnenglu.com/sixleaves/comments/commentRss/210044.htmlhttp://www.shnenglu.com/sixleaves/services/trackbacks/210044.html
  1 #include <stdio.h>
  2 #include <string.h>
  3 #define N 12
  4 
  5 char brd[N][N];
  6 int dx[] = {1,-1,0,0}, dy[] = {0,0,1,-1};
  7 int hx[] = {-2,-1,-2,-1,1,2,1,2};
  8 int hy[] = {-1,-2,1,2,-2,-1,2,1};
  9 int tx[] = {-1,-1,-1,-1,1,1,1,1};
 10 int ty[] = {-1,-1,1,1,-1,-1,1,1};
 11 int cr[2], cc[2];
 12 
 13 
 14 int check(intint);
 15 
 16 int main() {
 17     
 18     int t, bX, bY;
 19     char s[5];
 20     while (scanf("%d%d%d",&t, &bX, &bY), t || bX || bY) {
 21         
 22         memset(brd, 0, sizeof(brd));
 23         cr[0] = cc[0] = cr[1] = cc[1] = 0;
 24         
 25         for (int i = 0; i < t; i++) {
 26             
 27             int rX, rY;
 28             scanf("%s%d%d", s, &rX, &rY);
 29             if ('C' == s[0]) {
 30                 
 31                 if (cr[0]) cr[1] = rX, cc[1] = rY;
 32                 else cr[0] = rX, cc[0] = rY;
 33             }
 34             brd[rX][rY] = s[0];
 35         }
 36         
 37         // 判断是否四个方向都会被将?或者无路可走的方向也认为是被将?/span>
 38         int cnt = 0;
 39         for (int i = 0; i < 4; i++)
 40             cnt += check(bX + dx[i], bY + dy[i]);
 41         
 42         if (cnt < 4) puts("NO");
 43         else puts("YES");
 44         
 45     }
 46     
 47 }
 48 
 49 int check(int r, int c) {
 50     
 51     // 界Q无路可?/span>
 52     if (r < 1 || r > 3 || c < 4 || c > 6) return 1;
 53     
 54     
 55     // 因ؓ我们没法保证车中间有没有其他子Q所以必Mq到q一格格?br /> 56     // 车在同行且中间无子
 57     for (int j = c - 1; j > 0; j--) {
 58         if (brd[r][j])
 59             if ('R' == brd[r][j]) return 1;
 60             else break;
 61     }
 62     
 63     for (int j = c + 1; j <= 9; j++) {
 64         
 65         if (brd[r][j])
 66             if ('R' == brd[r][j]) return 1;
 67             else break;
 68     }
 69     
 70     
 71     // 车在同列且中间物子
 72     for (int j = r - 1; j > 0; j--) {
 73         
 74         if (brd[j][c])
 75             if ('R' == brd[j][c]) return 1;
 76             else break;
 77         
 78     }
 79     
 80     for (int j = r + 1; j <=10; j++) {
 81         
 82         if (brd[j][c])
 83             if ('R' == brd[j][c] || 'G' == brd[j][c]) return 1;
 84             else break;
 85         
 86     }
 87     
 88     // 炮将?/span>
 89     for (int k = 0; k < 2; k++) {
 90         
 91         // 行有?/span>
 92         if (r == cr[k]) {
 93             int cnt = 0;
 94             for (int j = c - 1; j > cc[k]; j--) if (brd[r][j]) ++cnt;
 95             if (cnt == 1) return 1;
 96             cnt = 0;
 97             for (int j = c + 1; j < cc[k]; j++) if (brd[r][j]) ++cnt;
 98             if (cnt == 1) return 1;
 99             
100         }
101         
102         // 列有?/span>
103         if (c == cc[k]) {
104             int cnt = 0;
105             for (int j = r - 1; j > cr[k]; j--) if (brd[j][c]) ++cnt;
106             if (cnt == 1) return 1;
107             cnt = 0;
108             for (int j = r + 1; j < cr[k]; j++) if (brd[j][c]) ++cnt;
109             if (cnt == 1) return 1;
110         }
111         
112         
113     }
114     
115     // 马将?马的8个方?/span>
116     for(int k = 0; k < 8; ++k) {
117         
118         int tr = r + hx[k], tc = c + hy[k];
119         if (tr < 1 || tr > 10 || tc < 1 || tc > 9) continue;
120         if (brd[tr][tc] == 'H' && (!brd[r + tx[k]][c + ty[k]]))
121             return 1;
122         
123     }
124     return 0;
125 }


swp 2015-03-15 16:42 发表评论
]]>
UVA 213 By sixleaveshttp://www.shnenglu.com/sixleaves/archive/2015/03/13/210027.htmlswpswpFri, 13 Mar 2015 12:06:00 GMThttp://www.shnenglu.com/sixleaves/archive/2015/03/13/210027.htmlhttp://www.shnenglu.com/sixleaves/comments/210027.htmlhttp://www.shnenglu.com/sixleaves/archive/2015/03/13/210027.html#Feedback0http://www.shnenglu.com/sixleaves/comments/commentRss/210027.htmlhttp://www.shnenglu.com/sixleaves/services/trackbacks/210027.html 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 
 5 int readChar();
 6 int readInt(int c);
 7 int readCodes();
 8 
 9 /*
10  1.d字符时候如何过滤掉换行W号Q兼容类unix和windows操作pȝ readChar()
11  2.熟练掌握二q制字符串映到数组的数据结?q个数据l构是个二元l,我们可以用该二元l唯一定一个长度ؓlen的二q制字符?br />12  Qlen, valueQ?nbsp;---- > code[len][value]
13  
14  */
15 
16 int main() {
17 
18     
19     while ( readCodes() ) { // ȝ码头
20         
21         for (;;) {  // M?/span>
22             
23             int len = readInt(3);
24             if (0 == len) break// ?该信息结?/span>
25             
26             for (;;) {  // M息段
27                 
28                 int v = readInt(len);
29                 
30                 if (v == (1 << len) - 1break// ?一个信息段l束
31                 putchar(code[len][v]);
32                 
33             }
34             
35         }
36         
37         putchar('\n');
38     }
39 }
40 
41 
42 int readInt(int l) {
43     
44     int v = 0;
45     //已经d? - l个字W?/span>
46     while (l--) {
47         
48         v = v * 2 + readChar() - '0';
49         
50     }
51     return v;
52     
53 }
54 
55 
56 char readChar() {
57     
58     char ch;
59     do {
60         
61         ch = getchar();
62         
63     }while ( '\n' != ch || '\r' != ch )
64     return ch;
65 }
66 
67 
68 int readCodes() {
69     
70     
71     memset(code, 0sizeof(code));
72     
73     // 因ؓ可能d~码头独占一行,所以我们可能读取编码头时候会dC一ơ的回R换行Q所以要使用readChar函数
74     code[1][0= readChar();
75     
76     for (int len = 2; len < 8; len++) {
77         
78         for (int v = 0; v < (1 << len) - 1; v++) {
79             
80             char ch = getchar();
81             if ( EOF == ch ) return 0;
82             if ( '\n' == ch || '\r' == ch) return 1;
83             code[len][v] = ch;
84             
85         }
86         
87     }
88     return 1;
89     
90     
91 }

swp 2015-03-13 20:06 发表评论
]]>
UVA 10474http://www.shnenglu.com/sixleaves/archive/2015/02/12/209800.htmlswpswpThu, 12 Feb 2015 02:43:00 GMThttp://www.shnenglu.com/sixleaves/archive/2015/02/12/209800.htmlhttp://www.shnenglu.com/sixleaves/comments/209800.htmlhttp://www.shnenglu.com/sixleaves/archive/2015/02/12/209800.html#Feedback0http://www.shnenglu.com/sixleaves/comments/commentRss/209800.htmlhttp://www.shnenglu.com/sixleaves/services/trackbacks/209800.html具体用到了C++ STL的两个函数模板一个是sort、一个是lower_bound,lower_bound(begin, end, v);
lower_bound与strchrcMQ但它用的是P代器begin与endq回的是大于{于v的最数所在的q代器,
使用该模板函数需要注意的问题是,必须是针对已l排序好得数字,具体代码如下?/div>
 1 #include <cstdio>
 2 #include <algorithm>
 3 
 4 using namespace std;
 5 const int maxn = 10000;
 6 
 7 
 8 int main() {
 9     
10     int n, q, x, a[maxn], kase = 0;
11     while (scanf("%d%d", &n, &q) == 2 && n) {
12         
13         for (int i = 0; i < n; i++) scanf("%d", &a[i]);
14         sort(a, a+n);
15         printf("CASE# %d:\n", ++kase);
16         while (q--) {
17             
18             scanf("%d", &x);
19             int p = lower_bound(a, a+n, x) - a;
20             if (a[p] == x) printf("%d found at %d\n", x, p + 1);
21             else printf("%d not found\n",x);
22             
23         }
24     }
25     return 0;
26 }


swp 2015-02-12 10:43 发表评论
]]>UVA 253http://www.shnenglu.com/sixleaves/archive/2015/02/08/209765.htmlswpswpSat, 07 Feb 2015 17:42:00 GMThttp://www.shnenglu.com/sixleaves/archive/2015/02/08/209765.htmlhttp://www.shnenglu.com/sixleaves/comments/209765.htmlhttp://www.shnenglu.com/sixleaves/archive/2015/02/08/209765.html#Feedback0http://www.shnenglu.com/sixleaves/comments/commentRss/209765.htmlhttp://www.shnenglu.com/sixleaves/services/trackbacks/209765.html/Files/sixleaves/253.pdf 
题目在上面,可以自己下蝲Q这道一开始没什么思\Q后来仔l想惻IH然有了点灵感,但是q是找不到比较简单的办法
E序q有优化的地步,用到的全局变量有点多?/div>
我的思\是:每个数字都有一个位于Top也就是最得时候,只要在这时候,竖直旋{4ơ,分别判断可以知道是否存在相同的cube了?/div>
E序M框架挺清晰的Q但是有一点就是下标没处理好,有兴的Q可以自q一一下?/div>
by sixleaves
 1 #include <stdio.h>
 2 
 3 //  枚D各个数字位于"?的一U可能情?br />  4 //  该情况下Q各个数字所处的位置
 5 int cubeTop[7][7] = {
 6     {0,0,0,0,0,0,0},
 7     {0,1,2,3,4,5,6},
 8     {0,2,6,3,4,1,5},
 9     {0,3,2,6,1,5,4},
10     {0,4,2,1,6,5,3},
11     {0,5,1,3,4,6,2},
12     {0,6,5,3,4,2,1}
13 };
14 
15 
16 int lastCube[7] = {0};
17 int curCube[7] = {0};
18 
19 int isFind;
20 
21 // [0,6)  [6,12)--为另外一个cube
22 char s[15] = {0};
23 void turn();
24 void update();
25 int isSameCube();
26 int main() {
27     
28     while (scanf("%s",s) != EOF) {
29     
30         // 枚Dtop面数?/span>
31         isFind = 0;
32         for (int i = 1; i <=6; i++) {
33             
34             //  初始化当前筛?/span>
35             for (int k = 1; k <= 6; k++) {
36                 lastCube[k] = curCube[k] = cubeTop[i][k];
37             }
38             
39             // 竖直转动4ơ筛?/span>
40             for (int j = 0; j < 4; j++) {
41                 
42                 turn();
43                 
44                 if (isSameCube()) {
45                     isFind = 1;
46                     break;
47                 }
48                 
49             }
50             
51             if (isFind) {
52                 break;
53             }
54         }
55         
56         printf( isFind == 1? "TRUE\n" : "FALSE\n");
57         
58     }
59     return 0;
60 }
61 
62 int isSameCube() {
63     
64     //分别比较对应6个面
65     for (int i = 0; i < 6; i++) {
66         if (s[curCube[i + 1] - 1] != s[i + 6])
67             return 0;
68     }
69     return 1;
70 }
71 
72 void turn() {
73     
74     curCube[2] = lastCube[3];
75     curCube[4] = lastCube[2];
76     curCube[5] = lastCube[4];
77     curCube[3] = lastCube[5];
78     //  更新lastCube
79     update();
80 }
81 
82 void update() {
83     
84     lastCube[2] = curCube[2];
85     lastCube[4] = curCube[4];
86     lastCube[5] = curCube[5];
87     lastCube[3] = curCube[3];
88     
89 }


swp 2015-02-08 01:42 发表评论
]]>UVA 489 http://www.shnenglu.com/sixleaves/archive/2015/02/07/209763.htmlswpswpSat, 07 Feb 2015 05:29:00 GMThttp://www.shnenglu.com/sixleaves/archive/2015/02/07/209763.htmlhttp://www.shnenglu.com/sixleaves/comments/209763.htmlhttp://www.shnenglu.com/sixleaves/archive/2015/02/07/209763.html#Feedback0http://www.shnenglu.com/sixleaves/comments/commentRss/209763.htmlhttp://www.shnenglu.com/sixleaves/services/trackbacks/209763.html

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define MAXN 1024
 4 
 5 int left, chance;
 6 int win, lose;
 7 void guess(char ch);
 8 
 9 char p[MAXN] = {0}, g[MAXN] = {0};
10 int main() {
11     
12     int r = 0;
13     
14     while (scanf("%d%s%s",&r, p, g) == 3 && r != -1) {
15         
16         printf("Round %d\n", r);
17         win = lose = 0;
18         left = strlen(p);
19         chance = 7;
20         for (int i = 0; g[i]; i++) {
21             
22             guess(g[i]);
23             
24             if (win || lose) break;
25             
26         }
27         
28         if (win) printf("You win.\n");
29         else if(lose) printf("You lose.\n");
30         else printf("You chickened out.\n");
31         
32     }
33     return 0;
34 }
35 
36 
37 void guess(char ch) {
38     
39     int finded = 0;
40     for (int i = 0; p[i]; i++) {
41         
42         if (ch == p[i]) {
43             p[i] = ' ';
44             left--;
45             finded = 1;
46             
47         }
48         
49     }
50     
51     if (!finded) --chance;
52     if (!chance) lose = 1;
53     if (!left) win = 1;
54     
55 }

by sixleaves


swp 2015-02-07 13:29 发表评论
]]>
UVA 227http://www.shnenglu.com/sixleaves/archive/2015/02/06/209753.htmlswpswpFri, 06 Feb 2015 02:24:00 GMThttp://www.shnenglu.com/sixleaves/archive/2015/02/06/209753.htmlhttp://www.shnenglu.com/sixleaves/comments/209753.htmlhttp://www.shnenglu.com/sixleaves/archive/2015/02/06/209753.html#Feedback0http://www.shnenglu.com/sixleaves/comments/commentRss/209753.htmlhttp://www.shnenglu.com/sixleaves/services/trackbacks/209753.html写得比较长,没有q行重构Q就q样吧。getchar(){I/O虽然会读取回车,但是要按下回车,产生中断Q在会结束输
告诉q些I/O函数来读?=。Mq题目,没什么特别的思想Q完全是模拟题,能写:?的语句,量写,化代?/div>
q有代码宁愿严密啰嗦Q也不要有Bug

#include <stdio.h>

#include <string.h>

typedef struct {

    int r;

    int c;

} Point;


int main() {

    

    const int maxn = 5;

    char puzzle[maxn][maxn] = {0};

    int kase = 0;

    int first = 1;

    for (;;) {

        

        //  1.先读取一个字W?span style="font-family: Menlo;">,看是不是Z,不是Z得看看是不是I字W?/p>

        char ch;

        ch = getchar();

        if (ch == 'Z') break;

        else {

            puzzle[0][0] = ch;

            first == 1? first = 0: printf("\n");

        }

        

        Point empty;

        if (ch == ' ') { empty.r = 0, empty.c = 0; }

        

        //  2.dPuzzle初始化布局

        for (int i = 1; i < 25; ) {

            ch = getchar();

            if (ch == ' ') {

                puzzle[ empty.r = i / 5 ][ empty.c = i % 5 ] = ch;

                i++;

            }

            if (ch != ' ' && ch != '\n' && ch != '\t' && ch != '\r') {

                puzzle[ i/5 ][ i%5 ] = ch;

                i++;

            }

        }

        

        //  3.执行指o

        int configuration = 1;

        while ((ch = getchar()) != '0') {

            

            if (ch == '\n' || ch == ' ' || !configuration) continue;

            int row = empty.r, col = empty.c;

            if (configuration) {

                switch (ch) {

                        

                    case 'A':

                        if (row - 1 >= 0) {

                            puzzle[row][col] = puzzle[row - 1][col];

                            puzzle[row - 1][col] = ' ';

                            empty.r = row - 1;

                            empty.c = col;

                        }else {

                            configuration = 0;

                        }

                        break;

                    case 'B':

                        if (row + 1 < maxn) {

                            puzzle[row][col] = puzzle[row + 1][col];

                            puzzle[row + 1][col] = ' ';

                            empty.r = row + 1;

                            empty.c = col;

                        }else {

                            configuration = 0;

                        }

                        break;

                    case 'R':

                        if (col + 1 < maxn) {

                            puzzle[row][col] = puzzle[row][col + 1];

                            puzzle[row][col + 1] = ' ';

                            empty.r = row;

                            empty.c = col + 1;

                        }else {

                            configuration = 0;

                        }

                        break;

                    case 'L':

                        if (col - 1 >= 0) {

                            puzzle[row][col] = puzzle[row][col - 1];

                            puzzle[row][col - 1] = ' ';

                            empty.r = row;

                            empty.c = col - 1;

                        }else {

                            configuration = 0;

                        }

                        break;

                    default:

                        configuration = 0;

                        break;

                        

                }

            }

            

        }

        //  4.吃掉回R

        ch = getchar();

        

        //  5.输出

        if (!configuration) {

            printf("Puzzle #%d:\nThis puzzle has no final configuration.\n", ++kase);

        }else {

            printf("Puzzle #%d:\n", ++kase);

            for (int row = 0; row < maxn; row++) {

                

                for (int col = 0; col < maxn; col++) {

                    

                    printf(col == maxn - 1 ? "%c" : "%c ", puzzle[row][col]);

                    

                }

                printf("\n");

            }

        }

        

    }

    

    return 0;

}



swp 2015-02-06 10:24 发表评论
]]>UVA 445http://www.shnenglu.com/sixleaves/archive/2015/02/05/209748.htmlswpswpThu, 05 Feb 2015 12:01:00 GMThttp://www.shnenglu.com/sixleaves/archive/2015/02/05/209748.htmlhttp://www.shnenglu.com/sixleaves/comments/209748.htmlhttp://www.shnenglu.com/sixleaves/archive/2015/02/05/209748.html#Feedback0http://www.shnenglu.com/sixleaves/comments/commentRss/209748.htmlhttp://www.shnenglu.com/sixleaves/services/trackbacks/209748.html
q有另外一个,len % t == 0是必dQ因为只有是周期倍数的才能求出周期。不然如Qabcdabc求出的周期就变成?.

 1 #include <stdio.h>
 2 #include <string.h>
 3 const int maxn = 1024;
 4 char buf[maxn] = {0};
 5 int isT(char * buf, int len,int t);
 6 int main() {
 7     
 8     int n;
 9     while (~scanf("%d",&n))
10         while ( n > 0 ) {
11             
12             //  输入
13             scanf("%s",buf);
14             int len = strlen(buf);
15             
16             //  计算最周?从小到大枚D
17             for (int t = 1; t <= len; t++) {
18                 if (len % t == 0)
19                     if (isT(buf, len, t)) {
20                         printf("%d\n", t);
21                         break;
22                     }
23             }
24             
25             if (--n) printf("\n");
26         }
27     
28     return 0;
29 }
30 
31 int isT(char * buf, int len,int t) {
32     
33     for (int i = t; i < len; i++) {
34         
35         if (buf[i % t] != buf[i]) {
36             
37             return 0;
38         }
39     }
40     
41     return 1;
42 }

by sixleaves


swp 2015-02-05 20:01 发表评论
]]>
UVA 1225http://www.shnenglu.com/sixleaves/archive/2015/02/05/209747.htmlswpswpThu, 05 Feb 2015 11:53:00 GMThttp://www.shnenglu.com/sixleaves/archive/2015/02/05/209747.htmlhttp://www.shnenglu.com/sixleaves/comments/209747.htmlhttp://www.shnenglu.com/sixleaves/archive/2015/02/05/209747.html#Feedback0http://www.shnenglu.com/sixleaves/comments/commentRss/209747.htmlhttp://www.shnenglu.com/sixleaves/services/trackbacks/209747.html
 1 #include <stdio.h>
 2 const int maxn= 10008;
 3 int count[maxn][10] = {0};
 4 int main() {
 5     
 6     
 7     int n;
 8     char buf[maxn] = {0};
 9     
10     //计算每一?/span>
11     for (int i = 1; i<= maxn; i++) {
12         
13         sprintf(buf,"%d", i);
14         for (int j = 0; buf[j]; j++) {
15             count[i][buf[j]-'0']++;
16         }
17         
18     }
19     
20     //建立数据,防止重复计算
21     for (int i = 2; i <= maxn; i++) {
22         
23         for (int j = 0; j < 10; j++) {
24             
25             count[i][j] += count[i - 1][j];
26             
27         }
28     }
29     
30     scanf("%d", &n);
31     while (n > 0) {
32         int e;
33         scanf("%d", &e);
34         
35         for (int i = 0; i < 10; i++) {
36             printf( i == 9? "%d\n" : "%d ", count[e][i]);
37         }
38         
39         n--;
40     }
41 }
by sixleaves

swp 2015-02-05 19:53 发表评论
]]>UVA 1586http://www.shnenglu.com/sixleaves/archive/2015/02/05/209746.htmlswpswpThu, 05 Feb 2015 11:37:00 GMThttp://www.shnenglu.com/sixleaves/archive/2015/02/05/209746.htmlhttp://www.shnenglu.com/sixleaves/comments/209746.htmlhttp://www.shnenglu.com/sixleaves/archive/2015/02/05/209746.html#Feedback0http://www.shnenglu.com/sixleaves/comments/commentRss/209746.htmlhttp://www.shnenglu.com/sixleaves/services/trackbacks/209746.html
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <ctype.h>
 4 #define MAXN 100
 5 
 6 char buf[MAXN] = {0};
 7 double t[26] = {0};
 8 int readInt(char * buf, int i, int *num);
 9 int main() {
10     
11     int n;
12     int ch, num;
13     t['C'-'A'] = 12.01, t['H'-'A'] = 1.008;
14     t['O' - 'A'] = 16.00, t['N' - 'A'] = 14.01;
15     scanf("%d", &n);
16     
17     while (n > 0) {
18         
19         //  1.输入数据
20         scanf("%s", buf);
21         int len = strlen(buf);
22         double sum = 0.0;
23         int i = 0;
24         
25         //  2.计算
26         for (;;) {
27             
28             for (; buf[i]&&isalpha(buf[i]); i++) {
29                 sum += t[buf[i] - 'A'];
30             }
31             if (i >= len) break;
32             
33             //  2.1[i, e)为整数范?num为整数?/span>
34             int e,num;
35             e = readInt(buf, i, &num);
36             sum += t[buf[i - 1] - 'A'] * (num - 1);
37             i = e;
38         }
39         
40         //  3.输出l果
41         printf("%.3f\n", sum);
42         n--;
43     }
44     
45     return 0;
46 }
47 
48 //  如果没有扑ֈ则返回i
49 int readInt(char * buf, int i, int *num) {
50     int sum = 0;
51     int j;
52     for (j = i; buf[j] && isdigit(buf[j]); j++) {
53         sum *= 10;
54         sum += buf[j] - '0';
55     }
56     *num = sum;
57     return j;
58     
59 }
by sixleaves

swp 2015-02-05 19:37 发表评论
]]>UVA 1585http://www.shnenglu.com/sixleaves/archive/2015/02/05/209735.htmlswpswpThu, 05 Feb 2015 01:09:00 GMThttp://www.shnenglu.com/sixleaves/archive/2015/02/05/209735.htmlhttp://www.shnenglu.com/sixleaves/comments/209735.htmlhttp://www.shnenglu.com/sixleaves/archive/2015/02/05/209735.html#Feedback0http://www.shnenglu.com/sixleaves/comments/commentRss/209735.htmlhttp://www.shnenglu.com/sixleaves/services/trackbacks/209735.html
 1 #include <stdio.h>
 2 #include <string.h>
 3 #define MAXN 86
 4 int main() {
 5     
 6     int n;
 7     char buf[MAXN] = {0};
 8     scanf("%d", &n);
 9     typedef enum {
10         WordIn,
11         WordOut,
12     } Word;
13     while ( n--> 0) {
14         
15         scanf("%s", buf);
16         int sum = 0, b = 0, e = 0, len = strlen(buf);
17         buf[len++] = 'X', buf[len] = '\0';
18         Word word = WordOut;
19         
20         //  遍历字符?/span>
21         for (int i = 0; buf[i]; i++) {
22             
23             //  记录单词开?/span>
24             if (buf[i] == 'O') {
25                 
26                 b = word == WordOut? ( word = WordIn, i) : b;
27                 
28             }else {
29                 
30                 //  记录单词l尾,q作l计
31                 if (word == WordIn) {
32                     word = WordOut;
33                     e = i;
34                     int max = e - b;
35                     sum += max*(max + 1) / 2;
36                 }
37                 
38             }
39         }
40         printf("%d\n", sum);
41     }
42     return 0;
43 }
by sixleaves

swp 2015-02-05 09:09 发表评论
]]>
关于怎么判断循环的次?/title><link>http://www.shnenglu.com/sixleaves/archive/2014/09/23/208389.html</link><dc:creator>swp</dc:creator><author>swp</author><pubDate>Tue, 23 Sep 2014 13:02:00 GMT</pubDate><guid>http://www.shnenglu.com/sixleaves/archive/2014/09/23/208389.html</guid><wfw:comment>http://www.shnenglu.com/sixleaves/comments/208389.html</wfw:comment><comments>http://www.shnenglu.com/sixleaves/archive/2014/09/23/208389.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/sixleaves/comments/commentRss/208389.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/sixleaves/services/trackbacks/208389.html</trackback:ping><description><![CDATA[<strong>在写E序中,我们l常要判断,或者写个@环,具体让其循环几次Q这个怎么计算?br />我分别D几个例子?br />eg1Qfor (i = 0; i < n; i++);<br />q个是大家再熟悉不过的了Qؓ什么它是nơ@环,如果我能用数学的角度看是q样的,首先i的取D围中Q每个整数对应一?br />循环Q所以所谓的循环ơ数Q也是q个范围内的整数的个数。而上q的循环范围是[0, n).它的整数个数是n - 0 = nQ只?br />对于半闭合半开攄区间能这栯,q也是ؓ什么从0开始计数的好处之一。一目了然?br />eg2Q?br />while(L--) {<br />}<br />q个循环循环几次呢,首先我们定L的有效范_׃是后|减减,所以有效范围是[L, 0).所以@环也是L?br />如果?br />while(--L){},那范围就是[L-1, 0),所以其循环ơ数是L - 1</strong><img src ="http://www.shnenglu.com/sixleaves/aggbug/208389.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/sixleaves/" target="_blank">swp</a> 2014-09-23 21:02 <a href="http://www.shnenglu.com/sixleaves/archive/2014/09/23/208389.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>UVA133http://www.shnenglu.com/sixleaves/archive/2014/09/23/208388.htmlswpswpTue, 23 Sep 2014 12:46:00 GMThttp://www.shnenglu.com/sixleaves/archive/2014/09/23/208388.htmlhttp://www.shnenglu.com/sixleaves/comments/208388.htmlhttp://www.shnenglu.com/sixleaves/archive/2014/09/23/208388.html#Feedback0http://www.shnenglu.com/sixleaves/comments/commentRss/208388.htmlhttp://www.shnenglu.com/sixleaves/services/trackbacks/208388.html题目描述

Z~短领救品的队伍,NNGLRP军_了以下策略:每天所有来甌救济品的Z被放在一个大圆圈Q面朝里面。选定一个h为编?1 P其他的就从那个h开始逆时针开始编L?N。一个官员一开始逆时针数Q数 k 个申误,然后另一个官员第 N 个始时针方向数 m 个申误,q两个hp送去再教肌Ӏ如果两个官员数的是同一个hQ那个h则被送去从政Q然?个官员再在剩下的人里面l选直到没人剩下来Q注意两个被?中的人是同时走掉的,所以就有可能两个官员选中一个h?/p>

[~辑]Input

输入含有多组试资料Q每l测试资料一列含有三个数 NQk ?mQk, m > 0Q?<N<20Q?当输入ؓ 0 0 0 代表输入l束?/p>

[~辑]Output

Ҏl测试资料输Z列。输选中的申误的~号序Q一对一对的Q。每个数的宽度ؓ 3 。每一对前面的那个~号为逆时针数的官员选出的,后面的那个编号ؓ时针数的官员选出的(但是如果q?个官员选出同一个hQ那只会有一个编P。每一?之间以逗号分开。格式请参考Sample Output?/p>

[~辑]Sample Input

10 4 3 
13 17 42
7 8 47
0 0 0

[~辑]Sample Output

 4 8, 9 5, 3 1, 2 6, 10, 7 
4 11, 10 1, 8 6, 13 7, 3, 5 12, 9 2
1 3, 5 7, 2 4, 6
q道题目有点l,也讲得不严密。这里主要说下几个容易错的地斏V?br />首先是你每次在写E序之前Q都要十分清除规则,题目中的人是围着一圈,而且W一个的左边是第N个hQ也是它是逆时针标L。这个十分关键?br />其次是go函数的实玎ͼgo函数是数qL个hQ返回最后一个的位置。我q不赞同Q某些版本数l是?开始计敎ͼ因ؓq样对于表达式的表达十分不方ѝ你可以
自己试?来做Q会很不方便。就是因为go函数是这样一个函敎ͼ所以当我们在下一ơP代的时候的开始位|,一定是为那个h出去的位|,也就是a[i]=0的位|?br />所以我们第一ơP代的位置Q原本A是应该在位置0QB在位|n-1。这时候只能是A在n-1和B?.Q你可以用数学归Ux理解Q?br />
 1 #include <stdio.h>
 2 
 3 #define MAXN 25
 4 int n,k,m;
 5 int a[MAXN];
 6 int go(int p, int d, int k);//数过k个hQ开始位|p必须是数1时候的前一个位|?nbsp;
 7 int main() {
 8     while (scanf("%d%d%d", &n, &k, &m) == 3 && n) {
 9         for (int i = 0; i < n; i++) {
10             a[i] = i + 1;
11         }
12         int left = n;
13         int pA = n-1, pB = 0;
14         int pANext,pBNext;
15         while (left) {
16             pA = go(pA, 1, k);//1表示逆时针,因ؓ它是逆时针标?br />17             pB = go(pB, -1, m);//-1表示时?br />18             printf("%3d", pA + 1); left--;
19             if (pA != pB) { printf("%3d", pB + 1); left--;}
20             a[pA] = a[pB] = 0;
21             if (left) printf(",");
22         }
23         printf("\n");
24     }    
25     return 0;
26 }
27 int go(int p, int d, int L) {
28     while (L--) {
29         do { p = (p+n+d)%n;} while(a[p] == 0);
30     }
31     return p;
32 }
解析:至于下一个位|ؓ什么是p = (p+n+d)%n.其实很简单。因为我们是一步步走的Q所以只有两U边界情c假讑ֽ前位|是p(0=<p<n),
W一U边界:p + 1 > n - 1Q即 p + 1此时应该是到?位置Q但此时p + 1 = nQ如果我们取余数Q则 Qp+1)%T = 0,T = n(T表示q个圆圈的周期大??br />刚好能符合,又因为T = nQ所?P+T+1)%Tq是不变的?br />W二U边? p - 1 < 0, ?p - 1此时的值是-1Q对于这U情况可以反q来看,它是向后退?个单位,可以看成向前走T - 1个单位即p -1 {效?p + T - 1
Q我们要{到此时的位|,再去余,(P+T-1)%T?br />对于情况一、二。可以归Uؓ(P+T+d)%T,当ؓ时针是d?Q否?1.

swp 2014-09-23 20:46 发表评论
]]>
A + B Problem II(hdu)http://www.shnenglu.com/sixleaves/archive/2014/09/04/208228.htmlswpswpThu, 04 Sep 2014 06:56:00 GMThttp://www.shnenglu.com/sixleaves/archive/2014/09/04/208228.htmlhttp://www.shnenglu.com/sixleaves/comments/208228.htmlhttp://www.shnenglu.com/sixleaves/archive/2014/09/04/208228.html#Feedback0http://www.shnenglu.com/sixleaves/comments/commentRss/208228.htmlhttp://www.shnenglu.com/sixleaves/services/trackbacks/208228.htmlq道题目是大数加法。ȝ一些一点经验?/strong>

Q?Q整值函数的应用。(q个不懂的话Q去看我Math栏目下有q个分析Q?/strong>

Q?Qsscanf、sprintf的应?/strong>

Q?Q分块计的格式问题?/strong>

先直接看代码Q?br />      1 #include <iostream>

 2 #include <cstdio>
 3 #include <vector>
 4 #include <cstring>
 5 using namespace std;
 6 struct BigInteger {
 7     static const int BASE = 100000000;
 8     static const int WIDTH = 8;
 9     vector<int> s;
10     
11     BigInteger(long long num = 0) {
12         *this = num;
13     }
14     
15     BigInteger operator=(long long num) {
16         s.clear();
17         do {
18             s.push_back(num % BASE);
19             num /= BASE;
20         }while (num > 0);
21         return *this;
22     }
23     
24 
25     BigInteger operator=(const string& str) {
26         s.clear();
27         int x;
28         int len = (str.size() - 1) / WIDTH + 1;//q里有h看不懂,L我写的整值函?nbsp;
29         for (int i = 0; i < len; i++) {
30             int end = str.size() - i * WIDTH;
31             int start = max(0, end - WIDTH);//定分割的区间ؓ[start, end)是左闭右开Q长度就是end - start 
32             sscanf(str.substr(start, end - start).c_str(), "%d", &x);
33             s.push_back(x);
34         }
35         return *this;
36     }
37     
38     BigInteger operator+(const BigInteger& bi) const{
39         BigInteger temp;
40         temp.s.clear();
41         for (int i = 0,g = 0;;i++) {
42             
43             if (g == 0 && i >= s.size() && i >= bi.s.size()) break;
44             int x = g;
45             if (i < s.size()) x += s[i];
46             if (i < bi.s.size()) x += bi.s[i];
47             temp.s.push_back(x % BASE);
48             g = x / BASE;
49         }
50         return temp;    
51     }    
52     
53     BigInteger operator +=(const BigInteger& b) {
54         *this = *this + b;return *this;
55     }
56 };
57 
58 istream& operator >>(istream &in, BigInteger& x) {
59     string s;
60     if (!(in >>s)) return in;
61     x = s;
62     return in;
63 }
64     
65 ostream& operator <<(ostream &out, BigInteger&bi) {
66     out << bi.s.back();
67     for (int i = bi.s.size()-2; i >= 0; i--) {//再从倒数W二个输?nbsp;
68         char buf[20];
69         sprintf(buf, "%08d", bi.s[i]);
70         //cout << buf;
71         for (int j =0; j < strlen(buf); j++) out << buf[j];
72     }
73     return out;
74 }
75 
76 int main() {
77     int n;
78     BigInteger a,b,c;
79     cin >> n;
80     int kase = 1;
81     while (n) {
82         cin >> a >> b;
83         c = a + b;
84         if (n != 1)
85            cout <<"Case " << kase++ << ":" << "\n" << a << " + " << b << " = " << c << endl << endl;
86         else
87            cout <<"Case " << kase++ << ":" << "\n" << a << " + " << b << " = " << c << endl;
88         n--;
89     }
90     return 0;
91 }
Q?Q(2Q略?br />Q?Q分块处理的坑:
前面两个我就不ȝ了,q里主要说下Q分块计的坑。假设有q个字符?123400000001"Q由于我们要其
按照?位字W分成一块所以第一块就?0000001、第二快是1234Q然后按照小端存储格式、低地址对应低位
但是但他们用sscanf格式化成整数时候,00000001不可能还是这Pq样是八进制了Q而是变成?Q所以我?br />在输出的时候,除了最高位对应的那一块可能不?位,剩下的块肯定都要8位处理,所以上面代码,才从W二快进?br />输出Q而且格式?08d.

swp 2014-09-04 14:56 发表评论
]]>
一些常?/title><link>http://www.shnenglu.com/sixleaves/archive/2014/08/02/207894.html</link><dc:creator>swp</dc:creator><author>swp</author><pubDate>Fri, 01 Aug 2014 21:00:00 GMT</pubDate><guid>http://www.shnenglu.com/sixleaves/archive/2014/08/02/207894.html</guid><wfw:comment>http://www.shnenglu.com/sixleaves/comments/207894.html</wfw:comment><comments>http://www.shnenglu.com/sixleaves/archive/2014/08/02/207894.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/sixleaves/comments/commentRss/207894.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/sixleaves/services/trackbacks/207894.html</trackback:ping><description><![CDATA[闰年记法:<br />      四年一?癑ֹ不闰,四百q再?x % 4  == 0 && x %100 != 0 || x % 400 == 0)<img src ="http://www.shnenglu.com/sixleaves/aggbug/207894.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/sixleaves/" target="_blank">swp</a> 2014-08-02 05:00 <a href="http://www.shnenglu.com/sixleaves/archive/2014/08/02/207894.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于大数法的ȝhttp://www.shnenglu.com/sixleaves/archive/2014/07/24/207767.htmlswpswpWed, 23 Jul 2014 18:58:00 GMThttp://www.shnenglu.com/sixleaves/archive/2014/07/24/207767.htmlhttp://www.shnenglu.com/sixleaves/comments/207767.htmlhttp://www.shnenglu.com/sixleaves/archive/2014/07/24/207767.html#Feedback0http://www.shnenglu.com/sixleaves/comments/commentRss/207767.htmlhttp://www.shnenglu.com/sixleaves/services/trackbacks/207767.html 大数加法是一十分十分基本的~程技能,好鸟不啰嗦鸟?br />
法核心思想Q?.字W串按照权重转换为整型数l中相应的位(0索引对应最低位Q权重ؓ1Q是个位)?.然后q行按照位相加运?br />
具体代码如下?br />
 2 //  main.cpp
 3 //  oj
 4 //
 5 //  Created by sixleaves on 14-7-23.
 6 //  Copyright (c) 2014q?nbsp;sixleaves. All rights reserved.
 7 //
 8 
 9 #include <iostream>
10 #include <string>
11 #include <cstdlib>
12 const int ArSize = 1024;
13 using namespace std;
14 char *psResult = new char[ArSize];//   分配于堆中,不是局部变?/span>
15 char* sum(string a, string b);

16 int main(int argc, const char * argv[])
17 {
18 
19     int nTestCase;
20     int i = 0;
21     cin >> nTestCase;
22     while (i < nTestCase) {
23         string a,b;
24         while (cin >> a >> b) {
25             cout << "Case " << i + 1 <<":"<< endl;
26             cout << a + " + " + b + " = "
27                  <<sum(a, b) << endl;
28             if(i + 1 != nTestCase)
29                 cout << endl;
30             i++;
31             break;
32         }
33     }
34     return 0;
35 }
36 
37 char* sum(string a, string b) {
38     //   q行数据的{换,把字W串数据转换为整?/span>
39     //   char *psResult = new char[ArSize];
        //   Z提高E序速度Q把q个攑֜了外面,不用每次都申?/span>
40     int nR[ArSize] = {0}, nA[ArSize] = {0}, nB[ArSize] = {0};//   q且都初始化?
41     int nLenA = a.length(), nLenB = b.length();
42     for(int i = 0; i < nLenA; i++) {
43         nA[i] = a[nLenA - i - 1] - '0';
44     }
45     for(int i = 0; i < nLenB; i++) {
46         nB[i] = b[nLenB - i - 1] - '0';
47     }
48     //   q行相加q算
49     int nLenMax = nLenA > nLenB? nLenA : nLenB;
50     for(int i = 0; i < nLenMax; i++) {
51         nR[i] += nA[i] + nB[i];
52         if(nR[i] > 9) {
53             nR[i] -= 10;
54             nR[i + 1]++;
55         }
56     }
57     //   转换为字W串
58     if(nR[nLenMax] != 0)//   如果最后一位相加有q位Q则总长度加1
59         nLenMax++;
60     for(int i = 0; i < nLenMax; i++) {
61         psResult[i] = nR[nLenMax - i - 1] + '0';
62     }
63     psResult[nLenMax] = '\0';
64     return psResult;
65 }
66 


swp 2014-07-24 02:58 发表评论
]]>
þþþþAŷAV| ޾ƷƬþ| Ļþۺ| þ޾Ʒһ | þۺɫHEZYO| þƵ| ޵һƷƷþ| Ʒþþþþô| þ㽶߿ۿ| þۺ㽶AV| ݺݺɫۺϾþð| 99þùۺϾƷˮ| ޹˾þۺһ77| vaþþþúݺ| ԭۺϾþ| þþþAVƬ| 996þùƷ߹ۿ| ŷ޹׾þþþþþ| ھƷþþĻ| ҹƷþþþþ| ձƷþþĻ| þþŮһ| þǿdŮ| þóӰԺƷ777| ҹþþƷ| 鶹wwwþùƷ| ƷƷھþø| 㽶þۺӰ| þۺav| þùƷ-þþƷ| þþƷҹһ| 㽶þƵ| þѹۿƵ| þþþþùƷ| ɫۺϾþ۾Ʒ| Ʒһþ| 91ƷɫۺϾþ| þ91˳ɵӰվ| Ʒþþþþø69| ŷvaþþþ| ɫۺϾþĻ|