Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

略吭

Posted by KatrineYang at 2016-09-22 05:40:07 on Problem 1396
782ms低空飘过,开始写了个高精度类TLE了,祘法一點没改就去掉类就过了
还搞了一次PE,注意最后的结果行的宽度不一定是最大的!反例: 0*20
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

bool isDigit(char c){
	return c>='0' && c<='9';
}

int mx(int a, int b){
	return (a>b) ? a : b;
}

int mx(int a, int b, int c){
	return mx(mx(a, b), c);
}

int ansM[505][505];
int ansWs[505];

int main() {
	int t;
	scanf("%d", &t);
	char str[2333];
	int ans[2333];
	for(int ii = 0; ii < t; ii++){
		int dd;
		char opr;
		scanf("%s", str);
		int len = strlen(str);
		for(int i = 0; i < len; i++){
			if(!isDigit(str[i])){
				dd = i;
				opr = str[i];
				break;
			}
		}
		int len1 = dd, len2 = len-dd-1;
		char *start1 = str+dd-1, *start2 = str+len-1;
		switch(opr){
			case '+':
			{
				int ws = mx(len1, len2);
				int carry = 0;
				for(int i = 0; i < ws; i++){
					ans[i] = 0;
					if(i<len1) ans[i] += (*(start1-i) - '0');
					if(i<len2) ans[i] += (*(start2-i) - '0');
					ans[i] += carry;
					if(ans[i] > 9) {
						ans[i] -= 10;
						carry = 1;
					}
					else carry = 0;
				}
				if(carry){
					ans[ws] = 1;
					ws++;
				}
				int zws = mx(ws, len2+1);
				for(int i = 0; i < zws-len1; i++) printf(" ");
				for(int i = len1-1; i >= 0; i--) printf("%c", *(start1-i));
				printf("\n");
				for(int i = 0; i < zws-len2-1; i++) printf(" ");
				printf("%c", opr);
				for(int i = len2-1; i >= 0; i--) printf("%c", *(start2-i));
				printf("\n");
				for(int i = 0; i < zws; i++) printf("-");
				printf("\n");
				for(int i = 0; i < zws-ws; i++) printf(" ");
				for(int i = ws-1; i >= 0; i--) printf("%d", ans[i]);
				printf("\n\n");
				break;
			}
			case '-':
			{
				int ws = mx(len1, len2);
				int carry = 0;
				for(int i = 0; i < ws; i++){
					ans[i] = 0;
					if(i<len1) ans[i] += (*(start1-i) - '0');
					if(i<len2) ans[i] -= (*(start2-i) - '0');
					ans[i] -= carry;
					if(ans[i] < 0) {
						ans[i] += 10;
						carry = 1;
					}
					else carry = 0;
				}
				int I;
				for(I = ws-1; I >= 0; I--){
					if(ans[I]) break;
				}
				ws = I+1;
				if(!ws) ws = 1;
				int zws = mx(len1, ws, len2+1);
				int xc = mx(len2+1, ws);
				for(int i = 0; i < zws-len1; i++) printf(" ");
				for(int i = len1-1; i >= 0; i--) printf("%c", *(start1-i));
				printf("\n");
				for(int i = 0; i < zws-len2-1; i++) printf(" ");
				printf("%c", opr);
				for(int i = len2-1; i >= 0; i--) printf("%c", *(start2-i));
				printf("\n");
				for(int i = 0; i < zws-xc; i++) printf(" ");
				for(int i = 0; i < xc; i++) printf("-");
				printf("\n");
				for(int i = 0; i < zws-ws; i++) printf(" ");
				for(int i = ws-1; i >= 0; i--) printf("%d", ans[i]);
				printf("\n\n");
				break;
			}
			case '*':
			{
				int ws = mx(len1, len2), zws;
				if(len2==1){
					int mult = *(start2)-'0';
					if(mult == 0){
						zws = mx(len1, 2);
						for(int i = 0; i < zws-len1; i++) printf(" ");
						for(int i = len1-1; i >= 0; i--) printf("%c", *(start1-i));
						printf("\n");
						for(int i = 0; i < zws-2; i++) printf(" ");
						printf("%c", opr);
						for(int i = len2-1; i >= 0; i--) printf("%c", *(start2-i));
						printf("\n");
						for(int i = 0; i < zws; i++) printf("-");
						printf("\n");
						for(int i = 0; i < zws-1; i++) printf(" ");
						printf("0\n\n");
					}
					else{
						int carry = 0;
						for(int i = 0; i < len1; i++){
							ans[i] = carry + mult * (*(start1-i)-'0');
							carry = ans[i]/10;
							ans[i]%=10;
						}
						if(carry){
							ans[ws] = carry;
							ws++;
						}
						zws = mx(ws, 2);
						for(int i = 0; i < zws-len1; i++) printf(" ");
						for(int i = len1-1; i >= 0; i--) printf("%c", *(start1-i));
						printf("\n");
						for(int i = 0; i < zws-2; i++) printf(" ");
						printf("%c", opr);
						for(int i = len2-1; i >= 0; i--) printf("%c", *(start2-i));
						printf("\n");
						for(int i = 0; i < zws; i++) printf("-");
						printf("\n");
						for(int i = 0; i < zws-ws; i++) printf(" ");
						for(int i = ws-1; i >= 0; i--) printf("%d", ans[i]);
						printf("\n\n");
					}
				}
				else{
					for(int i = 0; i < len2; i++){
						int mult = *(start2-i)-'0';
						if(!mult){
							ansM[i][0] = 0;
							ansWs[i] = 1;
						}
						else{
							ansWs[i] = len1;
							int carry = 0;
							for(int j = 0; j < len1; j++){
								ansM[i][j] = carry + mult * (*(start1-j)-'0');
								carry = ansM[i][j]/10;
								ansM[i][j]%=10;
							}
							if(carry){
								ansM[i][ansWs[i]] = carry;
								ansWs[i]++;
							}
						}
					}
					zws = len1+len2;
					int carry = 0;
					for(int i = 0; i < zws; i++){
						ans[i] = carry;
						for(int k = 0; k <= i && k < len2; k++){
							if(i-k<ansWs[k]) ans[i] += ansM[k][i-k];
						}
						//cout << ans[i] << endl;
						carry = ans[i]/10;
						ans[i]%=10;
					}
					while(zws > 0 && !ans[zws-1]) zws--;
					if(!zws) zws++;
					int zwss = mx(zws, len1, len2+1);
					//cout << zws << endl;
					for(int i = 0; i < zwss-len1; i++) printf(" ");
					for(int i = len1-1; i >= 0; i--) printf("%c", *(start1-i));
					printf("\n");
					for(int i = 0; i < zwss-len2-1; i++) printf(" ");
					printf("%c", opr);
					for(int i = len2-1; i >= 0; i--) printf("%c", *(start2-i));
					printf("\n");
					int xc = mx(ansWs[0], len2+1);
					for(int i = 0; i < zwss-xc; i++) printf(" ");
					for(int i = 0; i < xc; i++) printf("-");
					printf("\n");
					for(int i = 0; i < len2; i++){
						for(int j = 0; j < zwss-i-ansWs[i]; j++) printf(" ");
						for(int j = ansWs[i]-1; j >= 0; j--) printf("%d", ansM[i][j]);
						printf("\n");
					}
					int xh = mx(zws, ansWs[len2-1] + len2-1);
					for(int i = 0; i < zwss-xh; i++) printf(" ");
					for(int i = 0; i < xh; i++) printf("-");
					printf("\n");
					for(int i = 0; i < zwss-zws; i++) printf(" ");
					for(int i = zws-1; i >= 0; i--) printf("%d", ans[i]);
					printf("\n\n");
				}
				break;
			}
			default:
				break;
		}
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator