時間限制:3 秒 內存限制:128 兆 特殊判題:否 提交:1266 解決:351
題目描述:
輸入一個正整數N,輸出N的階乘。
輸入:
正整數N(0<=N<=1000)
輸出:
輸入可能包括多組數據,對於每一組輸入數據,輸出N的階乘
樣例輸入:
4
5
15
樣例輸出:
24
120
1307674368000
//清華2006:題目1076:N的階乘
//輸出N的階乘(0<=N<=1000) 即大數乘法
#include <fstream>
#include <iostream>
#include <memory.h>
using namespace std;
int a[100000];
int length; //a數組的實際佔用長度
void mul(int *a, int b ){ // b 爲乘數
int i, tmp, carry=0;
for( i=0; i<length; i++ ){
tmp = a[i] * b + carry;
a[i] = tmp % 10000; //每個數組max=9999 運算中實際max=9999000 carry=999
carry = tmp / 10000;
}
if( carry!=0 ){
a[length] = carry;
length++;
}
}
int main(){
int i, j, k, n;
//ifstream cin("THU_1076.txt");
while( cin >> n ){
memset(a,0,sizeof(a));
a[0] = 1;
length = 1;
for( i=2; i<=n; i++ ){
mul( a, i );
//逐級輸出 調試用
//for( j=length-1; j>=0; j-- ){
// if( j!=length-1 ){
// if( a[j]<10 )
// cout << "000";
// else if( a[j]<100 )
// cout << "00";
// else if( a[j]<1000 )
// cout << "0";
// }
// cout << a[j];
//}
//cout << endl;
}
//ofstream out("THU_1076_output.txt");
for( j=length-1; j>=0; j-- ){
if( j!=length-1 ){
if( a[j]<10 )
cout << "000";
else if( a[j]<100 )
cout << "00";
else if( a[j]<1000 )
cout << "0";
}// if
cout << a[j];
}// for
cout << endl;
}
return 0;
}
九度OJ 題目1077:最大序列和
時間限制:1 秒 內存限制:32 兆 特殊判題:否 提交:553 解決:137
題目描述:
給出一個整數序列S,其中有N個數,定義其中一個非空連續子序列T中所有數的和爲T的“序列和”。
對於S的所有非空連續子序列T,求最大的序列和。
變量條件:N爲正整數,N≤1000000,結果序列和在範圍(-2^63,2^63-1)以內。
輸入:
第一行爲一個正整數N,第二行爲N個整數,表示序列中的數。
輸出:
輸入可能包括多組數據,對於每一組輸入數據,
僅輸出一個數,表示最大序列和。
樣例輸入:
5
1 5 -3 2 4
6
1 -2 3 4 -10 6
4
-3 -1 -2 -5
樣例輸出:
9
7
-1
各種情況都要考慮到啊 包括單個輸入數據可能是大數 超出int範圍
//清華2006:題目1077:最大序列和
//求給定序列的最大非空連續子序列和
//N爲正整數,N≤1000000,結果序列和在範圍(-2^63,2^63-1)以內。
#include <fstream>
#include <iostream>
#define INT long long
using namespace std;
int main(){
int i, n;
INT temp, sum, b;
ifstream cin("THU_1077.txt");
while( cin >> n ){
for( i=0; i<n; i++ ){
cin >> b;
if( i == 0 ){
sum = b;
temp = 0;
}
if( temp > 0 )
temp += b;
else
temp = b;
if( temp > sum )
sum = temp;
}// for
cout << sum << endl;
}
system("pause");
return 0;
}
九度OJ 題目1078:二叉樹遍歷
時間限制:1 秒 內存限制:32 兆 特殊判題:否 提交:159 解決:109
題目描述:
二叉樹的前序、中序、後序遍歷的定義:
前序遍歷:對任一子樹,先訪問跟,然後遍歷其左子樹,最後遍歷其右子樹;
中序遍歷:對任一子樹,先遍歷其左子樹,然後訪問根,最後遍歷其右子樹;
後序遍歷:對任一子樹,先遍歷其左子樹,然後遍歷其右子樹,最後訪問根。
給定一棵二叉樹的前序遍歷和中序遍歷,求其後序遍歷(提示:給定前序遍歷與中序遍歷能夠唯一確定後序遍歷)。
輸入:
兩個字符串,其長度n均小於等於26。
第一行爲前序遍歷,第二行爲中序遍歷。
二叉樹中的結點名稱以大寫字母表示:A,B,C....最多26個結點。
輸出:
輸入樣例可能有多組,對於每組測試樣例,
輸出一行,爲後序遍歷的字符串。
樣例輸入:
ABC
BAC
FDXEAG
XDEFAG
樣例輸出:
BCA
XEDGAF
因爲二叉樹遍歷幾乎不會 所以參考了別人的程序 以後有時間寫個自己的非遞歸版吧
//清華2006:題目1078:二叉樹遍歷
//兩個字符串,其長度n均小於等於26。 第一行爲前序遍歷,第二行爲中序遍歷。
//二叉樹中的結點名稱以大寫字母表示:A,B,C....最多26個結點。
//輸出後序遍歷的字符串。
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
struct BiTree{
char a;
BiTree* l;
BiTree* r;
};
BiTree* process( string S, string T ){
if( S.empty() )
return NULL;
int mark = T.find(S[0]); //t[S[0]-64];
BiTree *b = new BiTree(); //b=binary tree
b->a = S[0];
b->l = process( S.substr(1,mark), T.substr(0,mark) );
b->r = process( S.substr(mark+1,S.size()-mark-1),
T.substr(mark+1,T.size()-mark-1) );
return b;
};
void postTraverse( BiTree* b ){
if( b != NULL ){
if( b->l != NULL) postTraverse(b->l);
if( b->r != NULL) postTraverse(b->r);
cout << b->a;
}
};
int main(){
int i, n;
string S, T; //preorder inorder postorder
ifstream cin("THU_1078.txt");
while( cin >> S >> T ){
BiTree *B = process( S, T );
postTraverse( B );
cout << endl;
}
system("pause");
return 0;
}