找回密码
 点一下
楼主: Renee

hmmm~~一个C语言编程题~~

[复制链接]
发表于 2008-4-29 20:34:01 | 显示全部楼层
55,对称冒泡。
回复

使用道具 举报

发表于 2008-4-29 21:01:54 | 显示全部楼层
本来就限制了相邻的交换了。。。
回复

使用道具 举报

发表于 2008-4-29 21:50:57 | 显示全部楼层
好像NOIP某一题...
回复

使用道具 举报

发表于 2008-4-30 08:57:34 | 显示全部楼层
#include <iostream>
#include <iomanip>
using namespace std;

const int MAX=26;//100;
int flag=0;
int PalindromeIt(char *s,int n)
{
    int i=0,
        j=n-1;
    for(;i<=j;i++,j--)
    {
        if(a!=a[j])
        {
        //    cout<<"这鸟东西不是回文"<<endl;
            return false;
        }
    }
    //cout<<"hmmmm……….."<<endl;
    return true;
}
void Pretty(char *s,int n)
{
    int i=0,j=0;
    int num[MAX]={0};
    int odd=0;
    int pivot=0;
    char pivotvalue=0;
    for(i=0;i<n;i++)//累计出现的次数
    {
        num[a-'a']++;
    }
//    for(int j=0;num[j]!='\\0';j++)
    for(j=0;j<MAX;j++)
    {
        if(num[j]%2!=0)//找出现次数为奇数的元素
        {
            odd++;
            //pivot=j;
            pivotvalue='a'+j;
        }
    }
    if(odd>1)//不可能改为回文串
    {
        cout<<"呃?"<<endl;
    }
    else//通过互换改为回文串
    {
        flag=1;
/*        if(pivot<=n/2)
        {
            int temp=a[pivot];
            for(i=pivot+1;i<=n/2+1;i++)
            {
                a[i-1]=a;
            }
            a[n/2+1]=temp;
        }
        else
        {
            int temp=a[pivot];
            for(i=pivot-1;i>=n/2+1;i--)
            {
                a[i+1]=a;
            }
            a[n/2+1]=temp;
        }*/

        for(i=0;i<MAX;i++)
        {
            if(a==pivotvalue)
            {
                pivot=i;
            }
        }

        char swap=a[pivot];//将中间位置的元素放到中间int change to char
        a[pivot]=a[n/2];
        a[n/2]=swap;
        for(i=0;i<=n/2;i++)//依次互换元素,放到相应的位置
        {
            for(j=i;j<n;j++)
            {
                if(a==a[j]&&j!=n/2)
                {
                    swap=a[j];
                    a[j]=a[n-1-i];
                    a[n-1-i]=swap;
                }
                //    break;
            }
        //    for(int k=)
        }
    }
}
int main(int argc,char *srgv[])
{
    int i,n=0;
//    char c;
    char string[MAX];
    cout<<"Input your string length:"<<endl;
    cin>>n;
    for(i=0;i<n;i++)
    {
    //    cin>>c;
        cin>>string;
    }
    if(PalindromeIt(string,n))
        cout<<"这鸟东西不是回文"<<endl;
    else
        cout<<"hmmmm……….."<<endl;
//    cout<<PalindromeIt(string,n);
    if(!PalindromeIt(string,n))//以上是输入字符串
        Pretty(string,n);
    if(flag)//可以改为回文串,改后输出
    {
        for(i=0;i<n;i++)
        {
            cout<<setw(3)<<string;
        }
        cout<<endl;
    }
    //cin.get();
    system("pause");
    return 0;
}
回复

使用道具 举报

发表于 2008-5-1 09:06:03 | 显示全部楼层
   高人啊,我如同看天书
回复

使用道具 举报

发表于 2008-5-1 11:47:03 | 显示全部楼层
如果LS的米学过C的话,我想哪怕有人写个计算圆形面积的的代码你也会如同看天书一般的- -
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 点一下

本版积分规则

Archiver|移动端|小黑屋|地精研究院

GMT+8, 2024-10-1 12:07 , Processed in 0.029176 second(s), 17 queries .

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表