一点点来

函数与递归
#include<bits/stdc++.h>//求欧里几德距离
using namespace std;
struct point
{
    double x,y;
};

double dist(struct point a,struct point b){
    return hypot(a.x-b.x,a.y-b.y);//hypot一个数学函数,求两个值平方差


}
int main()
{
    struct point a,b;
    a.x=1.5;
    a.y=2.0;
    b.x=1.0;
    b.y=1.5;
    printf("%lf",dist(a,b));


}

  • 用typedef定义结构体,后面结构体的初始也要改一下
#include<bits/stdc++.h>
using namespace std;
typedef struct
{
    double x,y;
}point;
double dist(point a,point b){
    return hypot(a.x-b.x,a.y-b.y);


}
int main()
{
    point a,b;
    a.x=1.5;
    a.y=2.0;
    b.x=1.0;
    b.y=1.5;
    printf("%lf",dist(a,b));


}

P80 刽子手游戏

#include<bits/stdc++.h>
using namespace std;
char s1[100],s2[100];//s1要猜的单词,s2是猜的单词
int n;
int flag=0;
int t=0;
int wa=0;
void guess(char c){
    int a=0;
    for(int i=0;i<strlen(s1);i++){
        if(s1[i]==c){
            s1[i]=' ';//将相同的设为空格
            t++;
            a=1;

        }
    }
    if(a==0)wa++;//这个字符不是单词中 的错误加一
    if(wa==7){flag=2;return;}//defeat
    if(t==strlen(s1)) {flag=1;return ;}//success

}
int main()
{
    while(scanf("%d",&n)&&n!=-1)
    {
        getchar();
        scanf("%s %s",s1,s2);
        for(int i=0;i<strlen(s2);i++){
            guess(s2[i]);
            if(flag==1)
            {
                printf("Success!\n");
                break;
            }
            if(flag==2)
            {
                printf("defeat!\n");
                break;
            }
        }
        if(wa<7&&flag==0)//不是失败和成功,flag还是为0 所以要andflag==0
        {
            printf("Give up!\n");
        }

    }


}

STL

P110 木块问题
原题地址

#include<bits/stdc++.h>
using namespace std;

const int maxn = 30;
int n;
vector<int>p[maxn];
void find_where(int a,int &pa,int &ha)//遍历每个容器的每个值,直到找出a或者b
{
    for(int i=0;i<n;i++){
        for(int j=0;j<p[i].size();j++){
            if(p[i][j]==a){
                pa=i;
                ha=j;
            }
        }
    }


}
void clear_all(int pa,int ha)//把pa上从ha开始的归位
{
    for(int i=ha+1;i<p[pa].size();i++)
    {
        int b=p[pa][i];
        p[b].push_back(b);//归位
    }
    p[pa].resize(ha+1);//只留h+1个位置
}
void pile_onto(int pa,int ha,int pb)
{
    for(int i=ha;i<p[pa].size();i++){
        p[pb].push_back(p[pa][i]);
    }
    p[pa].resize(ha);
}
void print()
{
    for(int i=0;i<n;i++){
            cout<<i<<":";
        for(int j=0;j<p[i].size();j++){
            cout<<" "<<p[i][j];
        }
    cout<<endl;
    }

}
int main()
{
    cin>>n;
    int a,b;
    string s1,s2;
    for(int i=0;i<n;i++)p[i].push_back(i);//最初先把n个木块归位
    while(cin>>s1){
        if(s1=="quit")
        break;
            cin>>a>>s2>>b;
        int pa,pb,ha,hb;
        find_where(a,pa,ha);
        find_where(b,pb,hb);
        if(pa==pb)continue;
        if(s1=="move") clear_all(pa,ha);//完成归位
        if(s2=="onto") clear_all(pb,hb);
        pile_onto(pa,ha,pb);


    }
    print();
    return 0;

}

P112
#include<bits/stdc++.h>
using namespace std;

