标题:怎么整理数列
只看楼主
xxm61424
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-5-6
结帖率:0
已结贴  问题点数:20 回复次数:3 
怎么整理数列
Your program will do the following:
1. Read in a positive integer n representing the number of values the user will enter.
2. Then read in n double values into an array. You may assume that n <= 1000, i.e., the
user will enter at most 1000 numbers.
3. Use an algorithm discussed in class to sort this array from smallest to largest.
4. Then print out for each distinct value (from smallest to largest), how many times it
appeared in the sequence.
这是国外大学的一个作业,小弟只会把数列以顺序的形式来输出,但是这个要求把数列里面的相同元素分为一类,然后再输出,我是第一次学这个,完全不懂,希望大家能帮个忙,
#include <iostream>
using namespace std;
int main () {
    int i;
    int n;
    int j;
    double decimal[1000];
    int counter;               
    int index;
    double temp;
    int constant = 0;
    cout << "Enter a positive integer:" << "\n";
    cin >> n;
    cout << "Enter " << n << " numbers:" << "\n";
        for (i = 0; i < n; i = i + 1) {
            cin >> decimal[i];
        }
        for (i = 0; i < n-1; i = i + 1) {
        index = i;
        for (j = i + 1; j < n; j = j + 1) {
            if (decimal[index] > decimal[j]) {
                index = j;
            }
        }
        temp = decimal[i];
        decimal[i] = decimal[index];
        values[index] = temp;
    }
        for (counter = 0;counter < n) {
            for (i = 0;i < n;i = i + 1) {
               
      
        
               
return 0;
}
这是我写完的一段,我现在在想接下来怎么写,
搜索更多相关主题的帖子: into following positive sequence 
2013-05-06 02:03
xxm61424
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-5-6
得分:0 
求大家了,我以前完全没学过啊,
2013-05-06 02:10
azzbcc
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江西财经大学
等 级:贵宾
威 望:81
帖 子:3293
专家分:12919
注 册:2012-11-4
得分:10 
要用到三个数据类型 double data[1000],int count[1000] = {0}, int ip = 0;

dta存储出现的数据,count存储出现次数,ip表示当前位置

然后遍历数组。


[fly]存在即是合理[/fly]
2013-05-06 07:05
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:10 
程序代码:
include <vector>
#include <algorithm>
#include <iostream>

int main()
{
    size_t n;
    std::cin >> n;
    if( !std::cin || n==0 )
        return 1;

    std::vector<double> ds;
    ds.resize( n );

    for( size_t i=0; i!=n; ++i )
        std::cin >> ds[i];

    std::sort( ds.begin(), ds.end() );

    // 以下这段代码就是你需要的
    {
        double pre = ds[0];
        size_t count = 1;

        for( size_t i=1; i<n; ++i )
        {
            if( ds[i] == pre )
                ++count;
            else
            {
                std::cout << pre << '\t' << count << '\n';

                pre = ds[i];
                count = 1;
            }
        }

        std::cout << pre << '\t' << count << std::endl;
    }

    return 0;
}
2013-05-06 08:43



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-406290-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.091972 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved