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;
}	
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值