⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 北大oj 1454 c++源代码.cc

📁 北大OJ上的题目
💻 CC
字号:
#define MAXSIZE 800
#include<stdio.h>
#include<string.h>

void strturn(char str[MAXSIZE])
{
	int i = 0, len = strlen(str);
	char temp;
	while (i < len){
		temp = str[i];
		str[i] = str[len - 1];
		str[len - 1] = temp;
		len --;
		i ++;
	}
}

char* Add(char s1[MAXSIZE], char s2[MAXSIZE])
{
	char result[MAXSIZE], str1[MAXSIZE], str2[MAXSIZE];
	int len1, len2, max, i, sum1, carry;
	strcpy(str1, s1);
	strcpy(str2, s2);
	strturn(str1);
	strturn(str2);
	len1 = strlen(str1);
	max = len2 = strlen(str2);
	if(len1 > max) {
		max = len1;
		for(i = len2; i < max; i ++) {
			str2[i] = '0';
		}
		str2[i] = '\0';
	} else {
		for(i = len1; i < max; i ++) {
			str1[i] = '0';
		}
		str1[i] = '\0';
	}
	for(i = 0, carry = 0; i < max; i ++) {
		sum1 = str1[i] - '0' + str2[i] - '0' + carry;
		carry = 0;
		if(sum1 >= 10) {
			sum1 -= 10;
			carry = 1;
		}
		result[i] = sum1 + '0';
	}
	if(carry == 1) {
		result[max] = '1';
		result[max + 1] = '\0';
	} else {
		result[max] = '\0';
	}
	strturn(result);
	return result;
}

char *Multiply(char s1[MAXSIZE], char s2[MAXSIZE])
{	
	char str1[MAXSIZE], str2[MAXSIZE], str3[MAXSIZE * 2], temp[MAXSIZE * 2];
	int i, j, k, len1, len2, number, carry;
	strcpy(str1, s1);
	strcpy(str2, s2);
	strturn(str1);
	strturn(str2);
	str3[0] = '0';
	str3[1] = '\0';
	len1 = strlen(str1);
	len2 = strlen(str2);
	for(i = 0; i < len2; i ++) {
		for(k = 0; k < i; k ++ ) {
			temp[k] = '0';
		}
		for(j = 0, carry = 0; j < len1; j ++) {
			number = (str1[j] - '0') * (str2[i] - '0') + carry;
			carry = number / 10;
			number %= 10;
			temp[j + i] = number + '0';
		}
		if(carry != 0) {
			temp[j + i] = '0' + carry;
			temp[j + 1 + i] = '\0';
		} else {
			temp[j + i] = '\0';
		}
		strturn(temp);
		strcpy(str3, Add(str3, temp));
	}
	for(i = 0, strcpy(temp, "0\0"); i < strlen(str3); i ++) {
		if(str3[i] != '0') {
			strcpy(temp, &str3[i]);
			break;
		}
	}
	return temp;
}


int main(void)
{
	char data[367][MAXSIZE], temp[MAXSIZE];
	int i, times, k, n, result[10], len;
	for(i = 1, strcpy(data[0], "1\0"); i <= 366; i ++) {
		times = i;
		k = 0;
		while(times != 0) {
			temp[k] = char(times % 10 + '0'); 
			times /= 10;
			k ++;
		}
		temp[k] = '\0';
		strturn(temp);
		strcpy(data[i], Multiply(temp, data[i - 1]));		
	}
	while(1) {
		scanf("%d", &n);
		if(n == 0) {
			break;
		}
		len = strlen(data[n]);
		memset(result, 0, sizeof(result));
		for(i = 0; i < len; i ++) {
			result[data[n][i] - '0'] ++;
		}
		printf("%d! --\n", n);
		for(i = 0; i < 10; i ++) {
			if(i == 0 || i == 5) {
				printf("   (%d)%5d", i, result[i]);
			} else {
				printf("    (%d)%5d", i, result[i]);
			}
			if(i == 4) {
				printf("\n");
			}
		}
		printf("\n");
	}
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -