【紫*书】第5章 c++与stl入门 #例题#习题

(随手一写qwq  没有传送门== )

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<sstream>
using namespace std;
/*【大理石在哪儿】(Uva 10474)
N个大理石上写一个非负整数。先把各数从小到大排序,然后回答Q个问题。
询问是否有一个大理石写着某个整数x,如果是,还要回答哪个大理石上写着x。
排序后的大理石从左到右编号为1~N。 */
const int maxn=10009;
int a[maxn];
int main(){
    int n,q,x,kase=0;
    while(cin>>n>>q && n!=0){
        printf("CASE# %d:\n",++kase);
        for(int i=0;i<n;i++) scanf("%d",&a[i]);
        sort(a,a+n);//统一从0开始就不用管stl函数的初始值问题了
        while(q--){
            scanf("%d",&x);
            int p=lower_bound(a,a+n,x)-a; 
            //lower_bound【查找大于或等于x的第一个位置】
            if(a[p]==x) printf("%d found at %d\n",x,p+1);
            else printf("%d not found\n",x);//返回值并不对应x
        }
    }
    return 0;
}



#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<sstream>
using namespace std;
/*【木块问题】(Uva 101)
从左到右有n个木块,编号0~n-1,4种操作:(a和b是木块编号)
move a onto b:把a和b上方的木块全部归位,然后把a摞在b上面。
move a over b:把a上方的木块全部归位,然后把a放在b所在木块堆的顶部。
pile a onto b:把b上方的木块全部归位,然后把a及上面的木块整体摞在b上面。
pile a over b:把a及上面的木块整体摞在b所在木块堆的顶部。
遇到quit时终止一组数据。a和b在同一堆的指令是非法指令,应当忽略。
输入:1234  输出:1234-> 3087-> 8352-> 6174-> 6174
【分析】每个木块堆的高度不确定,所以用vector来保存很合适 */
const int maxn = 30; int n;
vector<int> pile[maxn];   //每个pile[i]是一个vector

void find_block(int a, int& p, int& h) {
//找木块a所在的pile(堆)和height(高度),以{引用的形式}返回调用者  
    for(p=0;p<n;p++)
        for(h=0;h<pile[p].size();h++)
            if(pile[p][h] == a) return; //返回的只是此时a,p,h的状态
}

void clear_above(int p, int h) {
//把第p堆高度为h的木块上方所有木块移回原位
    for(int i = h+1; i < pile[p].size(); i++) {
        int b = pile[p][i];
        pile[b].push_back(b);  //把木块b放回原位
    }
    pile[p].resize(h+1); //pile 只应保留下标0~h 的元素  
    //***改变大小的函数,需要多加练习***
}

void pile_onto(int p, int h, int p2) {
//把第p堆高度为h及其上方的木块整体移动到p2 堆的顶部
    for(int i = h; i < pile[p].size(); i++)
        pile[p2].push_back(pile[p][i]); //直接加入
    pile[p].resize(h); //改高度
}

void print(){ //打印结果
    for(int i=0;i<n;i++){
        printf("%d:",i);
        for(int j=0;j<pile[i].size();j++) 
            printf(" %d",pile[i][j]);
        printf("\n");
    }
}

int main() {
    int a, b; cin >> n; string s1, s2;
    for(int i = 0; i < n; i++) pile[i].push_back(i);
    while(cin>>s1>>a>>s2>>b) {
        int pa,pb,ha,hb;
        find_block(a,pa,ha); find_block(b,pb,hb);
        if(pa == pb) continue; //非法指令
        if(s2 == "onto") clear_above(pb, hb);
        if(s1 == "move") clear_above(pa, ha); //移回原位
        pile_onto(pa, ha, pb); //移动到pb堆的顶部
    }
    print();
    return 0;
}

/* 数据结构的核心是vector<int>pile[maxn],所有操作都是围绕它进行的。
vector像一个{二维数组},第一维大小固定(不超过maxn),但第二维不固定。
技巧》提取出4种指令之间的共同点,编写函数,以减少重复代码。
总结》vector可以用clear( )清空,resize( )改变大小,用empty( )测试是否为空,
push_back( )、pop_back( )在尾部添加和删除元素,可以直接赋值或者作为函数的返回值。 */

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<sstream>
using namespace std;
/*【木块问题】(Uva 101)
从左到右有n个木块,编号0~n-1,4种操作:(a和b是木块编号)
move a onto b:把a和b上方的木块全部归位,然后把a摞在b上面。
move a over b:把a上方的木块全部归位,然后把a放在b所在木块堆的顶部。
pile a onto b:把b上方的木块全部归位,然后把a及上面的木块整体摞在b上面。
pile a over b:把a及上面的木块整体摞在b所在木块堆的顶部。
遇到quit时终止一组数据。a和b在同一堆的指令是非法指令,应当忽略。
输入:1234  输出:1234-> 3087-> 8352-> 6174-> 6174
【分析】每个木块堆的高度不确定,所以用vector来保存很合适 */
const int maxn = 30; int n;
vector<int> pile[maxn];   //每个pile[i]是一个vector