vector<string>word,word2;//存每个单词用vector就ok
map<string,int>cnt;
//标准化
string str(string &s)
{
    string s2=s;
    for(int i=0;i<s2.length();i++){
        s2[i]=tolower(s2[i]);//小写字母化
    }
    sort(s2.begin(),s2.end());
    return s2;
}
int main()
{
    string s;
    while(cin>>s){
        if(s=="#")break;
        word.push_back(s);
        string s2=str(s);
        word2.push_back(s2);//存变形好的string
        if(!cnt.count(s2))cnt[s2]=0;//原来不存在赋为0 ,原来存在直接++
        cnt[s2]++;
    }
    vector<string>ans;//储存输出单词的容器
    for(int i=0;i<word.size();i++)
    {
        if(cnt[word2[i]]==1)ans.push_back(word[i]);
    }
    sort(ans.begin(),ans.end());
    for(int i=0;i<ans.size();i++)
    {
        cout<<ans[i]<<endl;
    }
    cout<<endl;

}

其实string用STL还是挺方便的,有之间的插入删除等函数,多用!

  • 前几天的蓝桥杯模拟遇见一个题,用vector特别简单(可以没看清题fk)
    M有10个友好数字,现在JM想知道他这10个数字分别对42取余的结果中,所有结果数位上总共出现了多少个不同的数字。注意是所有数位,开始看成了各位。
    例如:
    [1,10][1,10]
    [1,10]十个数字分别对
    4242
    42计算余数,结果是:
    1,2,3,4,5,6,7,8,9,101,2,3,4,5,6,7,8,9,10
    1,2,3,4,5,6,7,8,9,10,数位上总共出现了
    0,1,2,3,4,5,6,7,8,90,1,2,3,4,5,6,7,8,9
    0,1,2,3,4,5,6,7,8,9十个不同的数字。
    输入
    输入
    1010
    10行,每行一个非负整数,每一个数均不超过
    10001000
    1000。
    输出
    输出一个整数表示数位上出现的不同数字的个数。
    样例
    输入
    复制
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    输出
    复制
    10
    输入
    复制
    39
    40
    41
    42
    43
    44
    82
    83
    84
    85
    输出
    复制
    6
    输入
    复制
    42
    84
    252
    420
    840
    126
    42
    84
    420
    126
    输出
    复制
    1
#include<bits/stdc++.h>
using namespace std;
int a[10];
map<int,int>m;
int main()
{
    for(int i=0; i<10; i++)
    {
        cin>>a[i];
        int c=a[i]%42;
        int a=c/10;
        int b=c%10;
        if(!m.count(b))//原来没有加进去
        {
            m[c]=1;

        }
        
        if(!m.count(c))//原来没有加进去
        {
            m[c]=1;

        }
    }
    cout<<m.size();
    return 0;


}
P118团队队列
  • 用来map和多个vector
#include<bits/stdc++.h>
using namespace std;

int main()
{
    int n,m;
    int k=0;
    while(cin>>n)
    {
        if(n==0)return 0;
        k++;
        printf("Scenario #%d",k);
        map<int,int>team;
        for(int i=0;i<n;i++)//输入队伍信息
        {
            int x;
            scanf("%d",&m);
            while(m--){
                scanf("%d",&x);
                team[x]=i;//i就是队伍号
            }
        }

        //模拟
        queue<int> q,q2[1010];//q是大队的队列,q2[i]是团队i成员的队列
        for(;;){
            int x;
            char cmd[10];
            scanf("%s",cmd);
            if(cmd[0]=='S')break;//Stop
            else if(cmd[0]=='D'){//出队队首
                int t=q.front();//t队伍
            printf("%d\n",q2[t].front());//输出t队伍的队首,因为输入的时候就已经排好那个队伍在前面了
            q2[t].pop();
            if(q2[t].empty())//如果这个队的人全pop掉了,那就在队的队列里把这个队给pop
                q.pop();


            }
            else if(cmd[0]=='E'){
                scanf("%d",&x);
                int t=team[x];//x编号所在的队列
                if(q2[t].empty())
                    q.push(t);
                q2[t].push(x);
            }
        }
        printf("\n");

    }

}

题目描述:

