0
点赞
收藏
分享

微信扫一扫

P1553 数字反转(升级版)

爱薇Ivy趣闻 2022-02-03 阅读 27
c++

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

#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;
}	
#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;
}	
#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 条评论