第14周 oj平台<对10个国家进行排序>




/* 
 * Copyright (c) 2014, 烟台大学计算机学院 
 * All rights reserved. 
 * 文件名称:test.cpp 
 * 作    者:呼亚萍 
 * 完成日期:2014年 12 月 3日 
 * 版 本 号:v1.0 
 * 
 * 问题描述:编写程序,输入10个国家的名字,根据字母进行排序
 * 输入描述:相应的程序
 * 程序输出:输出10个排序后的国家名称
 */ 

#include <iostream>
using namespace std;
const int N=10;
int main( )
{
    int i,j;
    string t,str[N];
    for(i=0; i<N; i++)
        cin>>str[i];
    for (j=0; j<N-1; j++)
        for(i=0; i<=N-j-1; i++)
            if (str[i]>str[i+1])
            {
                t=str[i];
                str[i]=str[i+1];
                str[i+1]=t;
            }
    for(i=0; i<N; i++)
        cout<<str[i]<<endl;
    cout<<endl;
    return 0;
}

运算结果:


知识点总结:

数组的应用,利用冒泡法对10个国家进行依次比较

学习心得:

在开始的时候,结果总是输出错误,在看老师的博客中发现错误,不要放弃!加油!

7-12 计数 分数 5 作者 cjyoleon 单位 哈尔滨理工大学 输入一组整数,统计其中每个整数出现的次数。 输入格式: 输入包括2行。 第一行为一个[3, 1000]区间内的整数 n。 第二行为n个[1, 10000]区间内的整数。各整数之间以空格分隔。 输出格式: 输出包括若干行,每一行包括输入中出现的一个整数及其出现的次数。该整数和其出现的次数之间以“-”连接。 须按由大到小的顺序输出各整数出现的次数。 输入样例: 7 3 2 3 4 3 2 1 输出样例: 4-1 3-3 2-2 1-1 代码长度限制 16 KB 时间限制 400 ms 内存限制 64 MB 栈限制 8192 KB C++ (g++) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include <iostream> #include <vector> #include <map> #include <algorithm> using namespace std; int main() { int n; cin >> n; map<int, int> countMap; for (int i = 0; i < n; i++) { int x; cin >> x; countMap[x]++; } // 转为 vector 存储 (number, count) vector<pair<int, int>> freq; for (auto& p : countMap) { freq.push_back({p.first, p.second}); } // 自定义排序:先按频次降序,频次相同则按数字降序 sort(freq.begin(), freq.end(), [](const pair<int, int>& a, const pair<int, int>& b) { if (a.second != b.second) { return a.second > b.second; // 频次高的在前 } return a.first > b.first; // 频次相同时,数字大的在前 }); // 输出结果 for (auto& p : freq) { cout << p.first << "-" << p.second << endl; } return 0; }
11-24
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<sstream> #include<string> #include<algorithm> #include<cstring> #include<cmath> #include<cstdlib> #include<vector> #include <cstdio> #include <queue> #include <map> using namespace std; int n; long long m; long long shu[1000005]; bool compare(long long x, long long y); long long process(long long x, long long shu[]); int main() { cin >> n >> m; int i; for (i = 0; i < n; i++) { scanf("%lld", &shu[i]); } sort(shu, shu + n,compare); long long ans = 0; long long l = 10, r = 0; int sign = 0; int left = 0, right = n - 1; while (left+1<right) { int mid = (right - left) / 2 + left; long long temp = shu[mid]; long long total = process(temp, shu); if (total < m) { right = mid; } else if (total > m) { left = mid; } else if (total == m) { ans = temp; sign = 1; break; } } if (sign == 0) { int i; for (i = left; i <=right; ) { long long temp = shu[i]; long long total = process(temp, shu); if (total > m) { i++; } else if (total == m) { ans = temp; break; } else if (total < m) { if (i == 0) { l = 0, r = temp - 1; break; } else { l = shu[i - 1] + 1; r = temp - 1; break; } } } while (l <= r) { long long mid = (r - l) / 2 + l; long long total = process(mid, shu); if (total == m) { ans = mid; break; } else if (total < m) { r = mid - 1; } else { l = mid + 1; } } } printf("%lld\n", ans); return 0; } long long process(long long x, long long shu[]) { int i; long long total = 0; for (i = n - 1; shu[i] >= x&&i>=0; i--) { total += (shu[i] - x); } return total; } bool compare(long long x, long long y) { return x < y; } //4 26 40 42 46
最新发布
12-04
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值