输入n个矩阵的维度和一些矩阵链乘表达式,输出乘法的次数。如果乘法无法进行,输出error。

Sample Input
9
A 50 10
B 10 20
C 20 5
D 30 35
E 35 15
F 15 5
G 5 10
H 10 20
I 20 25
A
B
C
(AA)
(AB)
(AC)
(A(BC))
((AB)C)
(((((DE)F)G)H)I)
(D(E(F(G(HI)))))
((D(EF))((GH)I))

Sample Output
0
0
0
error
10000
error
3500
15000
40500
47500
15125

#include<bits/stdc++.h>
using namespace std;

//这里不用管他怎么算的,直接给出了如果可以算
//直接用他给的矩阵维度来算就行

//用到栈和结构体比较简单些
struct Matrix
{
    int a,b;
    Matrix(int a=0,int b=0):a(a),b(b)
    {

    }



} m[26];

stack<Matrix>s;
int main()
{

    int n;
    cin>>n;
    for(int i=0; i<n; i++) //输入矩阵
    {
        string name;
        cin>>name;
        int k=name[0]-'A';
        cin>>m[k].a>>m[k].b;
    }//
    //输入运算规则
    string op;
    while(cin>>op)
    {
        int len=op.length();
        int flag=0;
        int num=0;
        for(int i=0; i<len; i++)
        {
            if(isalpha(op[i])) //如果是字母
            {
                s.push(m[op[i]-'A']);

            }
            else if(op[i]==')') //遇到这个符号就出栈两个进行相乘
            {
                Matrix m2 = s.top();
                s.pop();
                Matrix m1 = s.top();
                s.pop();
                if(m2.a!=m1.b)
                {
                    flag=1;
                    break;
                }
                //如果可以
                num+=m1.a*m1.b*m2.b;
                //将新的放在栈顶
                s.push(Matrix(m1.a,m2.b));


            }


        }
        if(flag==1)
            cout<<"error"<<endl;
        else
            cout<<num<<endl;



    }
    return 0;





}

链表

PTA上一个链表连接的题

输入两组数,然后按增序连接,创建链表的时候要注意是从L的next才开始有数据。

#include<bits/stdc++.h>
using namespace std;

typedef struct LNode
{
    int data;
    struct LNode *next;

}LNode,*LinkList;
int ListCreat(LinkList &L)//L就是一个新建的链表
{
    LNode *p,*q;
    int m;
    L=(LNode*)malloc(sizeof(LNode));
    if(!L)exit(-2);
    p=L;
    while(~scanf("%d",&m)&&m!=-1){
        q=(LNode*)malloc(sizeof(LNode));
        q->next=NULL;
        p->next=q;
        q->data=m;
        p=q;
    }
    return 1;
}
int combine(LinkList L1,LinkList L2,LinkList &L3)//加上& 的原因是要返回已经合并后的L3
{
    LNode *p1,*p2;
    LNode *rea,*cur;
    L3=(LNode*)malloc(sizeof(LNode));
    L3->next=NULL;
    rea=L3;
    p1=L1->next;
    p2=L2->next;
    while(p1&&p2)//都有的那个长度
    {
        cur=(LNode*)malloc(sizeof(LNode));
        cur->next=NULL;
        if(p1->data<p2->data){
            cur->data=p1->data;
            rea->next=cur;
            rea=cur;//rea也后移
            p1=p1->next;//后移


        }
        else
        {
            cur->data=p2->data;
            rea->next=cur;
            rea=cur;//rea也后移
            p2=p2->next;//后移
        }


    }
    if(p1){
        rea->next=p1;

    }
    if(p2){
        rea->next=p2;
    }
    return 1;

}
void print(LinkList L)
{
    LNode *p=L->next;
    if(!p)cout<<"NULL"<<endl;
    else{

        while(p){
            if(p->next==NULL)
                cout<<p->data<<endl;
            else
                cout<<p->data<<" ";
            p=p->next;
        }
    }
}

int main()
{
    LinkList L1,L2,L3;
    ListCreat(L1);
    ListCreat(L2);
    combine(L1,L2,L3);
    print(L3);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值