<sdut-ACM>1244数列有序

数列有序!

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

有n(n<=100)个整数,已经按照从小到大顺序排列好,现在另外给一个整数m,请将该数插入到序列中,并使新的序列仍然有序。

输入

输入数据包含多个测试实例,每组数据由两行组成,第一行是n和m,第二行是已经有序的n个数的数列。n和m同时为0表示输入数据的结束,本行不做处理。

输出

对于每个测试实例,输出插入新的元素后的数列。

示例输入

3 3
1 2 4
0 0

示例输出

1 2 3 4






//代码<1>


#include
    
    
     
     
int main()
{
    int n, m, i;
    int a[120];
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        int flag = 0;                         //判定插入是否已经进行过
        if(m==0&&n==0)  break;
        for(i = 0; i <= n-1; i++)
        {
            scanf("%d",&a[i]);
        }
        for(i = 0; i <= n-1; i++)
        {
            if(m <= a[i]&&flag == 0)
            {
                for(int j = n;j > i; j--)    //移位
                {
                    a[j] = a[j-1];
                }
                a[i] = m;
                flag = 1;
            }
            if(m > a[n-1])                    //如果m比任何数都大, 就直接放在最后
                a[n] = m;
        }
        for(i = 0; i <= n; i++)
        {
            if(i==0)
                printf("%d",a[i]);
            else
                printf(" %d",a[i]);
        }
        printf("\n");
    }
    return 0;
}



//代码<2>


#include
     
     
      
      
int main()
{
   int n, i, j, a[110], m;
   while(scanf("%d %d",&n,&m)!=EOF)
   {
       if(m == 0&&n == 0)break;
        for(i = 0;i<=n-1;i++)
           {
               scanf("%d",&a[i]);
           }
          for (i=0; i
      
      
       
        m)
                break;             //如果找到大于要插入的字符就跳出循环
        for (j=n; j>i; j--)        //插入字符
            a[j] = a[j-1];
        a[i] = m;
        for (i=0; i
       
       
      
      
     
     
    
    

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个简单的学生集合的示例代码,您可以参考: ```c++ #include <iostream> #include <string> #include <vector> using namespace std; class Student { private: string name; int age; double score; public: Student(string n, int a, double s) : name(n), age(a), score(s) {} string getName() const { return name; } int getAge() const { return age; } double getScore() const { return score; } }; class StudentCollection { private: vector<Student> students; public: void addStudent(Student s) { students.push_back(s); } void removeStudent(Student s) { for (vector<Student>::iterator it = students.begin(); it != students.end(); ++it) { if (it->getName() == s.getName()) { students.erase(it); break; } } } void printStudents() const { for (vector<Student>::const_iterator it = students.begin(); it != students.end(); ++it) { cout << "Name: " << it->getName() << ", Age: " << it->getAge() << ", Score: " << it->getScore() << endl; } } }; int main() { StudentCollection sc; sc.addStudent(Student("Tom", 18, 90)); sc.addStudent(Student("Jerry", 20, 80)); sc.addStudent(Student("Alice", 19, 85)); sc.printStudents(); sc.removeStudent(Student("Jerry", 20, 80)); sc.printStudents(); return 0; } ``` 这个示例代码中,定义了一个 `Student` 类和一个 `StudentCollection` 类。其中 `Student` 类表示一个学生,包括学生姓名、年龄、分数等信息。`StudentCollection` 类表示一个学生集合,包括添加学生、删除学生、打印学生信息等操作。在 `main` 函数中,演示了如何使用 `StudentCollection` 类来管理学生集合。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值