惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - Na57

ASP.NET MVC应用中一个诡异错误的处理 使用Enterprise Library 4.0 Logging 的问题 8. Action过滤 6. 视图和生成助手 5. 控制器和Action方法 2. 创建基本的MVC项目 3. URL路由 - Na57 - 博客园 4. 用MVC实现URL路由 Google笔记本迈向烂笔头 XML and Databases 笔记 烂笔头又来了 - Na57 - 博客园 GeoServer抛异常IllegalArgumentException的原因 CSS学习 关于CSS中float属性的理解 ItemCreated与ItemDataBound 《SAML简介:安全地共享数字身份信息》读后感 JavaScript 学习(2) - JS的内建对象 JavaScript from C#(入门篇) 2006.6《程序员》笔记
<程序员>200711期算法擂台的解答
Na57 · 2007-11-27 · via 博客园 - Na57

这期的题目是<完美的代码>,具体题目请看杂志.
解答程序如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 using System.IO;
 5 
 6 namespace Huiwen
 7 {
 8     class Program
 9     {
10         // 第一个参数是输入的文件的名称.
11         static void Main(string[] args)
12         {
13             // 从文件读取数据
14             StreamReader sr = File.OpenText(args[0]);
15             int length = Convert.ToInt32(sr.ReadLine());
16             char[] cs = sr.ReadLine().ToCharArray();
17 
18 
19             int count = 0;
20             
21             // 通过循环做置换
22             for (int i = 0; i < length/2; i++)
23             {
24                 char c = cs[i];
25                 
26                 // 从末尾找第一个与c相同的字符的位置.
27                 int index = LastIndex(cs, c, i+1, length - i - 1);
28 
29                 // 若未找到,则证明该字符串不能变为回文串.
30                 if (index == -1)
31                 {
32                     Console.WriteLine("Impossible");
33                     return;
34                 }
35 
36                 // 记录需要置换的次数.
37                 count += (length - 1 - i - index);
38 
39                 // 交换两个字符.
40                 cs[index] = cs[length - 1 - i];
41                 cs[length-1-i] = c;
42             }
43 
44             Console.WriteLine(count);
45         }
46 
47         // 从字符串数组中取字符串的位置索引.未找到则返回-1.
48         static int LastIndex(char[] ca, char c, int startIndex, int endIndex)
49         {
50             for (int i = endIndex; i >= startIndex; i--)
51             {
52                 if (ca[i].Equals(c)) return i;
53             }
54             return -1;
55         }
56     }
57 }
58 

这是我的想法,欢迎赐教.