<NOIP> 19 . P1553 数字反转(升级版)
2017-06-30 15:44
267 查看
题解:这是洛谷的第19道题目,这个题目的Testcase有很多都想不到,可谓千奇百怪。
注意
1 . 整数的最高位不能为0;
2 . 反转后的小数的整数部分的最高位不能为0,小数部分的末尾不能为0,除非小数部分都是0,则保留一个0;
3 . 反转后的分数的分子和分母不用约分,同样,分子和分母各自的最高位都不为0;
4 . 小数的输入存在这种测试用例:1020.0101000 ,输出为:201.101
5 . 反转后的百分数的整数部分的最高位不为0;
6 . 0反转后为0;
7 . 需要在输入字符串的时候,需要将‘.’、‘/’、‘%’提取出来,小数需要将整数部分和小数部分都分别提取出来,分数需要将分子和分母分别提取;
源代码:
#include <iostream>
#include <string.h>
#include <sstream>
#include <stdlib.h>
using namespace std;
void Filp1(const std::string str,int size)
{
string str1;
bool zeroornot = true;
for (size_t i = 0; i < size; i++)
{
if (str[i] != '0')
zeroornot = false;
}
if (zeroornot)
cout << "0";
else
{
bool judge = true;
for (int i = size - 1; i >= 0; i--)
str1 += str[i];
for (size_t i = 0; i < size; i++)
{
while (str1[i] == '0' && judge)
i++;
cout << str1[i];
judge = false;
}
}
}
void Filp2(const std::string str, int size)
{
string str1 = "";
bool zeroornot = true;
for (size_t i = 0; i < size; i++)
{
if (str[i] != '0')
zeroornot = false;
}
if (zeroornot)
cout << "0";
else
{
bool judge = true;
for (size_t i = 0; i < size; i++)
{
while (str[i] == '0' && judge)
i++;
str1 += str[i];
judge = false;
}
for (int i = str1.size()-1; i >= 0; i--)
cout << str1[i];
}
}
int main()
{
string input;
string first, second;
cin >> input;
string c;
int inputsize = input.size();
for (int i = 0; i < inputsize; i++)
{
if (input[i] == '0' || input[i] == '1' || input[i] == '2' || input[i] == '3' || input[i] == '4' ||
input[i] == '5' || input[i] == '6' || input[i] == '7' || input[i] == '8' || input[i] == '9')
second += input[i];
else
{
c = inpu
4000
t[i];
first = second;
second = "";
}
}
if (c == ".")
{
Filp1(first, first.size());
cout << c;
Filp2(second, second.size());
}
else if (c == "/")
{
Filp1(first, first.size());
cout << c;
Filp1(second, second.size());
}
else if (c == "%")
{
Filp1(first, first.size());
cout << c;
}
else
Filp1(second, second.size());
cout << endl;
system("pause");
return 0;
}
注意
1 . 整数的最高位不能为0;
2 . 反转后的小数的整数部分的最高位不能为0,小数部分的末尾不能为0,除非小数部分都是0,则保留一个0;
3 . 反转后的分数的分子和分母不用约分,同样,分子和分母各自的最高位都不为0;
4 . 小数的输入存在这种测试用例:1020.0101000 ,输出为:201.101
5 . 反转后的百分数的整数部分的最高位不为0;
6 . 0反转后为0;
7 . 需要在输入字符串的时候,需要将‘.’、‘/’、‘%’提取出来,小数需要将整数部分和小数部分都分别提取出来,分数需要将分子和分母分别提取;
源代码:
#include <iostream>
#include <string.h>
#include <sstream>
#include <stdlib.h>
using namespace std;
void Filp1(const std::string str,int size)
{
string str1;
bool zeroornot = true;
for (size_t i = 0; i < size; i++)
{
if (str[i] != '0')
zeroornot = false;
}
if (zeroornot)
cout << "0";
else
{
bool judge = true;
for (int i = size - 1; i >= 0; i--)
str1 += str[i];
for (size_t i = 0; i < size; i++)
{
while (str1[i] == '0' && judge)
i++;
cout << str1[i];
judge = false;
}
}
}
void Filp2(const std::string str, int size)
{
string str1 = "";
bool zeroornot = true;
for (size_t i = 0; i < size; i++)
{
if (str[i] != '0')
zeroornot = false;
}
if (zeroornot)
cout << "0";
else
{
bool judge = true;
for (size_t i = 0; i < size; i++)
{
while (str[i] == '0' && judge)
i++;
str1 += str[i];
judge = false;
}
for (int i = str1.size()-1; i >= 0; i--)
cout << str1[i];
}
}
int main()
{
string input;
string first, second;
cin >> input;
string c;
int inputsize = input.size();
for (int i = 0; i < inputsize; i++)
{
if (input[i] == '0' || input[i] == '1' || input[i] == '2' || input[i] == '3' || input[i] == '4' ||
input[i] == '5' || input[i] == '6' || input[i] == '7' || input[i] == '8' || input[i] == '9')
second += input[i];
else
{
c = inpu
4000
t[i];
first = second;
second = "";
}
}
if (c == ".")
{
Filp1(first, first.size());
cout << c;
Filp2(second, second.size());
}
else if (c == "/")
{
Filp1(first, first.size());
cout << c;
Filp1(second, second.size());
}
else if (c == "%")
{
Filp1(first, first.size());
cout << c;
}
else
Filp1(second, second.size());
cout << endl;
system("pause");
return 0;
}
相关文章推荐
- <NOIP> 9 . P1307 数字反转
- <NOIP> 26 . P1478 陶陶摘苹果(升级版)
- <NOIP> 28 . P1579 哥德巴赫猜想(升级版)
- 数字反转(0)<P2011_1>
- <From C++ To C#>输入的不是数字怎么办
- seq<打印出一串有序的数字>
- 实现足够大List<int>剔除重复的数字
- 【NOIP2011普及组真题】数字反转
- hdu 1172 猜数字 解题报告<暴力枚举>
- <NOIP> 15 . P1427 小鱼的数字游戏
- P1553 数字反转(升级版)(#1 - 5)
- poj3279 反转 <挑战程序设计竞赛>
- 【数字图像处理】YUV420转RGB并BMP存储<纯C++实现>
- 数组中重复的数字:hash表-->unordered_map<>
- Django之第一个app<19>
- 汇编 计算数字字典>0 <0 =0的个数
- (noip 模拟 flower 种花)<贪心+后悔>
- <五>Ioc控制反转——为bean.xml瘦身第二步(附源码)
- 洛谷P1553 数字反转(升级版)
- <NOIP> 15 . P1428 小鱼比可爱