uva10905同一思路的两种做法,前一种WA,后一种AC
这道题应该算一道普通的排序吧,实际上就是另一种形式地比大小,自己最开始是用int型存,后来觉着不行,改用long,结果还是WA,这是第一个程序。
第二个程序是改用string处理,确实比int方便很多,在运算符重载那里体现就非常明显。
这个题目用到了之前总结过的结构体加入优先级队列需要重载运算符,之所以写成结构体是因为我不知道直接将string加入优先级队列时怎么写比较函数。。。⊙﹏⊙b
通过这个题目充分说明你不知道测试数据到底有多么大,所以还是用string来处理大数据比较好。
转载请注明出处,谢谢!
原文地址:http://blog.csdn.net/monkeyduck
第一个程序
#include<iostream>
#include<cmath>
#include<queue> using namespace std; struct Node
{
int data,size;
bool operator < (const Node& a) const
{
if (size==a.size)
return data<a.data;
long long t1=(data*pow(10,double(a.size))+a.data);
long long t2=(a.data*pow(10,double(size))+data);
return t1<t2;
}
}; int n;
int main ()
{
while (cin>>n&&n)
{
priority_queue<Node> que;
for (int i=0;i<n;i++)
{
int num,flag=0;
cin>>num;
Node node;
node.data=num;
while (num)
{
num=num/10;
flag++;
}
node.size=flag;
que.push(node); }
while(!que.empty())
{
cout<<que.top().data;
que.pop();
}
cout<<endl; }
return 0;
}
第二个程序,AC代码
#include<iostream>
#include<queue>
#include<string> using namespace std;
struct Node
{
string str;
bool operator < (const Node& a) const
{
return str+a.str<a.str+str;
}
};
priority_queue<Node> que;
int n;
int main()
{
while (cin>>n&&n)
{
for (int i=0;i<n;i++)
{
Node node;
cin>>node.str;
que.push(node);
}
while (!que.empty())
{
cout<<que.top().str;
que.pop();
}
cout<<endl;
}
return 0;
}
uva10905同一思路的两种做法,前一种WA,后一种AC的更多相关文章
- jqGrid中实现radiobutton的两种做法
http://blog.sina.com.cn/s/blog_4f925fc30102e27j.html jqGrid中实现radiobutton的两种做法 ------------------- ...
- SqlServer保留几位小数的两种做法
SqlServer保留几位小数的两种做法 数据库里的 float momey 类型,都会精确到多位小数.但有时候 我们不需要那么精确,例如,只精确到两位有效数字. 解决: 1. 使用 Round( ...
- LVS+keepalived 的DR模式的两种做法
LVS DR模式搭建 准备工作 三台机器: dr:192.168.13.15 rs1:192.168.13.16 rs2: 192.168.13.17 vip:192.168.13.100 修改DR上 ...
- leetcode234 回文链表 两种做法(stack(空间非O(1)),空间O(1))
link: leetcode234 回文链表 方法1, 快慢指针,把前半部分存入栈中和后半部分比较 public boolean isPalindrome(ListNode head) { if(he ...
- Mediocre String Problem (2018南京M,回文+LCP 3×3=9种做法 %%%千年好题 感谢"Grunt"大佬的细心讲解)
layout: post title: Mediocre String Problem (2018南京M,回文+LCP 3×3=9种做法 %%%千年好题 感谢"Grunt"大佬的细 ...
- [置顶]
echarts x轴文字显示不全(xAxis文字倾斜比较全面的3种做法值得推荐)
echarts x轴标签文字过多导致显示不全 如图: 解决办法1:xAxis.axisLabel 属性 axisLabel的类型是object ,主要作用是:坐标轴刻度标签的相关设置.(当然yAxis ...
- 代码:PC 链接列表面板border的一种做法(每行之间有分割线)
PC 链接列表面板,border的一种做法 做页面经常遇到一种问题,上面是标题,下面是单行链接列表.为了保证后台套页面方便,所有列表项必须完全一样.但我们无法解决第一行或最后一行多出来的分割线. 使用 ...
- .NET类型转型的四种做法(转)
.NET类型转型的四种做法: ◆ 强制转型:(int)变量名称 ◆ int.Parse(字符串变量名称) ◆ Convert.To类型(变量名称) ◆ TryParse 强制转型 (casting) ...
- Android布局居中的几种做法
Android的布局文件中,如果想让一个组件(布局或View)居中显示在另一个布局(组件)中,可以由这么几种做法: android:layout_gravity android:gravity and ...
随机推荐
- 八皇后问题(dfs)
#include <iostream> #include <stdio.h> using namespace std; ; ], b[], c[], vis[][]; //a, ...
- Mac OS 10.12 - 在VMwear Workstation12.5.2中以两种方式进入恢复模式(Recovery)!!!
注意:如果你打算安装Mac OS10.12 到虚拟机里面学习,那么我强烈建议你在没有安装任何其它软件之前,按照我这篇博客来进入恢复模式(Recovery),禁用Rootless机制!!!这样处理后,你 ...
- BZOJ 3932 [CQOI2015]任务查询系统 - 差分 + 主席树
Solution 差分就好了, 在$s_i$ 的点+1, $e_i + 1$ 的点 - 1. 查询的时候注意$l == r$ 要返回 $k * b[l]$ ,而不是$sum[node] $因为当前位置 ...
- linux学习第四天 (Linux就该这么学)2018年11月16日
今天主要讲了 管道符,重写向与环境变量 输入输出重写向 标准输出重写向 (标准,覆盖,错误) > 将标准输出重写向到一个文件中 >> 追加到文件 2>错误输出重定向 2> ...
- css字符串转换为类map对象及反转
存储对象为啥是类map(即:{key:val,...}格式),因为Map对象的val为字符时,无法存储 '('.')' 左右括号,我也很无奈╮(╯▽╰)╭ 解析脚本: <!DOCTYPE htm ...
- vue ui之 iview 事件拦截
用过easyui的,应该清楚easyui组件有很多before事件拦截,有时候会特别重要. 最近在研究vue的ui组件iview虽然功能也不错,感觉还是没有像easyui那样强大,就比如before事 ...
- IOS初级:UITableView
先来看一下tableview 的结构(plain style). -------------------------------------- + header ...
- php实现MySQL两库对比升级版
define('DATABASE1', 'db1'); $dbi1 = new DbMysql; $dbi1->dbh = 'mysql://root:password@127.0.0.1/'. ...
- unity的三种update
void FixedUpdate () 固定更新 void Update () 更新 void LateUpdate() 晚于更新 FixedUpdate () 和 Update () ...
- 747. Largest Number At Least Twice of Others
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...