P1553 数字反转(升级版)
作者:互联网
#include<bits/stdc++.h>
using namespace std;
char fuhao[10];
int number1(int n) {
int number = n;
int temp = 0;
while (number!=0){
temp = temp * 10 + number % 10;
number /= 10;
}
return temp;
}
int main() {
string str;
int number=0, number_1=0, number_2 = 0;
int temp = 0, temp1 = 0, temp2 = 0;
int ans = 0;
cin >> str;
int len = str.length();
for (int i = 0; i < len; i++) {
if (str[i] == '.') {
fuhao[0]++;
temp1 = fuhao[0];
temp = i;
}
if (str[i] == '/') {
fuhao[1]++;
temp2 = fuhao[1];
temp = i;
}
}
if (temp1 == 0 && temp2 == 0 && str[len-1] != '%') {
for (int i = 0; i < len; i++) {
number = number * 10 + (str[i]-48);
}
ans = number1(number);
cout << ans << endl;
}
if (str[len-1] == '%') {
for (int i = 0; i < len - 1; i++) {
number = number * 10 + (str[i] - 48);
}
ans = number1(number);
cout << ans << '%' << endl;
}
if (temp1 == 1) {
for (int i = 0; i < temp; i++) {
number_1 = number_1 * 10 + (str[i] - 48);
}
for (int i = temp + 1; i < len; i++) {
number_2 = number_2 * 10 + (str[i] - 48);
}
cout << number1(number_1) << '.' << number1(number_2) << endl;
}
if (temp2 == 1) {
for (int i = 0; i < temp; i++) {
number_1 = number_1 * 10 + (str[i] - 48);
}
for (int i = temp + 1; i < len; i++) {
number_2 = number_2 * 10 + (str[i] - 48);
}
cout << number1(number_1) << '/' << number1(number_2) << endl;
}
system("pause");
return 0;
}
第一种是直接瞎写的 只有75分 出错原因好像是是int长度不够
#include<bits/stdc++.h>
using namespace std;
char fuhao[10];
char str[25];
int main() {
int number=0, number_1=0, number_2 = 0;
int temp = 0, temp1 = 0, temp2 = 0;
int ans = 0;
cin >> str;
int len = strlen(str);
for (int i = 0; i < len; i++) {
if (str[i] == '.') {
fuhao[0]++;
temp1 = fuhao[0];
temp = i;
}
if (str[i] == '/') {
fuhao[1]++;
temp2 = fuhao[1];
temp = i;
}
}
if (temp1 == 0 && temp2 == 0 && str[len-1] != '%') {
for (int i = len - 1; i >= 0; i--) {
cout << str[i];
}
cout << endl;
}
if (str[len-1] == '%') {
for (int i = len-2; i >=0; i--) {
cout << str[i];
}
cout << '%' << endl;
}
if (temp1 == 1) {
for (int i = temp-1; i >=0; i--) {
cout << str[i];
}
cout << '.';
for (int i = len-1; i >temp; i--) {
cout << str[i];
}
cout << endl;
}
if (temp2 == 1) {
for (int i = temp - 1; i >= 0; i--) {
cout << str[i];
}
cout << '/';
for (int i = len - 1; i > temp; i--) {
cout << str[i];
}
cout << endl;
}
system("pause");
return 0;
}
所以在第二次=尝试中把所有数据存入数组再进行输出 是没有问题了 但是又出现了新的问题 只有50分了 看了下数据 是前导零这个东西没有注意到 所以又要进行第三次修改 期间也有修修改改 比如判断是否只有一个0的情况 然后小数和分数也有不一样
#include<bits/stdc++.h>
using namespace std;
char fuhao[10];
char str[25];
int main() {
int number=0, number_1=0, number_2 = 0;
int temp = 0, temp1 = 0, temp2 = 0,TEMP=0;
int ans = 0;
cin >> str;
int len = strlen(str);
for (int i = 0; i < len; i++) {
if (str[i] == '.') {
fuhao[0]++;
temp1 = fuhao[0];
temp = i;
}
if (str[i] == '/') {
fuhao[1]++;
temp2 = fuhao[1];
temp = i;
}
}
if (temp1 == 0 && temp2 == 0 && str[len-1] != '%') {
while (str[len - 1] == '0' && len > 1)
len--;
for (int i = len - 1; i >= 0; i--) {
cout << str[i];
}
cout << endl;
}
if (str[len-1] == '%') {
while (str[len - 2] == '0' && len > 2)
len--;
for (int i = len-2; i >=0; i--) {
cout << str[i];
}
cout << '%' << endl;
}
if (temp1 == 1) {
TEMP = temp;
while (str[temp - 1] == '0' && temp > 1)
temp--;
for (int i = temp - 1; i >= 0; i--) {
cout << str[i];
}
cout << '.';
if (TEMP != temp) {
while (str[TEMP + 1] == '0' && (len-TEMP) > 2)
TEMP++;
for (int i = len - 1; i > TEMP; i--) {
cout << str[i];
}
}
else {
while (str[TEMP + 1] == '0' && (len-TEMP) > 2)
TEMP++;
for (int i = len - 1; i > TEMP; i--) {
cout << str[i];
}
}
cout << endl;
}
if (temp2 == 1) {
TEMP = temp;
while (str[temp - 1] == '0' && temp > 1)
temp--;
for (int i = temp - 1; i >= 0; i--) {
cout << str[i];
}
cout << '/';
if (TEMP != temp) {
while (str[TEMP + 1] == '0' && (len-TEMP) > 2)
TEMP++;
while (str[len - 1] == '0' && (len - TEMP) > 2)
len--;
for (int i = len - 1; i > TEMP; i--) {
cout << str[i];
}
}
else {
while (str[TEMP + 1] == '0' && (len-TEMP) > 2)
TEMP++;
while (str[len - 1] == '0' && (len - TEMP) > 2)
len--;
for (int i = len - 1; i > TEMP; i--) {
cout << str[i];
}
}
cout << endl;
}
system("pause");
return 0;
}
标签:TEMP,temp,int,反转,number,len,P1553,str,升级版 来源: https://blog.csdn.net/kaoyan010705/article/details/122729609