zoukankan      html  css  js  c++  java
  • 杭电1106 分割字符串问题

    此题的重点 在于思维的全面性

    由题意可以知道 中间的字符分割可以用5的存在来判断(这里还有注意多个5存在的情况)之后别忘了特殊情况的处理 比如初始值的确定(首字母是不是5) 已经末尾没有5的时候怎么去取值

    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    #include<string.h>
    #include<math.h>
    using namespace std;
    int main()
    {
        string s;
        int a[1001],i,j,t,sum,start,end,z;
        while(cin>>s!=NULL)//getline可以接受含有空格的输入
        {
         memset(a,0,sizeof(a));
         start=0;
         t=0;
         z=0;
         for(i=0;i<s.length();i++)
         {
          if(s[i]!='5')
          {
           start=i;
           break;
          }
         }
         for(i=start;i<s.length();i++)
         {  
          if(s[i]=='5'||i==s.length()-1)
          {
           if(s[i-1]!='5') end=i-1;

           else  if(i==s.length()-1) end=i; 
           if(s[i+1]!='5'||i==s.length()-1)
           {
               sum=0;
                z=0;
               for(j=end;j>=start;j--)
              {
                sum+=pow(10,z++)*(s[j]-'0');
              }
               a[t++]=sum;
               start=i+1;
           } 
          }
         }
         sort(a,a+t);
         for(i=0;i<t;i++)
         {
          if(i==0) cout<<a[i];
          else cout<<' '<<a[i];
         }
         cout<<endl;
        }
     return 0;
    }

    中间部分的好处理 尤其注意特殊部位的(开始 结束)的处理(也就多两个判断就好了)

  • 相关阅读:
    【C语言】学习笔记9——结构struct(2)
    WPF dev 获取gridControl筛选后的数据
    WPF DEV dxc:ChartControl 柱状图
    WPF 重写ListBox(透明效果)
    WPF 后台重写 DataTemplate
    WPF 去掉Drag a column header here to group by that column
    c# 对DataTable进行分组group by
    c# ref与out用法
    WPF canvas设置旋转角度和偏移位置
    WPF 流加载
  • 原文地址:https://www.cnblogs.com/z1141000271/p/5379510.html
Copyright © 2011-2022 走看看