PTA 11解题

7-1 统计字符串的所有子串中回文串的个数 (30 分)

本题要求输入一个字符串,输出该串的所有子串中回文串的数量 回文串:逆序与顺序相同的字符串,如aba,aabaa,a 本题要求所有对字符串的处理均用指针完成,例如对回文串的判断:

bool isPalindrome(char *a,int length)
{
    for (char *i = a; i < a + length; ++i)
    {
        if (*i != *(a+(a-i+length-1)))
            return false;
    }
    return true;
}

输入格式:

在一行中输入一个长度10的字符串a

输出格式:

对于每组输入,在一行中输出一个正整数,表示该串的所有子串中回文串的数量

输入样例:

在这里给出一组输入。例如:

abc

输出样例:

在这里给出相应的输出。例如:

3

Hint

样例中,abc的子串有:a, ab, abc, b, bc, c 
其中a, b, c是回文串,输出为3

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

int main(int argc, char* argv[])
{
    char s[5000];
    int p, i, Half, Left, Right, Count;
    while( cin>>s )
    {
        i = strlen(s);
        Count = 0;

        for(p=0; p<=i-1; p++)
        {
            Half = ((i-1)-p) / 2;
    
            if( ((i-1)-p)%2 == 0 )
            {
                Left = p + Half - 1;
                Right = p + Half + 1;
            }
            else   
            {
                Left = p + Half;
                Right = p + Half + 1;
            }
            while( Left >= p )
            {
                if( s[Left] == s[Right])  
                {
                    Count++;  
                    Left--;
                    Right++;
                }
                else  
                {
                    break;
                }
            }
        }
        for(p=i-2; p>=1; p--)
        {
            Half = p / 2;
            if( p%2 == 0 )
            {
                Left = Half - 1;
                Right = Half + 1;
            }
            else   
            {
                Left = Half;
                Right = Half + 1;
            }
            while( Left >= 0 )
            {
                if( s[Left] == s[Right] )
                {
                    Count++;  
                    Left--;
                    Right++;
                }
                else  
                {
                    break;
                }
            }
        }
        cout<<Count + i;
    }
    return 0;
}
7-2 冒泡 (20 分)

鸿鸿哥最近学习了指针,感觉这个知识点有点难以理解,于是想要通过编程实践来掌握它。鸿鸿哥以前学习数组(第7章)的时候已经掌握了冒泡排序的一般写法,现在他想用指针来实现排序的功能函数。但是他遇到了困难,你能帮帮他吗?

指针实现冒泡排序函数,函数名统一用void bubbleSort(int *p,int c)。 具体方法自己实现。

输入格式:

一组输入,第一行是待排数据个数n, 第二行是数据的具体值。

输出格式:

输出排序后的数,两个数之间以空格间开,最后一个数字末尾有空格

输入样例:

在这里给出一组输入。例如:

5
503 87 512 61 908

输出样例:

在这里给出相应的输出。例如:

61 87 503 512 908
#include <iostream>
#include <cstring>
#include<assert.h>
void BubbleSort(int* a,int n) ;
using namespace std;

int main ()
{
    int c,i=0;
    cin>>c;
    int a[20];
    for(i;i<c;i++)
    {
        cin>>a[i];
    }
    BubbleSort( a,c);
    
    for(int b=0;b<c;b++)
    {
        cout<<a[b]<<" ";
    }

}
void BubbleSort(int* a,int n)
{
    assert(a&&n);
    if(n==1)
        return;
    for(int i=0;i<n-1;i++)
    {
        for(int j=0;j<n-1-i;j++)
        {
            if(a[j]>a[j+1])
            {
                int tmp=a[j];
                a[j]=a[j+1];
                a[j+1]=tmp;
            }    
        }
    }
}
7-3 小明的指针 (20 分)

小明学习了指针之后十分开心,因为他觉得指针实在是太好用太方便了,因此它觉得要用指针来做作业,现在小明遇到一道不会写的题,希望能得到你的帮助。题目是:存在一个分数表,其中有学生的名字和分数,他们有着一定的排列顺序。现在你要找到其中分数最大的学生,让它和第一个位置的学生交换,找到分数最小的学生。让它和最后一个学生交换。输出交换后的名单,其中分数表的第一位和最后一位不为分数最大或最小(用指针实现上述操作)

输入格式:

输入一个n表示分数表上的学生人数,接下来n行中,每行有一个字符串s表示学生名字和一个数字表示学生分数

输出格式:

输出改变位置后的名单

输入样例:

4
Xiaoming 80
Xiaohong 50
Xiaolan 90
Xiaobai 70

输出样例:

Xiaolan 90
Xiaobai 70
Xiaoming 80
Xiaohong 50

#include<stdio.h>
#include <iostream>
#include<string.h>
using namespace std;



struct Student
{
char name[20];
int  score;
};





int main()
{
    int n;
    cin>>n;
    
struct Student stu[n];



for(int a=0;a<n;a++)
{
    cin>>stu[a].name ;
    cin>>stu[a].score ;
}



struct Student temp1,temp2;




    int j=0;
int temp3=stu[0].score;  //
int temp4=stu[0].score;  //
    for(j;j<n-1;j++)
    {
    

        if(temp3<stu[j+1].score) 
        {
            temp3=stu[j+1].score;
            temp1=stu[j+1];
        }
        
        if(temp4>stu[j+1].score)  
        {
        temp4=stu[j+1].score;    
        temp2=stu[j+1];
        }
            
    }



//    cout<<temp1.score <<"       "<<temp2.score <<endl;




    struct Student t1=stu[0];
    struct Student t2=stu[n-1];
//    cout<<t1.score <<"         "<<t2.score<<endl ; 
    for(int k=0;k<n;k++)
    {
        if(stu[k].score==temp3)   stu[k]=t1;
        if(stu[k].score==temp4)   stu[k]=t2;
    }
//    cout<<"      "<<temp3<<"     "<<temp4<<'\n';
    

    stu[0]=temp1;
    stu[n-1]=temp2;
for(int i=0;i<n;i++)
printf("%s %d\n",stu[i].name,stu[i].score);
return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
typedef enum
{
FALSE,
TRUE
}BOOL;

BOOL IsLeapYear (int year)
{
#if 0
if ((year % 4) == 0)
{
if ((year % 100) == 0)
{
if ((year % 400) == 0)
{
return TRUE;
}
else
{
return FALSE;
}
}
else
{
return TRUE;
}
}
else
{
return FALSE;
}
#endif?
if((year%4==0 && year%100!=0) || year%400==0)
{
cout<<"yes";
return TRUE;
}
else
{
cout<<"no";
return FALSE;
}
}

int main (void)
{
int year=0,month=0,day=0;
cin>>year>>month>>day;
fflush (stdin);

IsLeapYear(year);

return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
typedef enum
{
FALSE,
TRUE
}BOOL;

BOOL IsLeapYear (int year)
{
#if 0
if ((year % 4) == 0)
{
 if ((year % 100) == 0)
{
 if ((year % 400) == 0)
{
return TRUE;
 }
else
{
 return FALSE;
 }
 }
else
{
return TRUE;
 }
}
else
{
return FALSE;
}
#endif?
if((year%4==0 && year%100!=0) || year%400==0) 
{
cout<<"yes";
return TRUE;
}
else 
{
cout<<"no";
return FALSE;
}
}

int main (void)
{
int year=0,month=0,day=0;
cin>>year>>month>>day;
fflush (stdin);

IsLeapYear(year);

return 0;
}

猜你喜欢

转载自www.cnblogs.com/yingni/p/10117329.html
PTA