清華大學2010年複試上機題 解題報告

九度OJ 題目1083:特殊乘法
時間限制:1 秒  內存限制:32 兆  特殊判題:否  提交:335  解決:205
題目描述:
    寫個算法,對2個小於1000000000的輸入,求結果。
    特殊乘法舉例:123 * 45 = 1*4 +1*5 +2*4 +2*5 +3*4+3*5
輸入:
     兩個小於1000000000的數
輸出:
     輸入可能有多組數據,對於每一組數據,輸出Input中的兩個數按照題目要求的方法進行運算後得到的結果。
樣例輸入:
    123 45
樣例輸出:
    54
//清華2010:題目1083:特殊乘法
//對2個小於1000000000的輸入[即9位數] 求結果
//特殊乘法舉例 123*45=1*4+1*5+2*4+2*5+3*4+3*5
#include <iostream>
using namespace std;

int main(){
	int m, n, i, j;
	int a[9], b[9];
	int al, bl;
	while( cin >> m >> n ){
		for( i=0; m!=0; i++ ){
			a[i] = m % 10;
			m /= 10;
		}
		al = i;
		for( j=0; n!=0; j++ ){
			b[j] = n % 10;
			n /= 10;
		}
		bl = j;
		int num = 0;
		for( i=0; i<al; i++ )
			for( j=0; j<bl; j++ )
				num += a[i]*b[j];
		cout << num << endl;
	}
	//system("pause");
	return 0;
}


九度OJ 題目1084:整數拆分
時間限制:1 秒  內存限制:32 兆  特殊判題:否  提交:417  解決:118
題目描述:
    一個整數總可以拆分爲2的冪的和,例如:
    7=1+2+4
    7=1+2+2+2
    7=1+1+1+4
    7=1+1+1+2+2
    7=1+1+1+1+1+2
    7=1+1+1+1+1+1+1
    總共有六種不同的拆分方式。
    再比如:4可以拆分成:4 = 4,4 = 1 + 1 + 1 + 1,4 = 2 + 2,4=1+1+2。
    用f(n)表示n的不同拆分的種數,例如f(7)=6.
    要求編寫程序,讀入n(不超過1000000),輸出f(n)%1000000000。
輸入:
    每組輸入包括一個整數:N(1<=N<=1000000)。
輸出:
    對於每組數據,輸出f(n)%1000000000。
樣例輸入:
    7
樣例輸出:
    6
//清華2010:題目1084:整數拆分 
//c[n]=c[n-2]+c[n/2]
//int -> 2^31-1 = 2147483647
//unsigned int -> 2^32 -1 = 4294967295
//long long -> 2^63 -1 = 9223372036854775807
//unsigned __int64 -> 2^64 -1 = 18446744073709551615
#include <iostream>
using namespace std;
int c[1000002];

int main(){
	int n, i;
	c[0]=c[1]=1;
	c[2]=c[3]=2;

	for( i=2; i<=500000; i++ ){
		c[2*i] = (c[2*i-2] + c[i])%1000000000;
		c[2*i+1] = c[2*i];
	}

	while( cin >> n )
		cout << c[n] << endl;
	//system("pause");
	return 0;
}


九度OJ 題目1085:求root(N, k)
時間限制:1 秒  內存限制:32 兆  特殊判題:否  提交:237  解決:75
題目描述:
        N<k時,root(N,k) = N,否則,root(N,k) = root(N',k)。N'爲N的k進製表示的各位數字之和。輸入x,y,k,輸出root(x^y,k)的值 (這裏^爲乘方,不是異或),2=<k<=16,0<x,y<2000000000,有一半的測試點裏 x^y 會溢出int的範圍(>=2000000000)
輸入:
        每組測試數據包括一行,x(0<x<2000000000), y(0<y<2000000000), k(2<=k<=16)
輸出:
        輸入可能有多組數據,對於每一組數據,root(x^y, k)的值
樣例輸入:
    4 4 10
樣例輸出:
    4
//清華2010:題目1085:求root(N, k)
//已知0<x<2000000000, 0<y<2000000000, 2<=k<=16
//unsigned int 0~4294967295
#include <iostream>
#include <fstream>
#include <memory.h>
using namespace std;
static unsigned int SIGN = 4000000000;

int main(){
	
	int x, y, k;
	ifstream cin("THU_1085.txt");
	while( cin >> x >> y >> k ){
		int i, j, result=1;
		k--;
		if( x % k == 0 ){
			cout << k << endl;
			continue;
		}
		if( x > k )	//第一輪 削弱大輸入
			x = x % k;	//此時x最大爲15

		//cout << "x=" << x << " y=" << y << endl;//

		int m[32];	//2^32=4294967296>max(y)=2000000000
		memset( m, 0, sizeof(m) );

		//cout << "Too Big!\n";
		i = 0;
		bool con = 0;
		while( y > 1 ){
			if( y % 2 != 0 ){
				m[i] = x;
				i++;
			}
			y = y / 2;
			x = x * x;
			if( x % k == 0 ){
				cout << k << endl;
				con = 1;
				break;
			}
			if( x > k )
				x = x % k;
		}
		if( con == 1 )
			continue;
		for( j=0; j<i; j++ ){
			result = ( result * m[j] ) % k;	// % 100
			//cout << "m[" << j << "]=" << m[j] << " ";//
			//if( j==i-1 ) cout << endl;//
		}
		result = ( result * x ) % k;
		
		cout << result << endl; //"result=" << 
		//system("pause");
	}
	system("pause");
	return 0;
}
發佈了43 篇原創文章 · 獲贊 4 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章