3/26 NEFUOJ题目

函数阶乘
Problem:824
Time Limit:1000ms
Memory Limit:65536K
Description
从键盘输入一个数值n(n <= 15),计算出数值n的阶乘。
Input
输入数据有多组,每组1个整数值n (0 <= n <=15)。
Output
对应输入数值的阶乘的值。
Sample Input
0
1
2
3
10
Sample Output
1
1
2
6
3628800
Hint
Source

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    while(cin>>n)
    {
        int ans=1;
       if(n==0)
       {
           cout<<"1"<<endl;
       }
       else
       {for(int i=n;i>0;i--)
       {
           ans=ans*i;
       }
       cout<<ans<<endl;}
    }
    return 0;
}

求斐波那契数函数版
Problem:827
Time Limit:1000ms
Memory Limit:65536K
Description
根据斐波那契递推关系,求解斐波那契数值
斐波那契数列,又称黄金分割数列,指的是这样一个数列:1、1、2、3、5、8、13、21、……在数学上,斐波纳契数列以如下被以递归的方法定义:F0=0,F1=1,Fn=F(n-1)+F(n-2)
Input
输入数据有多组,一个正整数表示要求取的第几项斐波那契数值;(2 <= n <= 40)
Output
输出斐波那契数值
Sample Input
5
8
10
20
Sample Output
5
21
55
6765
Hint
Source

#include <bits/stdc++.h>

using namespace std;
int f(int a)
{
    if(a==1)
    {
        return 1;
    }
    if(a==2)
    {
        return 1;
    }
    return f(a-1)+f(a-2);
}
int main()
{
    int n,ans=0;
    while(cin>>n)
    {
        cout<<f(n)<<endl;
    }
    return 0;
}

求斐波那契数函数版
Problem:827
Time Limit:1000ms
Memory Limit:65536K
Description
根据斐波那契递推关系,求解斐波那契数值
斐波那契数列,又称黄金分割数列,指的是这样一个数列:1、1、2、3、5、8、13、21、……在数学上,斐波纳契数列以如下被以递归的方法定义:F0=0,F1=1,Fn=F(n-1)+F(n-2)
Input
输入数据有多组,一个正整数表示要求取的第几项斐波那契数值;(2 <= n <= 40)
Output
输出斐波那契数值
Sample Input
5
8
10
20
Sample Output
5
21
55
6765
Hint
Source

#include <bits/stdc++.h>

using namespace std;
int f(int a)
{
    if(a==1)
    {
        return 1;
    }
    if(a==2)
    {
        return 1;
    }
    return f(a-1)+f(a-2);
}
int main()
{
    int n,ans=0;
    while(cin>>n)
    {
        cout<<f(n)<<endl;
    }
    return 0;
}

### 三级标题:爬取指定高校网站信息的Python爬虫代码 为了从目标网站 `https://www.nefu.edu.cn/` 爬取通知、规章制度和院系介绍信息,以下提供一个完整的 Python 爬虫示例。此代码基于 `requests` 和 `BeautifulSoup` 实现,适用于静态页面数据提取。 ```python import requests from bs4 import BeautifulSoup import pandas as pd import os # 设置基础URL base_url = "https://www.nefu.edu.cn/" # 获取页面内容 def fetch_page(url): headers = { &#39;User-Agent&#39;: &#39;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36&#39; } response = requests.get(url, headers=headers) response.encoding = &#39;utf-8&#39; # 根据网页编码设置 return response.text # 解析并提取通知信息 def parse_notice(html): soup = BeautifulSoup(html, &#39;html.parser&#39;) notices = [] for item in soup.select(&#39;.notice-list li&#39;): # 假设通知列表在类名为.notice-list的<ul>或<ol>中 title = item.find(&#39;a&#39;).text.strip() link = item.find(&#39;a&#39;)[&#39;href&#39;] full_link = base_url + link if link.startswith(&#39;/&#39;) else link notices.append({&#39;title&#39;: title, &#39;link&#39;: full_link}) return notices # 解析并提取规章制度信息 def parse_regulations(html): soup = BeautifulSoup(html, &#39;html.parser&#39;) regulations = [] for item in soup.select(&#39;.regulation-list li&#39;): # 假设规章制度在类名为.regulation-list的元素中 title = item.find(&#39;a&#39;).text.strip() link = item.find(&#39;a&#39;)[&#39;href&#39;] full_link = base_url + link if link.startswith(&#39;/&#39;) else link regulations.append({&#39;title&#39;: title, &#39;link&#39;: full_link}) return regulations # 解析并提取院系介绍信息 def parse_departments(html): soup = BeautifulSoup(html, &#39;html.parser&#39;) departments = [] for item in soup.select(&#39;.department-list li&#39;): # 假设院系介绍在类名为.department-list的元素中 name = item.find(&#39;h3&#39;).text.strip() intro = item.find(&#39;p&#39;).text.strip() link = item.find(&#39;a&#39;)[&#39;href&#39;] full_link = base_url + link if link.startswith(&#39;/&#39;) else link departments.append({&#39;name&#39;: name, &#39;intro&#39;: intro, &#39;link&#39;: full_link}) return departments # 保存数据到CSV文件 def save_to_csv(data, filename): df = pd.DataFrame(data) df.to_csv(filename, index=False, encoding=&#39;utf-8-sig&#39;) # 主程序入口 def main(): # 爬取通知页面 notice_html = fetch_page(base_url + &#39;tzgg.htm&#39;) # 假设通知页面为/tzgg.htm notices = parse_notice(notice_html) save_to_csv(notices, &#39;nefu_notices.csv&#39;) # 爬取规章制度页面 regulation_html = fetch_page(base_url + &#39;szdw.htm&#39;) # 假设规章制度页面为/szdw.htm regulations = parse_regulations(regulation_html) save_to_csv(regulations, &#39;nefu_regulations.csv&#39;) # 爬取院系介绍页面 department_html = fetch_page(base_url + &#39;xyjs.htm&#39;) # 假设院系介绍页面为/xyjs.htm departments = parse_departments(department_html) save_to_csv(departments, &#39;nefu_departments.csv&#39;) print("数据已成功保存为CSV文件") if __name__ == &#39;__main__&#39;: main() ``` #### 功能说明: 1. **发送请求**:使用带有 User-Agent 的请求头模拟浏览器访问,避免被反爬机制拦截。 2. **解析HTML**:利用 `BeautifulSoup` 对 HTML 内容进行结构化解析,通过 CSS 选择器定位目标数据区域。 3. **提取通知信息**:遍历 `.notice-list` 中的 `<li>` 元素,获取标题与链接,并构造完整 URL。 4. **提取规章制度信息**:类似逻辑提取 `.regulation-list` 中的数据。 5. **提取院系介绍信息**:从 `.department-list` 提取院系名称、简介及详情链接。 6. **保存数据**:将提取的数据保存为 CSV 文件,便于后续分析或展示。 #### 注意事项: - 若目标页面存在 JavaScript 渲染内容(如异步加载),建议改用 `Selenium` 或 `Playwright` 进行动态渲染处理。 - 需要根据实际页面结构调整 CSS 选择器路径以匹配真实 DOM 结构。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值