zoukankan      html  css  js  c++  java
  • 紫书水题

    开始刷紫书...

    1.水题,开灯问题,模拟即可,很好思维点是开关灯用0和1表示(每次只有两个变量都应该想到0和1) i 代表序号要求输出序号,a[i]代表属性

    #include <bits/stdc++.h>
    using namespace std;
    const int maxn=1111;
    int a[maxn];
    int main()
    {
        int n,k;
        cin>>n>>k;
        int flag=1;
        //对于每一个灯都都判断需要改变几次也成
        for(int i=1; i<=k; i++){
            for(int j=1; j<=n; j++){
                if(j%i==0) a[j]=!a[j];
            }
        }
        for(int i=1; i<=n ; i++){
            if(a[i]){
                if(flag) flag=0;
                else
                    cout<<' ';
                cout<<i;
            }
        }
        cout<<endl;
        return 0;
    }
    
    

    模拟即可,两个变化的量 tot 作为连续的变化量。而坐标可以通过行列的变化变化,如果未达到边界那么就执行 while起到了很好的作用 原则是##先判断再移动## 这个在很多里面都适用 作为这道题体现的思维点

    #include <bits/stdc++.h>
    using namespace std;
    const int maxn=111;
    int a[maxn][maxn];
    int main()
    {
        int n,x,y;
        int tot=1;
       // memset(a,0,sizeof(a));
        cin>>n;
        x=0;y=n-1;
        a[x][y]=1;
        while(tot<n*n)
        {
            while(x+1<n&&!a[x+1][y]) a[++x][y]=++tot;
            while(y-1>=0&&!a[x][y-1]) a[x][--y]=++tot;
            while(x-1>=0&&!a[x-1][y]) a[--x][y]=++tot;
            while(y+1<n&&!a[x][y+1]) a[x][++y]=++tot;
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++)
                printf("%3d",a[i][j]);
            cout<<endl;
        }
        return 0;
    }
    

    1.还是那个有两个元素用0 1翻转
    2.不该存的没必要存

    #include<cstdio>
    int main()
    {
        char c;
        int q=1;
        while((c=getchar())!=EOF)
        {
            if(c=='"')
            {
                printf("%s",q?"``":"''");
                q=!q;
            }
            else
            printf("%c",c);
        }
        return 0;
     }
    
    

    生成元 打表的思想

    #include <bits/stdc++.h>
    using namespace std;
    const int maxn=1e5+10;
    int ans[maxn];
    int main()
    {
        int t,n;
        for(int i=1;i<=maxn;i++){
            int x=i,y=i;
            while(x>0){
                y+=x%10;
                x/=10;
                if(ans[y]==0||i<ans[y]) ans[y]=i;
            }
        }
        cin >> t;
        while(t--){
            cin>>n;
            cout<<ans[n]<<endl;
        }
        return 0;
    }
    

    ch来标记字母。

    
    #include <bits/stdc++.h>
    using namespace std;
    double suan(char c)
    {
        double M;
        if (c == 'C') M = 12.01;
        else if (c == 'H') M = 1.008;
        else if (c == 'O') M = 16.00;
        else if (c == 'N') M = 14.01;
        return M;
    }
    int main()
    {
        string s;
        cin>>s;
        double sum=0.0;
        int cnt;
        char ch;
        int j;
        for(int i=0;i<s.size();i++){
            ch=s[i];
            if((i+1==s.size()||isalpha(s[i+1])))
                cnt=1;
            else{
                cnt=0;
                for( j=i+1;j<s.size();j++){
                    if(isdigit(s[j]))
                        cnt=cnt*10+(s[j]-'0');
                    else
                        break;
                }
                i=j-1;
            }
            sum+=suan(ch)*cnt;
        }
        //printf("%f",sum);
     cout <<setprecision(3) << sum << endl;
    }
    
    
    
    

    水题写个差不多了

    第三章里面习题还有几道写起来很繁琐的水题 明天写

    写水题还是令人开心的.... (:别写水题了啊喂。

  • 相关阅读:
    关于ActionScript中那些你不知道的事情
    Flash Player 11 Stage3D学习大杂烩
    Qt 控制台输入输出(支持中文)
    Redis消息发布订阅的稳定性验证结论
    C++11 Lambda表达式(匿名函数)用法详解
    vue中“:”、“.”、“@”意义
    QT中printf输出不同步的解决办法
    QT5中使用SQLite
    QT 调用user32.dll移动鼠标
    10分钟学会Visual Studio将自己创建的类库打包到NuGet进行引用(net,net core,C#)
  • 原文地址:https://www.cnblogs.com/muvseea/p/8870435.html
Copyright © 2011-2022 走看看