codeforces Educational Codeforces Round 5 A. Comparing Two Long Integers
题目链接:http://codeforces.com/problemset/problem/616/A
题目意思:顾名思义,就是比较两个长度不超过 1e6 的字符串的大小
模拟即可。提供两个版本,数组版本 & 指针版本。
(1)数组版本(短的字符串从高位处补0,直到跟长的字符串长度相同)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std; const int maxn = 1e6 + ;
char a[maxn], b[maxn];
int rev_a[maxn], rev_b[maxn]; int cmp(int len)
{
int f = ; // 0: a=b; 1: a>b; 2: a<b
// 比较的时候要从高位比起,存储的时候是从低位开始存的
for (int i = len-; i >= && !f; i--) {
if (rev_a[i] > rev_b[i]) {
f = ;
}
else if (rev_a[i] < rev_b[i]) {
f = ;
}
}
return f;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE while (scanf("%s%s", a, b) != EOF) {
int la = strlen(a);
int lb = strlen(b); for (int i = ; i < la; i++) {
rev_a[la-i-] = a[i]-'';
} for (int i = ; i < lb; i++) {
rev_b[lb-i-] = b[i]-'';
} int flag = ;
// 保证比较的字符串长度相等, 0补上
if (la < lb) { // la < lb
for (int i = ; i < lb-la; i++) {
rev_a[la+i] = ;
}
flag = cmp(lb);
}
else { // la >= lb
for (int i = ; i < la-lb; i++) {
rev_b[lb+i] = ;
}
flag = cmp(la);
}
if (flag == ) puts(">");
else if (flag == ) puts("<");
else puts("=");
}
return ;
}
(2)指针版本(过滤前缀0之后,再逐位比较大小)
/*
指针版本
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std; const int maxn = 1e6 + ;
char a[maxn], b[maxn]; int cmp(char *s1, char *s2)
{
// 过滤前缀 0
while (*s1 == '') {
s1++;
}
while (*s2 == '') {
s2++;
} int l1 = strlen(s1);
int l2 = strlen(s2);
if (l1 > l2) {
return '>';
}
else if (l1 < l2) {
return '<';
}
// a,b长度相等(l1 = l2)
for (int i = ; i < l1; i++) {
if (*s1 < *s2) { // 指针指向的值
return '<';
}
else if (*s1 > *s2) {
return '>';
}
s1++; // 指针右移一位
s2++;
}
return '=';
} int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE while (scanf("%s%s", a, b) != EOF) {
printf("%c\n", cmp(a, b));
}
return ;
}
codeforces Educational Codeforces Round 5 A. Comparing Two Long Integers的更多相关文章
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...
- Codeforces Educational Codeforces Round 5 A. Comparing Two Long Integers 高精度比大小,模拟
A. Comparing Two Long Integers 题目连接: http://www.codeforces.com/contest/616/problem/A Description You ...
- Educational Codeforces Round 5 A. Comparing Two Long Integers
A. Comparing Two Long Integers time limit per test 2 seconds memory limit per test 256 megabytes inp ...
- codeforces Educational Codeforces Round 16-E(DP)
题目链接:http://codeforces.com/contest/710/problem/E 题意:开始文本为空,可以选择话费时间x输入或删除一个字符,也可以选择复制并粘贴一串字符(即长度变为两倍 ...
- Codeforces Educational Codeforces Round 15 E - Analysis of Pathes in Functional Graph
E. Analysis of Pathes in Functional Graph time limit per test 2 seconds memory limit per test 512 me ...
- Codeforces Educational Codeforces Round 15 D. Road to Post Office
D. Road to Post Office time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codeforces Educational Codeforces Round 15 C. Cellular Network
C. Cellular Network time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Educational Codeforces Round 5 E. Sum of Remainders 数学
E. Sum of Remainders 题目连接: http://www.codeforces.com/contest/616/problem/E Description The only line ...
随机推荐
- 自己用WordPress搭了个站点
这周买了台阿里云,用wordpress搭了个自己的站点,折腾了几天. 网站的网址是 http://www.smarteyeball.com/ ,取名慧眼网,宗旨是让大家发现新世界.
- [译]git init
git init git init命令用来创建一个新的Git仓储.可以用在一个已经存在的但是没有受Git版本控制的项目,或者用来初始化一个全新的没有任何文件的空仓储.git init通常是你开始一个新 ...
- 【AngularJS】—— 4 表达式
前面了解了AngularJS的基本用法,这里就跟着PDF一起学习下表达式的相关内容. 在AngularJS中的表达式,与js中并不完全相同. 首先它的表达式要放在{{}}才能使用,其次相对于javas ...
- Runas命令:能让域用户/普通User用户以管理员身份运行指定程序。
注:本文由Colin撰写,版权所有!转载请注明原文地址,谢谢合作! 在某些情况下,为了安全起见,大部分公司都会使用域控制器或只会给员工电脑user的用户权限,这样做能大大提高安全性和可控性,但由此也带 ...
- AlwaysOn可用性组测试环境安装与配置(一)--SQL群集环境搭建
一.测试环境介绍 1. 宿主使用工作站(HYPR-V)基本配置如下: 处理器:Intel(R) Core(TM) i5-4470 CPU @ 3.20GHz 3.20GHz 内存(RAM):8.00G ...
- nyoj 171 聪明的kk
聪明的kk 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 聪明的“KK”非洲某国展馆的设计灵感源于富有传奇色彩的沙漠中陡然起伏的沙丘,体现出本国不断变换和绚丽多彩的 ...
- Octal Fractions
Octal Fractions Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 149 Solved: 98 Description Fractions ...
- linux 驱动程序中断号的申请
1.linux下查看硬中断与软中断 硬中断:/proc/interrupts 软中断:/proc/softirqs 往往一些硬件中断号都是跟CPU所分配的硬件中断号相匹配的. 即同类型的cpu可能对同 ...
- servlet过滤器实现维护项目
最近公司需要系统维护,提出要建一个维护系统,要求: 1.访问公司域名跳到系统首页 2.点击首页的任意按钮给出维护提示信息 3.用户访问之前收藏的任意系统链接跳转到首页 下面介绍下用过滤器实现上述需求 ...
- windows server 2008 R2 SP1 安装exchange 2010
一. 先决条件 若在windows server R2 SP1企业版系统上典型安装exchange server2010 SP3,则需要提前确定一下先决条件 AD域环境,域和林的功能级别必须是wind ...