void find_block(int a, int& p, int& h) {
//找木块a所在的pile(堆)和height(高度),以{引用的形式}返回调用者  
    for(p=0;p<n;p++)
        for(h=0;h<pile[p].size();h++)
            if(pile[p][h] == a) return; //返回的只是此时a,p,h的状态
}

void clear_above(int p, int h) {
//把第p堆高度为h的木块上方所有木块移回原位
    for(int i = h+1; i < pile[p].size(); i++) {
        int b = pile[p][i];
        pile[b].push_back(b);  //把木块b放回原位
    }
    pile[p].resize(h+1); //pile 只应保留下标0~h 的元素  
    //***改变大小的函数,需要多加练习***
}

void pile_onto(int p, int h, int p2) {
//把第p堆高度为h及其上方的木块整体移动到p2 堆的顶部
    for(int i = h; i < pile[p].size(); i++)
        pile[p2].push_back(pile[p][i]); //直接加入
    pile[p].resize(h); //改高度
}

void print(){ //打印结果
    for(int i=0;i<n;i++){
        printf("%d:",i);
        for(int j=0;j<pile[i].size();j++) 
            printf(" %d",pile[i][j]);
        printf("\n");
    }
}

int main() {
    int a, b; cin >> n; string s1, s2;
    for(int i = 0; i < n; i++) pile[i].push_back(i);
    while(cin>>s1>>a>>s2>>b) {
        int pa,pb,ha,hb;
        find_block(a,pa,ha); find_block(b,pb,hb);
        if(pa == pb) continue; //非法指令
        if(s2 == "onto") clear_above(pb, hb);
        if(s1 == "move") clear_above(pa, ha); //移回原位
        pile_onto(pa, ha, pb); //移动到pb堆的顶部
    }
    print();
    return 0;
}

/* 数据结构的核心是vector<int>pile[maxn],所有操作都是围绕它进行的。
vector像一个{二维数组},第一维大小固定(不超过maxn),但第二维不固定。
技巧》提取出4种指令之间的共同点,编写函数,以减少重复代码。
总结》vector可以用clear( )清空,resize( )改变大小,用empty( )测试是否为空,
push_back( )、pop_back( )在尾部添加和删除元素,可以直接赋值或者作为函数的返回值。 */


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <queue>
#include <vector>
#include <cmath>
#include <map>
#include <sstream>
using namespace std;

/*【反片语】Uva 156
输入一些单词,找出所有满足如下条件的单词:
该单词不能通过字母重排,得到输入文本中的另外一个单词。
在判断是否满足条件时,字母不分大小写,但在输出时应保留输入中的大小写,
按字典序进行排列(所有大写字母在所有小写字母的前面)。*/

/* 输入: ladder came tape soon leader acme RIDE lone Dreis peat
ScAlE orb eye Rides dealer NotE derail LaCeS drIed
noel dire Disk mace Rob dries  #
输出:Disk  NotE  derail  drIed  eye  ladder  soon
【分析】利用map。“标准化”:把每个单词全部转化为小写字母后进行排序。  */

map<string,int> cnt;
vector<string> words;

string repr(const string& s){ //将单词s进行“标准化”(转小写)
    string ans = s;
    for(int i = 0; i < ans.length(); i++)
        ans[i] = tolower(ans[i]);//转小写函数
    sort(ans.begin(), ans.end());
    return ans;
}

int main() {
    int n = 0; string s;
    while(cin >> s) {
        if(s[0] == '#') break; //输入终点
        words.push_back(s); //记入map
        string r = repr(s);
        if(!cnt.count(r)) cnt[r] = 0;
        cnt[r]++;
    }
    vector<string> ans;
    for(int i = 0; i < words.size(); i++)
    if(cnt[repr(words[i])]==1)  ans.push_back(words[i]);
    sort(ans.begin(),ans.e
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值