CF 2 C. Commentator problem

题目:Commentator problem

思路:从重心开始向周围探测,看喵呜大神的代码过的


#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;
#define eps 1e-6
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct circle
{
    double x,y,r;
}c[3];
double p2(double x)
{
    return x*x;
}
double dis(double x,double y,double xx,double yy)
{
    return sqrt(p2(x-xx)+p2(y-yy));
}
double f(double x,double y)
{
    double tmp[3];
    for(int i=0;i<3;i++)
        tmp[i]=dis(x,y,c[i].x,c[i].y)/c[i].r;
    double ans=0;
    for(int i=0;i<3;i++)
        ans+=p2(tmp[i]-tmp[(i+1)%3]);
    return ans;
}
int main()
{
    double x=0,y=0;
    for(int i=0;i<3;i++)
    {
        scanf("%lf%lf%lf",&c[i].x,&c[i].y,&c[i].r);
        x+=c[i].x/3;
        y+=c[i].y/3;
    }
    double step=1;
    while(step>eps)
    {
        double tmp=f(x,y);
        int tag=-1;
        for(int i=0;i<4;i++)
        {
            double cnt=f(x+dir[i][0]*step,y+dir[i][1]*step);
            if(cnt<tmp)
            {
                tmp=cnt;
                tag=i;
            }
        }
        if(tag==-1)
            step/=2;
        else
        {
            x=x+dir[tag][0]*step;
            y=y+dir[tag][1]*step;
        }
    }
    if(f(x,y)<eps)
        printf("%.5lf %.5lf\n",x,y);
    return 0;
}


为您介绍一个基于Django的实战项目:一个简单的博客系统。该博客系统具有以下功能: 1.用户可以注册、登录、注销账户。 2.用户可以创建、编辑、删除博客文章。 3.用户可以查看其他用户发布的博客文章。 4.博客文章可以按照发布时间或者浏览量进行排序。 5.用户可以对博客文章进行评论。 6.用户可以对其他用户的评论进行回复。 以下是该博客系统的实现步骤: 1.创建Django项目 ```shell django-admin startproject myblog ``` 2.创建Django应用 ```shell python manage.py startapp blog ``` 3.定义模型 ```python from django.db import models from django.contrib.auth.models import User class Article(models.Model): title = models.CharField(max_length=100) content = models.TextField() author = models.ForeignKey(User, on_delete=models.CASCADE) created_time = models.DateTimeField(auto_now_add=True) modified_time = models.DateTimeField(auto_now=True) views = models.PositiveIntegerField(default=0) class Comment(models.Model): content = models.TextField() article = models.ForeignKey(Article, on_delete=models.CASCADE) commentator = models.ForeignKey(User, on_delete=models.CASCADE) created_time = models.DateTimeField(auto_now_add=True) parent_comment = models.ForeignKey('self', null=True, blank=True, on_delete=models.CASCADE, related_name='children') ``` 4.创建数据库表 ```shell python manage.py makemigrations python manage.py migrate ``` 5.编写视图函数 ```python from django.shortcuts import render, get_object_or_404 from django.http import HttpResponse from .models import Article, Comment from .forms import CommentForm def index(request): article_list = Article.objects.all().order_by('-created_time') return render(request, 'blog/index.html', context={'article_list': article_list}) def detail(request, article_id): article = get_object_or_404(Article, pk=article_id) article.views += 1 article.save(update_fields=['views']) form = CommentForm() comment_list = Comment.objects.filter(article=article) context = {'article': article, 'form': form, 'comment_list': comment_list } return render(request, 'blog/detail.html', context=context) def archives(request, year, month): article_list = Article.objects.filter(created_time__year=year, created_time__month=month ).order_by('-created_time') return render(request, 'blog/index.html', context={'article_list': article_list}) def category(request, category_id): pass def search(request): pass def login(request): pass def register(request): pass def logout(request): pass ``` 6.编写模板文件 ```html <!-- index.html --> {% for article in article_list %} <h2><a href="{% url 'blog:detail' article.id %}">{{ article.title }}</a></h2> <p class="date">{{ article.created_time }}</p> <p>{{ article.content|truncatechars:200 }}</p> {% endfor %} <!-- detail.html --> <h2>{{ article.title }}</h2> <p class="date">{{ article.created_time }}</p> <p>{{ article.content }}</p> <h3>评论</h3> <form method="post" action="{% url 'blog:comment' article.id %}"> {% csrf_token %} {{ form.as_p }} <button type="submit">发表评论</button> </form> {% for comment in comment_list %} <div class="comment"> <p>{{ comment.content }}</p> <p class="date">{{ comment.created_time }}</p> <p class="commentator">{{ comment.commentator }}</p> <a href="#comment-form">回复</a> {% for child_comment in comment.children.all %} <div class="child-comment"> <p>{{ child_comment.content }}</p> <p class="date">{{ child_comment.created_time }}</p> <p class="commentator">{{ child_comment.commentator }}</p> <a href="#comment-form">回复</a> </div> {% endfor %} </div> {% endfor %} <!-- base.html --> {% block content %} {% endblock %} ``` 7.编写表单 ```python from django import forms from .models import Comment class CommentForm(forms.ModelForm): class Meta: model = Comment fields = ['content'] ``` 8.配置URL ```python from django.urls import path from . import views app_name = 'blog' urlpatterns = [ path('', views.index, name='index'), path('article/<int:article_id>/', views.detail, name='detail'), path('archives/<int:year>/<int:month>/', views.archives, name='archives'), path('category/<int:category_id>/', views.category, name='category'), path('search/', views.search, name='search'), path('comment/<int:article_id>/', views.comment, name='comment'), path('login/', views.login, name='login'), path('register/', views.register, name='register'), path('logout/', views.logout, name='logout'), ] ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值