ZOJ.3437 Very Hard Problem【strtoll函数】 2015/09/23
2015-09-23 18:46
295 查看
Very Hard Problem
Time Limit: 2 Seconds
Memory Limit: 65536 KB Special Judge
It was in the ancient world. ZOJ, the greatest treasure hunter in the world has been lost in the forest for more than 100 hours. Being with no food and no water for such a long time, he was really exhausted this night.
He then took out an old map, which was a very strange map. There are many stared positions on the map and it seems those stars are connected by some roads. "I'm sure the treasure is near me, but where is it?", said ZOJ, "If I can't find it, I will be laughed
by others."
Suddenly, ZOJ noticed a slight light. "It was unsual.", said ZOJ and he started looking for the treasure again. After three hours' search, he finally found an entrance. But to enter the entrance, a puzzle should be solved.
The puzzle was described like this. Every time, you were given a character in the set {'-', '!', '~'} and a b-based number. You should take the character as an operator (i.e. '-' changes a number to its opposite number, '!' changes zero to one and non-zero
value to zero, '~' takes bitwise operation NOT on all 64 bits of a number) and operates on the number. All you need to do is to print out the result.
The puzzle seemed quite simple, but ZOJ was only good at working out the output of the programs written by others, and had no idea about how to solve such a problem. So he turned to you for help.
b and a number n in b-based (2 ≤ b ≤ 16, when
b is no less than 10, 'a'..'f' or 'A'..'F' are used) are given in order in one line.
ch is assured to be in the set {'-', '!', '~'}, the number (n)b is assured in the range of a signed 64-bit integer.
ch, b, n are seperated by one or more spaces. Leading or trailing spaces may also be added to the lines.
Author: ZHUANG, Junyuan
Contest: Let's Celebrate the 100th Contest on ZOJ!
strtoll函数,将字符型转化成对应进制的数值型。取-时,注意越界
Time Limit: 2 Seconds
Memory Limit: 65536 KB Special Judge
It was in the ancient world. ZOJ, the greatest treasure hunter in the world has been lost in the forest for more than 100 hours. Being with no food and no water for such a long time, he was really exhausted this night.
He then took out an old map, which was a very strange map. There are many stared positions on the map and it seems those stars are connected by some roads. "I'm sure the treasure is near me, but where is it?", said ZOJ, "If I can't find it, I will be laughed
by others."
Suddenly, ZOJ noticed a slight light. "It was unsual.", said ZOJ and he started looking for the treasure again. After three hours' search, he finally found an entrance. But to enter the entrance, a puzzle should be solved.
The puzzle was described like this. Every time, you were given a character in the set {'-', '!', '~'} and a b-based number. You should take the character as an operator (i.e. '-' changes a number to its opposite number, '!' changes zero to one and non-zero
value to zero, '~' takes bitwise operation NOT on all 64 bits of a number) and operates on the number. All you need to do is to print out the result.
The puzzle seemed quite simple, but ZOJ was only good at working out the output of the programs written by others, and had no idea about how to solve such a problem. So he turned to you for help.
Input
There are multiple test cases. In each test case, a character ch, a numberb and a number n in b-based (2 ≤ b ≤ 16, when
b is no less than 10, 'a'..'f' or 'A'..'F' are used) are given in order in one line.
ch is assured to be in the set {'-', '!', '~'}, the number (n)b is assured in the range of a signed 64-bit integer.
ch, b, n are seperated by one or more spaces. Leading or trailing spaces may also be added to the lines.
Output
For each test case, print one line, the 10-based result.Sample Input
~ 10 4 ! 10 0 - 16 -F
Sample Output
-5 1 15
Author: ZHUANG, Junyuan
Contest: Let's Celebrate the 100th Contest on ZOJ!
strtoll函数,将字符型转化成对应进制的数值型。取-时,注意越界
#include<iostream> #include<cstdio> #include<string.h> #include<cmath> #include<limits.h> #include<stdlib.h> using namespace std; int main(){ char str[10],temp[110]; int b; while( ~scanf("%s%d%s",str,&b,temp) ){ long long n = strtoll(temp,NULL,b); if( str[0] == '~' ) printf("%lld\n",~n); else if( str[0] == '!' ) printf("%d\n",!n); else{ if( n == LLONG_MIN ) printf("%llu\n",(long long unsigned)n); else printf("%lld\n",-n); } } return 0; }
相关文章推荐
- intro: what's the problem
- 在做微信第三方的时候遇到的问题、导包问题
- [memcached]部署memcached环境及PHP扩展
- mysqldump备份警告
- 探索Ionic过程中的一些亮点与各种坑整理
- LeetCode(49) Populating Next Right Pointers in Each Node I II
- 数据库存储树形结构的数据
- 安卓:ActionBar的分离
- quick减少包体
- shell 时间统计脚本
- [shell]php-fpm容易假死,实现自动重启php服务~
- java 容器中排序算法的源码实现分析(京东电话面试)
- 黑马程序员---Java基础---IO流
- 排序值冒泡排序的详解
- linux下添加PATH环境变量
- 程序员的业余项目
- Linux 命令 - traceroute: 数据报传输路径追踪
- MySQL 表名大小写区分
- 程序员的业余项目
- 程序员的业余项目