三道跟string有关的题

版权声明:闻盲 https://blog.csdn.net/MI55_845/article/details/84190065

参考string的函数

①来源 && 题目信息

这道题我卡在不知道怎么存储多个字符串,后面学习到了…

一般我使用string字符串的输入,都是这样 :

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string a;
	
	/*不读人空格
	cin >> a;
	
	cout << a << endl;
	*/	
	
	/*读入空格
	getline( cin , a );
	
	cout << a << endl;
	*/
	
	return 0;
} 

因此,我认为string是不可以写出 string a【i】的形式的。所以,不知道怎么存储多个字符串。

但是那道题改变了我的想法 , string也是可以用 a【i】的 。

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    string a[7];

    for( int i = 0 ; i < 7 ; i ++)
    {
        cin >> a[i];
    }

    for( int i = 0 ; i < 7 ; i ++)
    {
        cout << a[i] << endl;
    }

    return 0;
}

哎。。。。。。。。。。

②来源 && 题目信息

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
     
    cin >> n;
     
    string a[150];
     
    int x,y,z,j,k ;
     
    int i = 0 , s = 0;
     
    char ar[1000];
     
    char ae[1000];
     
    for( x = 0; x < n ; x ++)
         
        cin >> a[x];
 
    for( x = 0 ; x < n ; x ++)
         
        for( y = x + 1; y < n; y ++)
               
              if( a[x] == a[y])
                  
                 s ++;
 
    for( x = 0 ; x < n ; x ++)
    {
        int len = a[x].length(); 
         
        ar[x] = a[x][len-1];//第 i 个 字符串的 第 len - 1 个字符。
         
        ae[x] = a[x][0];
    }
 
    for( x=0 ; x < n - 1 ; x ++)
    {
        if( ae[x+1] != ar[x])
             
            i++;
    }
 
    if( i == 0 && s == 0)
         
         cout<<"Yes";
     
    else cout<<"No";
 
    return 0;
}

哎。。。。。。。。。。。

③来源 && 题目信息

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

int main()
{
    int t , i;

    string a;

    while(cin >> a)
    {
        t = a.find(".");//找到 小数点 在字符串中的位子。

        if( t == -1)//返回-1,说明没找到
        {
            a = a + ".00";
        }

       else
       {
           if( a[t + 1] == '\0')//小数点后第一位是不是空 , 如果是则为 X.00
           {
               a = a + "00";
           }

           if( a[t + 2] == '\0' )//小数点后第二位是不是空 , 如果是则为 X.X0
           {
               a = a + '0';
           }

            if( a[t + 3] != '\0' )//小数点后第三位是不是空 ,不是则判断四舍五入。
           {
               if( a[t+3] >= '5' && a[t+3] <= '9')
               {
                   a[t+2]++;

                   a[t+3] = '\0';

                    if( a[t+2] == ':')
                    {
                         a[t+2] = '0';

                         a[t+1]++;

                        if( a[t+1] == ':' )
                        {
                            a[t+1] = '0';

                            a[t-1]++;

                            for( i = t - 1 ; i > 0 ; i --)
                            {
                                if( a[i] == ':' )
                                {
                                    a[i] = '0';

                                    if( i == 1 && a[i-1] == '-')
                                    {
                                        a[0] = '1';

                                        a = "-" + a;
                                    }

                                    else a[i - 1] ++;
                                }
                            }

                            if( a[0] == ':' )
                            {
                                a[0] = '0';

                                a = "1" + a;
                            }

                        }
                    }
               }

                else a[t + 3] = '\0';
           }
       }

        t = a.find(".");

        int s = 0;

        for( i = 0 ; i < t ; i ++)
        {
            if( a[i] == '0')
            {
                s ++;
            }
        }

        if( a[0] != '-' && s == t)
        {
            a.replace( 0 , t , "0");
        }

        else if( a[0] == '-' && s == t - 1)
        {
            a.replace( 1 , s , "0");
        }

        else if( a[0] == '-')
        {
            while( a[1] == '0')
            {
                a.erase(1,1);//第二个字符是0,则删除第三个字符。
            }
        }

        else if( a[0] != '-')
        {
            while( a[0] == '0')
            {
                a.erase(0,1);//第二个字符是0,则删除第二个字符。
            }
        }

       	t = a.find(".");

       	if( a[0] != '-')
        {
            for( i = t - 1 ; i > 2 ;  )
            {
                a.insert( i - 2 ,",");

                t = a.find(",");

                i = t - 1;
            }
        }

        else
        {
            for( i = t - 1 ; i > 3 ; )
            {
                a.insert( i - 2 , "," );

                t = a.find(",");

                i = t - 1;
            }
        }

        if(a[0]=='-')
        {
            a.erase(0,1);

            cout << "(" << a.c_str() << ")" << endl;
        }

        else

		cout << a.c_str() << endl;
    }

    return 0;
}

/*

c_str 是c++ 中 string类 (class) 的 函数,
它能把 string类 的对象里的字符串
转换成 C 中 char 型变量 的 字符串。

*/

NICE ! ! !

猜你喜欢

转载自blog.csdn.net/MI55_845/article/details/84190065
今日推荐