例题5-8 Unix ls 命令 UVa400

算法竞赛入门经典(第2版) 第5C++与STL入门

题5-8 Unix ls 命令  UVa400

感悟。

1、从网站下载英文原题,重点在看输入输出数据与格式。

2、英文大意看懂,但在输出文件内容列数的变化规律没摸清。

3、反复读题,猜测大意,是符合条件的列,组合成行,尽可能的多,占领一行(每行60字符)。The rightmost column will be the width of the longest filename and all other columns will be the width of the longest filename plus 2. There will be as many columns as will fit in 60 characters.文中输出的文件列数决定因素。

样例1:

M=19

n*(M+2)+M=60=>n=1,故样例1有两列,每列文件数(10+2-1)/2=5

样例2:

M=12

n*(M+2)+M=60=>n=3,故样例2有四列,每列文件数(12+4-1)/4=3

样例3:

M=9

n*(M+2)+M=60=>n=4,故样例3有五列,每列文件数(19+5-1)/5=4

4、用set存储文件名,按3、进行输出。

5、通过http://blog.youkuaiyun.com/u010902721/article/details/45771597复习了set中迭代器的用法,注意:

it<input.end()错误;it!=input.end()正确。

6、突然发现,字符串是一行一行输出的,怎样将一个系列的字符串按行列有序输出,看来得费些功夫。

7、准备引入vector进行处理。

8、睡了一觉,很快将一维字符串数组表示成二维表格形式,稍加调试,样例通过,在http://vjudge.net上提交AC,再一次,一次提交AC,在https://uva.onlinejudge.org/提交AC,看了看时间2016-11-24

9、扫了一遍书中代码,核心思想比较接近。但代码写起来,差异还是比较大的。

附上AC代码,编译环境Dev-C++4.9.9.2

#include <iostream>
#include <set>
#include <vector>

using namespace std;

int main(){
    int n;
    
    while(cin>>n){
        set<string> input;
        string filename;
        int row,col;
        int len;
        int maxlen=0;
        int count=0;
        int size;
        vector<string> vec;
        int i,j,k,m;
        for(i=0;i<n;i++){
            cin>>filename;
            len=filename.length();
            if(maxlen<len)//查找最长文件名的长度
                maxlen=len;
            input.insert(filename);
        }
        col=(60-maxlen)/(maxlen+2)+1;
        row=(n+col-1)/col;
        cout<<"------------------------------------------------------------"<<endl;
        //迭代器
        for(set<string>::iterator it=input.begin();it!=input.end();it++){//it<input.end()错误
            vec.push_back(*it);
        }
        //打印字符串
        size=vec.size();
        for(i=0;i<row;i++){
            for(j=0;j<col;j++){
                k=i+j*row;//注意是j*row而不是j*col
                if(k<size){//字符串处理
                    string stmp=vec[k];
                    int begin,end;
                    begin=stmp.length();
                    
                    if(j<=col-2){//除最后一列外
                        end=maxlen+2;
                    }else{//最后一列
                        end=maxlen;
                    }
                    
                    for(m=begin;m<end;m++){//通过加空格来补齐
                        stmp+=" ";
                    }
                    cout<<stmp;
                }
                    
            }
            cout<<endl;
        }
    }
    return 0;
}




例题5-8 的情况可能是要求编写一个涉及财务记账的程序,使用函数的方式来管理账户的收入、支出和查询余额。通常这样的例子会包含以下几个步骤: 1. **定义函数**:首先,你可以定义一些函数,比如 `deposit` (存款)、`withdraw` (取款) 和 `check_balance` (查询余额)。 ```python def deposit(account, amount): account.balance += amount print(f"存入了 {amount} 元,当前余额:{account.balance}") def withdraw(account, amount): if account.balance >= amount: account.balance -= amount print(f"取出了 {amount} 元,当前余额:{account.balance}") else: print("余额不足,无法取款") def check_balance(account): print(f"当前余额:{account.balance}") ``` 2. **创建对象**:然后创建一个表示账户的类(例如 `BankAccount`),存储余额作为属性。 ```python class BankAccount: def __init__(self, initial_balance=0): self.balance = initial_balance # 创建一个初始余额为0的账户实例 my_account = BankAccount() ``` 3. **调用函数**:通过账户对象来使用这些函数进行操作。 ```python deposit(my_account, 1000) withdraw(my_account, 500) check_balance(my_account) ``` 在这个例子中,多态并未直接体现,因为所有的函数都是针对 `BankAccount` 对象设计的。但是,如果后续添加了其他类型的账户(如信用卡、储蓄账户等),并保持相同的函数接口,那么就可以体现多态性——即不同类型的账户可以对同一组函数有不同的响应。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值