头像

小牛马




离线:3小时前


最近来访(14)
用户头像
正则灵均
用户头像
WAWA大哭
用户头像
斯卡布罗
用户头像
封禁用户
用户头像
vlily

活动打卡代码 Linux 3.1. homework_1

#! /bin/bash

if [ $# -ne 1 ]
then
    echo arguments not valid
    exit 1
fi

if ! [ -e "$1" ]
then
    echo not exist
    exit 2
fi

if [ -f "$1" ]
then
    echo regulare file
fi

if [ -d "$1" ]
then
    echo directory
fi

if [ -r "$1" ]
then
    echo readable
fi

if [ -w "$1" ]
then
    echo writable
fi

if [ -x "$1" ]
then
    echo executable
fi


活动打卡代码 Linux 3.4. homework_4

#! /bin/bash

input_file=$1
output_file=$2

read n < $input_file

sum=0
for ((i = 1; i <= n; i ++ ))
do
    sqr=`expr $i \* $i`
    sum=`expr $sum + $sqr`
done

echo $sum > $output_file


活动打卡代码 Linux 3.2. homework_2

#! /bin/bash

read n

a[0]=1
a[1]=1
for((i = 2; i <= n; i ++ ))
do
    x=$(expr $i - 1)
    y=$(expr $i - 2)
    a[$i]=$(expr ${a[$x]} + ${a[$y]})
done

echo ${a[$n]}


活动打卡代码 Linux 3.3. homework_3

写的时候注意if用的中括号,if和中括号之间要有空格,中括号里面的条件两边各加一个空格,否则会报错
! /bin/bash

read n
read m

for ((i = 1; i <= n; i ++ ))
do
    st[$i]=0
done

dfs(){
    if [ $1 -eq $n ]
    then                                                                                                                                                                     
        m=`expr $m - 1`
        if [ $m -eq 0 ] 
        then
            for((i = 0; i < n; i ++ ))
            do
                echo -e "${path[$i]} \c"
            done
            echo ''
            return 0
        fi
        return 1
    fi  

    local j=0
    for ((j = 1; j <= n; j++ ))
    do  
        if [ ${st[$j]} -eq 0 ] 
        then
            path[$1]=$j
            st[$j]=1
            if dfs `expr $1 + 1`
            then
                return 0
            fi

            st[$j]=0
        fi
    done

    return 1
}

dfs 0




小牛马
10天前
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 1e5 + 10;

int n, m;
int h[N], e[N], ne[N], idx;
int q[N], d[N];//q是队列,d表示每个数的入度

void add(int a, int b)
{
    e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}

int topsort()
{
    int hh = 0, tt = -1;

    for(int i = 1; i <= n; i ++)//必须从1开始,因为排序的数字是从1开始的
    {
        if(!d[i]) q[ ++ tt] = i;
    }

    while(hh <= tt)//剩下的都是入度不为0的
    {
        int t = q[hh ++];
        for(int i = h[t]; i != -1; i = ne[i])
        {
            int j = e[i];//无需判断,入度为0的已经加入了, 剩下的都是入度不为0的
            d[j] --;
            if(!d[j])
            q[ ++ tt] = j;
        }
    }
    return tt == n - 1;//因为对头是从0开始的所以判断的时候要让队尾等于n-1
}

int main()
{
    cin >> n >> m;

    memset(h, -1, sizeof h);

    for(int i = 0; i < m; i ++)
    {
        int a, b;
        cin >> a >> b;
        add(a,b);
        d[b] ++;
    }

    if(topsort())
    {
        for(int i = 0; i < n; i ++)//因为所有入度为0的数都加到队列里面,所有直接输出队列就ok
        printf("%d ",q[i]);
    }
    else puts("-1");

    return 0;
}



小牛马
11天前
#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

const int N = 1e5 + 10;

int n;//输出n - 1条边
int h[N], e[N*2], ne[N*2], idx;//存储结构类似拉链法
int ans = N;//答案
bool st[N];//判断是否走过

void add(int a, int b)//相当于往a后面插入了一个b
{
    e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}

int dfs(int u)
{
    st[u] = true;//标记一下以u为根的节点走过了

    int sum = 1, res = 0;//sum是统计以u为根的叶子节点个数,res是每一个子链的最大值
    for(int i = h[u]; i != -1; i = ne[i])
    {
        int j = e[i];
        if(!st[j])
        {
            int s = dfs(j);
            res = max(res, s);//更新res为每一个子链的最大值
            sum += s;//计算以u为根的叶子节点之和
        }
    }

    res = max(res, n - sum);//每一个子链的最大值和n-sum再取最大值,结果就是删除u之后的最大连通块中点的个数
    ans = min(res, ans);//最大值的最小值

    return sum;//返回sum是因为需要迭代res
}

int main()
{
    cin >> n;

    memset(h, -1, sizeof h);

    for(int i = 0; i < n - 1; i ++)//n个点只有n - 1条边
    {
        int a, b;
        cin >> a >> b;
        add(a,b);
        add(b,a);
    }

    dfs(1);

    cout << ans << endl;

    return 0;
}


活动打卡代码 AcWing 3.0. homework_0

小牛马
14天前
vim helper.sh
chmod +x helper.sh
#! /bin/bash                                                                                                                                                                

# ********** homework_0 *********                                                                                                                                           

homework 1 create 0                                                                                                                                                         

dir0=/home/acs/homework/lesson_1/homework_0                                                                                                                                 

for i in dir_a dir_b dir_c                                                                                                                                                  
do                                                                                                                                                                          
    mkdir ${dir0}/$i                                                                                                                                                        
done                                                                                                                                                                        

# ********** homework_1 *********                                                                                                                                           

homework 1 create 1                                                                                                                                                         

dir1=/home/acs/homework/lesson_1/homework_1                                                                                                                                 

for i in a.txt b.txt c.txt                                                                                                                                                  
do                                                                                                                                                                          
    cp ${dir1}/${i} ${dir1}/${i}.bak                                                                                                                                        
done                                                                                                                                                                        

# ********** homework_2 *********                                                                                                                                           

homework 1 create 2

homework 1 create 2                                                                                                                                                         

dir2=/home/acs/homework/lesson_1/homework_2                                                                                                                                 

for i in a b c
do
    mv ${dir2}/${i}.txt ${dir2}/${i}_new.txt
done

# ********** homework_3 *********

homework 1 create 3

dir3=/home/acs/homework/lesson_1/homework_3 

for i in a.txt b.txt c.txt
do
    mv ${dir3}/dir_a/$i ${dir3}/dir_b/$i
done

# ********** homework_4 *********

homework 1 create 4

dir4=/home/acs/homework/lesson_1/homework_4 

for i in a.txt b.txt c.txt
do
    rm ${dir4}/$i
done

# ********** homework_5 *********

homework 1 create 5

dir5=/home/acs/homework/lesson_1/homework_5 

for i in dir_a dir_b dir_c
do
    rm ${dir5}/$i -r
done

# ********** homework_6 *********

homework 1 create 6

dir6=/home/acs/homework/lesson_1/homework_6 

mv ${dir6}/task.txt "${dir6}/done.txt"
mkdir ${dir6}/dir_a
mv "${dir6}/done.txt" ${dir6}/dir_a/

# ********** homework_7 *********

homework 1 create 7

dir7=/home/acs/homework/lesson_1/homework_7 

for((i=0; i<3; i++))
do
    mkdir ${dir7}/dir_$i
    for j in a b c
    do
        cp ${dir7}/${j}.txt ${dir7}/dir_${i}/${j}${i}.txt
    done
done

# ********** homework_8 *********

homework 1 create 8

dir8=/home/acs/homework/lesson_1/homework_8

rm ${dir8}/dir_a/a.txt
mv ${dir8}/dir_b/b.txt ${dir8}/dir_b/b_new.txt
cp ${dir8}/dir_c/c.txt ${dir8}/dir_c/c.txt.bak

# ********** homework_9 *********

homework 1 create 9

dir9=/home/acs/homework/lesson_1/homework_9

rm ${dir9}/*.txt


homework 1 test


活动打卡代码 Linux 2.9. homework_9

小牛马
30天前
 a + b


活动打卡代码 Linux 2.8. homework_8

小牛马
30天前
cd homework_8
vim source.cpp
选中15-21行   15Gv21G
向右缩进一次   >
同理再缩进一次

选中22-23行   22Gv23G
向左缩进一次   <
:wq   保存并退出


活动打卡代码 Linux 2.7. homework_7

小牛马
30天前
cd homework_7
vim source.cpp
gg=G  全文格式化
:wq  保存并退出