zoukankan      html  css  js  c++  java
  • 数据结构作业1——2020

    问题 A: 回文数

    题目描述

    我们把从左往右和从右往左念起来相同的数字叫做回文数。例如,75457就是一个回文数。
    当然某个数用某个进制表示不是回文数,但是用别的进制表示可能就是回文数。
    例如,17是用十进制表示的数,显然它不是一个回文数,但是将17用二进制表示出来是10001,显然在二进制下它是一个回文数。
    现在给你一个用十进制表示的数,请你判断它在2~16进制下是否是回文数。

    输入

    输入包含多组测试数据。每组输入一个用十进制表示的正整数n(0<n<50000),当n=0时,输入结束。

    输出

    对于每组输入,如果n在2~16进制中的某些进制表示下是回文数,则输出“Number i is palindrom in basis ”,在后面接着输出那些进制。其中i用n的值代替,后面输出的进制中,每两个数字之间空一个。
    如果n在2~16进制的表示下都不为回文数,则输出“Number i is not a palindrom”,其中i用n的值代替。

    样例输入

    17
    19
    0
    

    样例输出

    Number 17 is palindrom in basis 2 4 16
    Number 19 is not a palindrom
    
    #include<iostream>
    using namespace std;
    int judge(int n,int i)//n为值,i为进制 
    {
        int a[200];
        int g,num=0;
        while(n)
        {
            a[num++]=n%i;//num还有计数的功能 
            n/=i;
        }
        int t=1;
        for(g=0;g<num/2;g++)
        {
            if(a[g]!=a[num-1-g])
            {
                t=0;
                break;
            }
        }
        return t;
    }
    
    int main()
    {
    	int n;//n为输入值 
    	while(cin>>n&&n!=0) 
    	{
    		int a[20]={0};
    		int t=0;
    		int i;
    		for(i=2;i<17;i++)
    		{
    			if(judge(n,i))
    			{
    			  t=1;
    			  a[i]=1;
    		    }
    		} 
    		
    		if(t)
    		{
    			cout<<"Number"<<' '<<n<<" is palindrom in basis";
    			for(i=2;i<17;i++)
    			{
    				if(a[i])
    				cout<<' '<<i;
    			}
    		    cout<<endl;
    		}
    		else cout<<"Number "<<n<<" is not a palindrom"<<endl;		
    	}
    	
    	
    	return 0;    
    }
    
    

    问题 B: 丑数

    题目描述

    如果一个数的素因子只包含2,3,5或7,那么我们把这种数叫做丑数。序列1,2,3,4,5,6,7,8,9,10,12,14,15,16,18,20,21,24,25,27...展示了前20个丑数。
    请你编程寻找这个序列中的第n个元素。

    输入

    输入包含多组测试数据。每组输入为一个整数n(1<=n<=5842),当n=0时,输入结束。

    输出

    对于每组输入,输出一行“The nth humble number is number.”。里面的n由输入中的n值替换,“st”,“nd”,“rd”和“th”这些序数结尾的用法参照输出样例。

    样例输入

    1
    2
    3
    4
    11
    12
    13
    21
    22
    23
    100
    1000
    5842
    0
    

    样例输出

    The 1st humble number is 1.
    The 2nd humble number is 2.
    The 3rd humble number is 3.
    The 4th humble number is 4.
    The 11th humble number is 12.
    The 12th humble number is 14.
    The 13th humble number is 15.
    The 21st humble number is 28.
    The 22nd humble number is 30.
    The 23rd humble number is 32.
    The 100th humble number is 450.
    The 1000th humble number is 385875.
    The 5842nd humble number is 2000000000.
    
    #include<iostream>
    using namespace std;
    int min(int a,int b,int c,int d)//min函数作用为取abcd最小值,将最小值替换为a,返回a
    {
        if(a>b)a=b;
        if(a>c)a=c;
        if(a>d)a=d;
        return a; 
    }
    int main()
    {
        int a[5843];
    	int b2=1,b3=1,b5=1,b7=1;
    	a[1]=1;	    
    	for(int i=2;i<5844;i++)
    	{
    		a[i]=min(a[b2]*2,a[b3]*3,a[b5]*5,a[b7]*7);
            if(a[i]==a[b2]*2)  b2++;
            if(a[i]==a[b3]*3)  b3++;
            if(a[i]==a[b5]*5)  b5++;
            if(a[i]==a[b7]*7)  b7++;
        }
    	int n;
    	while(cin>>n&&n!=0)
    	{
    	if(n%10==1&&n%100!=11) 
    	{
    		cout<<"The "<<n<<"st humble number is "<<a[n]<<'.';
    		cout<<endl;
        }   
    	else if(n%10==2&&n%100!=12) 
    	{
    	    cout<<"The "<<n<<"nd humble number is "<<a[n]<<'.';
    		cout<<endl;
    	}
    	else if(n%10==3&&n%100!=13) 
    	{
    	    cout<<"The "<<n<<"rd humble number is "<<a[n]<<'.';
    		cout<<endl;
        }
    	else 
    	{
    	    cout<<"The "<<n<<"th humble number is "<<a[n]<<'.';
    		cout<<endl;
        }  
    	
    	}
    	return 0; 	
    }
    

    问题 C: 单词排序

    题目描述

    小红学会了很多英文单词,妈妈为了帮小红加强记忆,拿出纸、笔,把 N 个单词写在纸上的一行里,小红看了几秒钟后,将这张纸扣在桌子上。妈妈问小红:“你能否将这 N 个单词按照字典排列的顺序,从小到大写出来?”小红按照妈妈的要求写出了答案。现在请你编写程序帮助妈妈检查小红的答案是否正确。注意:所有单词都由小写字母组成,单词两两之间用一个空格分隔。

    输入

    输入包含两行。

    第一行仅包括一个正整数N(0<N≤26)。

    第二行包含N个单词,表示妈妈写出的单词,两两之间用一个空格分隔。

    单个单词长度不超过1010。

    输出

    输出仅有一行。针对妈妈写出的单词,按照字典排列的顺序从小到大排列成一行的结果,每个单词后带一个空格。

    样例输入

    4
    city boy tree student
    

    样例输出

    boy city student tree 
    
    //方法一:用algorithm里的sort函数
    #include<iostream>
    #include<algorithm>
    #include<string>
    using namespace std;
    int main()
    {
    	int n;
    	cin>>n;
    	string s[n];
    	for(int i=0;i<n;i++)
    	cin>>s[i];
        sort(s,s+n);
        for(int j=0;j<n;j++)
        cout<<s[j]<<' ';
        return 0;
    	
     } 
    
    
    //方法二:用二维字符数组+冒泡+string.h里的(strcmp函数+strcpy函数)
    #include<iostream>
    #include<string.h>
    using namespace std;
    int main()
    {
        int n,i,j;
        char cha[27][1010],temp[1010];
        cin>>n;
        for(i=0;i<n;i++) 
        {
            cin>>cha[i];
        }
        for(i=0;i<n-1;i++)
        {
             for(j=0;j<n-1-i;j++) //冒泡+strcmp函数+strcpy函数
             {       
                if(strcmp(cha[j],cha[j+1])>0)
                {
                    strcpy(temp,cha[j]);
                    strcpy(cha[j],cha[j+1]);
                    strcpy(cha[j+1],temp);
                }
            }
        }
        for(i=0;i<n;i++)
        {
            cout<<cha[i]<<' ';
        }
        return 0;
    }
    
    
    

    问题 D: Power Strings

    题目描述

    Given two strings a and b we define ab to be their concatenation. For example, if a = "abc" and b = "def" then ab = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = "" (the empty string) and a^(n+1) = a*(a^n).

    输入

    Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.

    输出

    For each s you should print the largest n such that s = a^n for some string a.

    样例输入

    abcd
    aaaa
    ababab
    .
    

    样例输出

    1
    4
    3
    
    #include<iostream>
    #include<string.h> 
    using namespace std;
    char s[1000005];
    int ne[1000001];
    void GetNext(char *a)
    {
        int len=strlen(a);
        int i=0, j=-1;
        ne[0]=-1;
        while(i<len)
        {
            if(j==-1||a[i]==a[j])
            {
                ne[++i]=++j;
            }
            else j=ne[j];
        }
    }
    
    int main()
    {
         while(cin>>s&&s[0]!='.')
         {
              GetNext(s);
              int L=strlen(s);
              if(L%(L-ne[L])==0)///如果第i-1位为结尾的循环必定有 i%(i-Next[i])==0
                   cout<<(L/(L-ne[L]))<<endl;///0~i-1共有i个字符,i/(i-Next[i])就是循环次数
              else
                   cout<<"1"<<endl;
          }
         return 0;
    }
    

    问题 E: 迷宫问题

    题目描述

    小明置身于一个迷宫,请你帮小明找出从起点到终点的最短路程。
    小明只能向上下左右四个方向移动。

    输入

    输入包含多组测试数据。输入的第一行是一个整数T,表示有T组测试数据。
    每组输入的第一行是两个整数N和M(1<=N,M<=100)。
    接下来N行,每行输入M个字符,每个字符表示迷宫中的一个小方格。
    字符的含义如下:
    ‘S’:起点
    ‘E’:终点
    ‘-’:空地,可以通过
    ‘#’:障碍,无法通过
    输入数据保证有且仅有一个起点和终点。

    输出

    对于每组输入,输出从起点到终点的最短路程,如果不存在从起点到终点的路,则输出-1。

    样例输入

    1
    5 5
    S-###
    -----
    ##---
    E#---
    ---##
    

    样例输出

    9
    
    #include<iostream>
    #include<string.h>
    #include<queue>
    using namespace std;
    char map[105][105]={0};
    bool bmap[105][105]={0};//1被访问过 
    int x[4]={1,0,-1,0};//tx,ty竖着看,对行进行移动,竖着移,常规+1可认为y轴负方向 
    int y[4]={0,1,0,-1};//对列进行移动,横着移,常规+1可认为x轴正方向
    int n,m;
    struct s
    {
        int a,b,step;
    };
    queue<s> q1;
    int bfs() 
    {
        int a,b,c=0;
        s t=q1.front();
        a=t.a ,b=t.b;
        while(map[a][b]!='E'&&!q1.empty())
        {
            q1.pop();s ss[4];
            for(int i=0;i<4;i++)
            {
                if(a+x[i]<0||b+y[i]<0||a+x[i]>=n||b+y[i]>=m)continue;
                else if(map[a+x[i]][b+y[i]]=='#'||bmap[a+x[i]][b+y[i]]==1)continue;
                else 
                {
                    ss[i].a=a+x[i],ss[i].b=b+y[i],ss[i].step=t.step+1;
                    bmap[a+x[i]][b+y[i]]=1;
                    q1.push(ss[i]);
                }
            }
            t=q1.front();
            a=t.a ,b=t.b;
            if(map[a][b]=='E')break;    
        }
        if(map[a][b]=='E')return t.step;
        return -1;
    }
    int main()
    {
        int a;
        cin>>a;
        while(a--)
        {
            memset(map,0,sizeof(map));
            memset(bmap,0,sizeof(bmap));
            while(!q1.empty())q1.pop();//有n个迷宫需清空队列q1 
                int a,b;
                cin>>n>>m;
                for(int i=0;i<n;i++)
                    for(int j=0;j<m;j++)
                    {
                        cin>>map[i][j];
                        if(map[i][j]=='S')
                        a=i,b=j;
                    }
                bmap[a][b]=1;
                s ss[4];
                for(int i=0;i<4;i++)
                {
                    if(a+x[i]<0||b+y[i]<0||a+x[i]>=n||b+y[i]>=m||map[a+x[i]][b+y[i]]=='#')continue;
                    else 
                    {
                        ss[i].a=a+x[i],ss[i].b=b+y[i],ss[i].step=1;bmap[ss[i].a][ss[i].b]=1;
                        q1.push(ss[i]);
                    }
                }
            int step=bfs();
            cout<<step<<endl;
        }
        return 0;
    }
    

    感谢学长留下的财富
    点击传送

  • 相关阅读:
    mysql左连接、右链接、内连接
    各种排序算法及其java程序实现(转载)
    将Android手机设备挂载到ubuntu中
    EditText 不让其自动获取焦点
    Ubuntu安装JDK+Tomcat+Eclipse以及Android adb配置环境变量
    ADT22解决引用第三方jar提示java.lang.NoClassDefFoundError
    winform实现类似google的搜索提示Suggest Search
    抽象工厂模式
    各种小知识随笔
    CSS知识点【待整理】
  • 原文地址:https://www.cnblogs.com/gylic/p/13042487.html
Copyright © 2011-2022 走看看