清华大学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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值