紫书水题

开始刷紫书...

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;
}



水题写个差不多了

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

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

posted @ 2018-04-17 22:27  muvseea  阅读(336)  评论(0编辑  收藏  举报