freemandj 2020-03-10 21:49 采纳率: 100%
浏览 321
已采纳

我写了一个,但答案不对,希望帮忙改一下,谢谢

我们已经知道了将N个整数按从小到大排序的冒泡排序法。本题要求将此方法用于字符串序列,并对任意给定的K(<N),输出扫描完第K遍后的中间结果序列。

输入格式:
输入在第1行中给出N和K(1≤K<N≤100),此后N行,每行包含一个长度不超过10的、仅由小写英文字母组成的非空字符串。

输出格式:
输出冒泡排序法扫描完第K遍后的中间结果序列,每行包含一个字符串。

输入样例:
6 2
best
cat
east
a
free
day

输出样例:
best
a
cat
day
east
free

以下是我的代码
#include
#include
using namespace std;
#define max 10000
int main()
{
string str[max],str1;
int i,n,k,j,m,l;
cin>>n>>k;
for(i=0;i {
str[i]=cin.get();
}
for(j=0;j for(m=0;m {
if(str[m]>str[m+1])
{
str1=str[m];
str[m]=str[m+1];
str[m+1]=str1;
}
}
}

for(l=0;l<n;l++)
{
    cout<<str[l]<<endl;
}

}

  • 写回答

1条回答 默认 最新

  • threenewbee 2020-03-10 22:12
    关注
    // Q1058158.cpp : Defines the entry point for the console application.
    //
    
    #include <iostream>
    #include <string>
    using namespace std;
    #define max 10000
    int main()
    {
        string str[max],str1;
        int i,n,k,j,m,l;
        cin>>n>>k;
        for(i=0;i <n;i++){
            cin >> str[i];
        }
        for(j=0;j<n-1;j++) {
            for(m=0;m<n-j-1;m++) {
                if(str[m]>str[m+1])
                {
                    str1=str[m];
                    str[m]=str[m+1]; 
                    str[m+1]=str1;
                }
            }
            if (j == k-1)
            {
                for(l=0;l<n;l++)
                {
                    cout<<str[l]<<endl;
                }
            }
        }
        //for(l=0;l<n;l++)
        //{
        //  cout<<str[l]<<endl;
        //}
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用