Asp.net Core 入门实战
Asp.Net Core 是开源,跨平台,模块化,快速而简单的Web框架.
Asp.net Core官网的一个源码合集,方便一次性Clone
目录
持续更新,也可以通过我的网站访问,欢迎探讨交流
快速入门
安装
查看dotnet sdk 版本
$ dotnet --version`
2.1.4
创建项目目录
$ mkdir study
$ cd study
使用 dotnet new 命令创建项目
$ dotnet new console -n Demo
$ cd Demo
在 VS Code中打开Demo文件夹
一个最小的应用
打开 Program.cs
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseStartup<Startup>()
.Build();
host.Run();
}
}
class Startup
{
public void Configure(IApplicationBuilder app)
{
app.Run(async context =>
{
await context.Response.WriteAsync("Hello, World!");
});
}
}
}
安装相关的Nuget包
$ dotnet add package Microsoft.AspNetCore.Hosting --version 2.0.1
$ dotnet add package Microsoft.AspNetCore.Server.Kestrel --version 2.0.1
下载依赖项
$ dotnet restore
运行
$ dotnet run
输出结果:
Hosting environment: Production
Content root path: C:\netcore\study\Demo\bin\Debug\netcoreapp2.0\
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
项目模板
根据指定的模板,创建新的项目、配置文件或解决方案。
$ dotnet new -h
使用情况: new [选项]
选项:
-h, --help 显示有关此命令的帮助。
-l, --list 列出包含指定名称的模板。如果未指定名称,请列出所有模板。
-n, --name 正在创建输出的名称。如果未指定任何名称,将使用当前目录的名 称。
-o, --output 要放置生成的输出的位置。
-i, --install 安装源或模板包。
-u, --uninstall 卸载一个源或模板包。
--type 基于可用的类型筛选模板。预定义的值为 "project"、"item" 或 "other"。
--force 强制生成内容,即使该内容会更改现有文件。
-lang, --language 指定要创建的模板的语言。
模板 短名称 语言 标记
-------------------------------------------------------------------------------- ------------------------
Console Application console [C#], F#, VB Common/Console
Class library classlib [C#], F#, VB Common/Library
Unit Test Project mstest [C#], F#, VB Test/MSTest
xUnit Test Project xunit [C#], F#, VB Test/xUnit
ASP.NET Core Empty web [C#], F# Web/Empty
ASP.NET Core Web App (Model-View-Controller) mvc [C#], F# Web/MVC
ASP.NET Core Web App razor [C#] Web/MVC/Razor Pages
ASP.NET Core with Angular angular [C#] Web/MVC/SPA
ASP.NET Core with React.js react [C#] Web/MVC/SPA
ASP.NET Core with React.js and Redux reactredux [C#] Web/MVC/SPA
ASP.NET Core Web API webapi [C#], F# Web/WebAPI
global.json file globaljson Config
NuGet Config nugetconfig Config
Web Config webconfig Config
Solution File sln Solution
Razor Page page Web/ASP.NET
MVC ViewImports viewimports Web/ASP.NET
MVC ViewStart viewstart Web/ASP.NET
Examples:
dotnet new mvc --auth Individual
dotnet new page --namespace
dotnet new --help
使用dotnet new 选择 console 模板 创建项目
$ dotnet new console -n Demo
$ cd Demo
创建后的项目文档结构是
.
├── Demo
│ ├── Demo.csproj
│ └── Program.cs
路由
启用,配置路由
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
namespace Demo {
class Startup {
public void ConfigureServices (IServiceCollection services) {
//启用路由
services.AddRouting ();
}
public void Configure (IApplicationBuilder app) {
//配置路由
app.UseRouter (routes => {
//根路由 (/)
routes.MapGet("", context =>
{
return context.Response.WriteAsync("Hello, World!");
});
//在根路由 (/) 上,配置 /hello Get请求的路由
routes.MapGet ("hello", context => {
return context.Response.WriteAsync ("Got a Get request at /hello");
});
});
}
}
}
安装相关的Nuget包
$ dotnet add package Microsoft.AspNetCore.Routing --version 2.0.1
下载依赖项
$ dotnet restore
运行
$ dotnet run
访问 http://localhost:5000/hello ,输出结果:
Got a Get request at /hello
对 /user 路由的 POST 请求进行响应:
routes.MapPost("hello", context => {
return context.Response.WriteAsync ("Got a Post request at /hello");
})
对 /user 路由的 PUT 请求进行响应:
routes.MapPut("hello", context => {
return context.Response.WriteAsync ("Got a Put request at /hello");
})
对 /user 路由的 DELETE 请求进行响应:
routes.MapDelete("hello", context => {
return context.Response.WriteAsync ("Got a Dlete request at /hello");
})
静态文件
启用,配置静态资源
创建 wwwroot文件夹
$ mkdir wwwroot
$ cd wwwroot
复制 index.html 到 wwwroot目录
$ mkdir images
$ cd images
复制 favicon.png 到 images文件夹下
项目结构图如下:
.
├── Demo
│ ├── wwwroot
│ │ ├── images
│ │ │ ├── favicon.png
│ │ ├── index.html
│ ├── Demo.csproj
│ └── Program.cs
更改 Program.cs,设置ContentRoot路径为当前项目根目录,启用静态资源
using System;
using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
namespace Demo {
class Program {
static void Main (string[] args) {
Console.WriteLine (AppContext.BaseDirectory);
var host = new WebHostBuilder ()
.UseKestrel ()
//设置 ContentRoot, ContentRoot是任何资源的根路径,比如页面和静态资源
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.Build ();
host.Run ();
}
}
class Startup {
public void Configure (IApplicationBuilder app) {
//使用静态资源,默认的目录是 /wwwroot
app.UseStaticFiles ();
}
}
}
运行
http://localhost:5000/index.html
http://localhost:5000/images/favicon.png
配置其他文件夹
新建文件夹myfiles
$ mkdir myfiles
$ cd myfiles
创建一个新的 index.html到myfiles文件夹,配置并使用myfiles文件夹
app.UseStaticFiles (new StaticFileOptions {
FileProvider = new PhysicalFileProvider (
Path.Combine (Directory.GetCurrentDirectory (), "myfiles")),
RequestPath = "/myfiles"
});
运行
http://localhost:5000/myfiles/index.html
页面渲染
Razor模板引擎
Asp.net Core自带的模板引擎是 Razor模板引擎,传统的Razor页面以cshtml结尾, 阅读Razor语法
遗憾的是Razor模板引擎在Asp.net Core中封装在了MVC模块之中,需要做一些扩展才可以单独拿出来使用
创建模板页面
创建 views文件夹
$ mkdir views
$ cd views
创建 index.cshtml 页面
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<ul>
@for (int i = 0; i < 5; i++)
{
<li>第@(i + 1)行</li>
}
</ul>
</body>
</html>
使用模板
using System;
using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(AppContext.BaseDirectory);
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.Build();
host.Run();
}
}
class Startup
{
public void ConfigureServices(IServiceCollection services)
{
//启用Mvc
services.AddMvc();
//扩展类
services.AddSingleton<RazorViewRenderer>();
}
public void Configure(IApplicationBuilder app)
{
app.UseRouter(routes =>
{
//根路径 /
routes.MapGet("", context =>
{
return context.Render("/views/index.cshtml");
}
});
}
}
}
安装相关的Nuget包
$ dotnet add package Microsoft.AspNetCore.Mvc --version 2.0.2
下载依赖项
$ dotnet restore
运行
$ dotnet run
结果
第1行
第2行
第3行
第4行
第5行
渲染数据
新增实体类 UserInfo
public class UserInfo
{
public string Name { get; set; }
public int Age { get; set; }
}
新增user.cshtml页面
@using Demo;
@model UserInfo
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<ul>
<li>姓名:@Model.Name</li>
<li>年龄:@Model.Age</li>
</ul>
</body>
</html>
更改Program.cs
app.UseRouter(routes =>
{
//根路径 /
routes.MapGet("", context =>
{
return context.Render("/views/index.cshtml");
}
routes.MapGet("/user", context =>
{
return context.Render("/views/user.cshtml", new UserInfo() { Name = "张三", Age = 18 });
});
});
运行
$ dotnet run
结果
姓名:张三
年龄:18
请求数据
QueryString
var queryCollection = context.Request.Query;
foreach (var item in queryCollection)
{
Console.WriteLine(item.Key + ":" + item.Value);
}
Form
if (context.Request.ContentType.ToLower().Contains("application/x-www-form-urlencoded")
{
var formCollection = context.Request.Form;
foreach (var item in formCollection)
{
Console.WriteLine(item.Key + ":" + item.Value);
}
}
Files
var fileCollections = context.Request.Form.Files;
var rootPath = context.RequestServices.GetService<IHostingEnvironment>().ContentRootPath;
foreach (var item in fileCollections)
{
var path = Path.Combine(rootPath,item.FileNa
using (var stream = new FileStream(path, FileMode.Create))
{
await item.CopyToAsync(stream);
}
Console.WriteLine(item.FileName + ":" + item.Length);
}
Header
var headerCollection = context.Request.Headers;
foreach (var item in headerCollection)
{
Console.WriteLine(item.Key + ":" + item.Value);
}
Body
StreamReader reader = new StreamReader(context.Request.Body);
string text = reader.ReadToEnd();
Cookies
var cookieCollection=context.Request.Cookies;
foreach (var item in cookieCollection)
{
Console.WriteLine(item.Key + ":" + item.Value);
}
错误和重定向
重定向
context.Response.Redirect("path")
状态代码页
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.IO;
using System.Text.Encodings.Web;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(AppContext.BaseDirectory);
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.Build();
host.Run();
}
}
class Startup
{
public void ConfigureServices(IServiceCollection services)
{
//启用路由
services.AddRouting();
services.AddMvc();
services.AddSingleton<RazorViewRenderer>();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
//启用状态码页
app.UseStatusCodePages();
app.UseRouter(routes =>
{
//根路径 /
routes.MapGet("", context =>
{
return context.Render("/views/index.cshtml");
}
});
}
}
}
自定义状态码页
新增 404.cshtml
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<h1> Oops! Page Not Found ! </h1>
</body>
</html>
配置和使用自定义的状态码页
app.UseStatusCodePagesWithRedirects("/status/{0}");
app.UseRouter(routes =>
{
//""
routes.MapGet("", context =>
{
return context.Response.WriteAsync("root path");
});
routes.MapGet("status/{code}", (request, response, routeData) =>
{
var statusCodePagesFeature = request.HttpContext.Features.Get<IStatusCodePagesFeature>();
var code = routeData.Values["code"];
if (statusCodePagesFeature != null && code!=null && code.ToString() == "404")
{
//跳转到自定义的404页面
return request.HttpContext.Render("/views/404.cshtml");
}
else
{
return response.WriteAsync(HtmlEncoder.Default.Encode("状态码:"+ routeData.Values["code"]));
}
});
});
异常错误页
开发异常页面,Asp.net Core自带
app.UseDeveloperExceptionPage();
新增测试路由,抛出空指针异常
env.EnvironmentName = EnvironmentName.Development;
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/error");
}
app.UseRouter(routes =>
{
//""
routes.MapGet("", context =>
{
return context.Response.WriteAsync("root path");
}
//test
routes.MapGet("test", context =>
{
throw new Exception("空指针异常!!!");
});
});
访问http://localhost:57393/test,得到下面的页面

自定义异常页面
env.EnvironmentName = EnvironmentName.Production;
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
//自定义异常错误页面
app.UseExceptionHandler("/error");
}
app.UseRouter(routes =>
{
//""
routes.MapGet("", context =>
{
return context.Response.WriteAsync("root path");
}
//test
routes.MapGet("test", context =>
{
throw new Exception("空指针异常!!!");
});
//自定义异常错误页面
routes.MapGet("error", context =>
{
context.Response.StatusCode = 500;
context.Response.ContentType = "text/html";
var error = context.Features.Get<IExceptionHandlerFeature>();
if (error != null)
{
return context.Response.WriteAsync("<h1>" + HtmlEncoder.Default.Encode("发生了错误: " + error.Error.Messa + "</h1>");
}
else
{
return context.Response.WriteAsync("<h1>" + HtmlEncoder.Default.Encode("Oops! 发生了错误,请联系管理员")"</h1>");
}
});
});
关于响应
会话
Session
新增登陆页面login.cshtml
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<form action="/login" method="post">
<div>
<label>用户名:</label>
<input type="text" name="UserName" />
</div>
<div>
<label>密码:</label>
<input type="password" name="PassWord" />
</div>
<div>
<input type="submit" value="登陆" />
</div>
</form>
</body>
</html>
添加并启用Session,输入用户名和密码,点击登陆,保持Session数据,登陆成功跳转到/home路由页面,访问Session数据,显示当前登陆的用户名
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.IO;
using System.Threading.Tasks;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(AppContext.BaseDirectory);
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.Build();
host.Run();
}
}
class Startup
{
public void ConfigureServices(IServiceCollection services)
{
//启用路由
services.AddRouting();
services.AddMvc();
services.AddSingleton<RazorViewRenderer>();
//增加Session
services.AddSession();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseDeveloperExceptionPage();
// 启用Session
app.UseSession();
app.UseRouter(routes =>
{
routes.MapGet("", (request, response, routeData) =>
{
return request.HttpContext.Render("/views/login.cshtml");
});
routes.MapGet("home", context =>
{
string userName = context.Session.GetString("userName");
if (!string.IsNullOrEmpty(userName))
{
return context.Response.WriteAsync("User:" + userName);
}
else
{
context.Response.Redirect("/");
return Task.CompletedTask;
}
});
routes.MapPost("login", context =>
{
var userName = context.Request.Form["UserName"].ToString();
var password = context.Request.Form["PassWord"].ToString();
if (userName.Equals("admin") && password.Equals("123456"))
{
context.Session.SetString("userName", password);
context.Response.Redirect("/home");
return Task.CompletedTask;
}
else
{
throw new Exception("用户名或密码错误");
}
});
});
}
}
}
日志
安装Nuget包
$ dotnet add package Microsoft.Extensions.Logging --version 2.0.0
$ dotnet add package Microsoft.Extensions.Logging.Console --version 2.0.0
下载依赖项
$ dotnet restore
添加日志模块
//启用日志
services.AddLogging(builder=> {
//使用控制台日志提供程序
builder.AddConsole();
});
创建ILogger实例
var logger = context.RequestServices.GetService<ILogger<Startup>> ();
//记录日志
logger.LogInformation("before hello world");
日志分类
日志的类别,可以是任意字符串,默认是完全限定的类名,例如: Demo.Startup
日志级别
logger.LogTrace();
logger.LogDebug();
logger.LogInformation();
logger.LogWarning();
logger.LogError();
logger.LogCritical();
日志过滤
给日志分类为 Demo.Startup的日志类别限定最小的日志级别是LogLevel.Information
services.AddLogging(builder=> {
builder.AddFilter("Demo.Startup", LogLevel.Information);
builder.AddConsole();
});
访问更多关于日志类别,级别,模板,过滤,作用域的说明日志类别
配置
添加配置
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
var configBuilder = new ConfigurationBuilder();
configBuilder.AddInMemoryCollection(new Dictionary<string, string>() {
{ "name","zhangsan"},
{ "age","18"}
});
var config = configBuilder.Build();
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseConfiguration(config)
.UseStartup<Startup>()
.Build();
host.Run();
}
}
}
读取配置
app.UseRouter(routes =>
{
routes.MapGet("", context =>
{
var config = context.RequestServices.GetService<IConfiguration>();
string name = config["name"];
string age = config["age"];
return context.Response.WriteAsync("name:"+name+",Age:"+age);
});
});
国际化
添加依赖的Nuget包
$ dotnet add package Microsoft.Extensions.Localization --version 2.0.1
下载依赖项
$ dotnet restore
资源文件命名规则
命名规则 {分类名称}.{语言区域名称}.resx
- 分类名称:默认的分类名称就是类型的FullName,在AssemblyName后的相对路径,比如Demo.Startup,程序集的名称Demo,就剩下了Startup
- 语言区域名称: 语言区域名称有zh-CN,en-US,更多
特殊条件下你也可以将资源文件和类定义文件放在同一个目录,这样资源文件的分类名称就是{类名称}.{语言区域名称}.resx
添加资源文件
- 新建 myresources文件夹
- 新建 Startup.en-US.resx 新增项 name是sayhi,value是 HelloWorld
- 新建 Startup.zh-CN.resx 新增项 name是sayhi,value是 世界你好
添加本地化
services.AddLocalization(options =>
options.ResourcesPath = "myresources";
});
启用本地化
var cultureList = new List<CultureInfo>() {
new CultureInfo("en-US"),
new CultureInfo("zh-CN")
};
var options = new RequestLocalizationOptions()
{
SupportedCultures = cultureList,
SupportedUICultures = cultureList,
DefaultRequestCulture = new RequestCulture("zh-CN")
};
//新建基于Query String的多语言提供程序
var provider = new QueryStringRequestCultureProvider();
provider.QueryStringKey = "culture";
provider.UIQueryStringKey = "uiculture";
//删除所有多语言提供程序
options.RequestCultureProviders.Clear();
options.RequestCultureProviders.Add(provider);
资源文件读取
var stringLocalizer = context.RequestServices.GetService<IStringLocalizer<Startup>>();
string sayhi = stringLocalizer["sayhi"];
访问 http://localhost:57393/?culture=en-US 切换为英文版本,默认的语言是中文
多语言提供程序
- 通过Query String,[默认自带]
- 通过Cookie,[默认自带]
- 通过Accept-Language header,[默认自带]
- 通过RouteData
模板格式化
string username="张三";
string localizedString = _localizer["Hello {0}!", username];
Asp.net Core 入门实战的更多相关文章
- Asp.net Core 入门实战 2.请求流程
Asp.Net Core 是开源,跨平台,模块化,快速而简单的Web框架. Asp.net Core官网的一个源码合集,方便一次性Clone,喜欢的(Star),本系列持续更新,也可以通过我的网站访问 ...
- net Core 入门实战
Asp.net Core 入门实战 Asp.Net Core 是开源,跨平台,模块化,快速而简单的Web框架. Asp.net Core官网的一个源码合集,方便一次性Clone 目录 快速入门 安 ...
- 【无私分享:ASP.NET CORE 项目实战】目录索引
简介 首先,我们的 [无私分享:从入门到精通ASP.NET MVC] 系列已经接近尾声,希望大家在这个过程中学到了一些思路和方法,而不仅仅是源码. 因为是第一次写博客,我感觉还是比较混乱的,其中 ...
- 【无私分享:ASP.NET CORE 项目实战(第三章)】EntityFramework下领域驱动设计的应用
目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 在我们 [无私分享:从入门到精通ASP.NET MVC] 系列中,我们其实也是有DDD思想的,但是没有完全的去实现,因为并不是 ...
- 【无私分享:ASP.NET CORE 项目实战(第二章)】添加EF上下文对象,添加接口、实现类以及无处不在的依赖注入(DI)
目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 上一章,我们介绍了安装和新建控制器.视图,这一章我们来创建个数据模型,并且添加接口和实现类. 添加EF上下文对象 按照我们以前 ...
- Asp.Net Core 项目实战之权限管理系统(4) 依赖注入、仓储、服务的多项目分层实现
0 Asp.Net Core 项目实战之权限管理系统(0) 无中生有 1 Asp.Net Core 项目实战之权限管理系统(1) 使用AdminLTE搭建前端 2 Asp.Net Core 项目实战之 ...
- Asp.Net Core 项目实战之权限管理系统(0) 无中生有
0 Asp.Net Core 项目实战之权限管理系统(0) 无中生有 1 Asp.Net Core 项目实战之权限管理系统(1) 使用AdminLTE搭建前端 2 Asp.Net Core 项目实战之 ...
- Asp.Net Core 项目实战之权限管理系统(1) 使用AdminLTE搭建前端
0 Asp.Net Core 项目实战之权限管理系统(0) 无中生有 1 Asp.Net Core 项目实战之权限管理系统(1) 使用AdminLTE搭建前端 2 Asp.Net Core 项目实战之 ...
- Asp.Net Core 项目实战之权限管理系统(2) 功能及实体设计
0 Asp.Net Core 项目实战之权限管理系统(0) 无中生有 1 Asp.Net Core 项目实战之权限管理系统(1) 使用AdminLTE搭建前端 2 Asp.Net Core 项目实战之 ...
随机推荐
- flask-日料网站搭建
引言:想使用python的flask框架搭建一个日料网站,主要包含web架构,静态页面,后台系统,交互. 本节知识:搭建web目录,目前正在copy网站. python环境:python2.7,fla ...
- IdentityServer Topics(4)- 登录
为了使IdentityServer代表用户发布令牌,该用户必须登录到IdentityServer. Cookie认证 使用来自ASP.NET Core的cookie身份验证处理程序管理的cookie跟 ...
- Yii2基本概念之——属性(property)
学习任何一门学问,往往都是从起基本的概念学起.万丈高楼平地起,这些基本概念就是高楼的基石,必须做详尽的分析.我们知道,Yii2是一款脉络清晰的框架,理顺了基础的概念和基本功能,学习更高级和复杂的功能就 ...
- Java与算法之(5) - 老鼠走迷宫(深度优先算法)
小老鼠走进了格子迷宫,如何能绕过猫并以最短的路线吃到奶酪呢? 注意只能上下左右移动,不能斜着移动. 在解决迷宫问题上,深度优先算法的思路是沿着一条路一直走,遇到障碍或走出边界再返回尝试别的路径. 首先 ...
- Gym 101612C Consonant Fencity
原题传送门 题意:给定一个只含小写字母的字符串,假设aeiouyw为元音字母,现在要把一些字母变为大写,要求相同字母的大小写必须相同,统计变化后的字符串中有多少对相邻.同为辅音字母且大小写不一样的字符 ...
- HDU 2503 a/b + c/d(最大公约数与最小公倍数,板子题)
话不多说,日常一水题,水水更健康!┗|`O′|┛ 嗷~~ a/b + c/d Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768 ...
- 洛谷 P1876 开灯(思维,枚举,规律题)
P1876 开灯 题目背景 该题的题目是不是感到很眼熟呢? 事实上,如果你懂的方法,该题的代码简直不能再短. 但是如果你不懂得呢?那...(自己去想) 题目描述 首先所有的灯都是关的(注意是关!),编 ...
- Codeforces Round #416 (Div. 2)(A,思维题,暴力,B,思维题,暴力)
A. Vladik and Courtesy time limit per test:2 seconds memory limit per test:256 megabytes input:stand ...
- hdu_2089(数位dp)
hdu_2089(数位dp) 标签: dp 我初次接触数位dp表面上看上去挺简单,但是仔细学还是要考虑很多细节的.wa了无数次,这里引入一个 很好地博客 #include<cstdio> ...
- use ambiguous的错误——编译错误
出现这样的问题是因为namespace std里面已经有一个count了,而 using namespace std;语句把该namespace 打开了,这导致了后面的引用不明确: 不过这里也可以把u ...