1. 编程实现大数乘法(十进制128位以上,乘数与被乘数均不超过1024位)
[Accepted 2013/03/21]
#include <stdio.h>
#include <string.h>
#define MAX_LEN 1024
void print(char data[]) {
int i = 0;
for(i = 0; i < 2 * MAX_LEN; i++) {
if(data[i] != 0) break;
}
printf("The product of number1 and number2 is:\n");
if(i == 2 * MAX_LEN) printf("0");
while(i < 2 * MAX_LEN) {
printf("%d", data[i]);
i++;
}
}
int main() {
int i = 0;
int j = 0;
int k = 0;
int l = 0;
int len1 = 0;
int len2 = 0;
int temp = 0;
int carry = 0;
int product = 0;
char data1[MAX_LEN + 1] = {0};
char data2[MAX_LEN + 1] = {0};
char data3[2*MAX_LEN + 1] = {0};
printf("please input big number1==> ");
scanf("%s", data1);
printf("please input big number2==> ");
scanf("%s", data2);
len1 = strlen(data1);
len2 = strlen(data2);
for(i = len1 - 1, l = 2*MAX_LEN - 1; i >= 0; i--, l--) {
k = l;
carry = 0;
for(j = len2 - 1; j >= 0; j--) {
product = (data1[i] - '0') * (data2[j] - '0');
temp = ((int)data3[k]) + product % 10 + carry;
data3[k] = temp % 10;
carry = product / 10 + temp / 10;
k--;
}
data3[k] += carry;
}
print(data3);
return 0;
}
Well done!
第一个完全独立实现,并且经过反复调试的,具有一定难度的编程题,(*^__^*) 嘻嘻……
2. 现有一个集合 A = {苹果, 香蕉, 梨, 桃子}, 请编程输出集合A 的所有排列
[Accepted 2013/03/25]
void permutation(int data[], int start, int end) {
int i = 0;
int temp = 0;
if(start == end) {
for(i = 0; i <= end; i++) printf("%d ",data[i]); //递归到只剩1个数,直接输出序列
printf("\n");
return;
}
else {
for(i = start; i <= end; i++) { //依次选取一个数打头,对余下的数做全排列
temp = data[start];
data[start] = data[i];
data[i] = temp; //交换,依次选取一个数打头
permutation(data,start+1,end); //递归调用对余下的数做全排列
temp = data[start];
data[start] = data[i];
data[i] = temp; //为保持原来的顺序 将调换的数交换回来
}
}
}