SGU 107: 987654321 problem

Jasonlin posted @ 2011年3月31日 16:29 in SGU with tags 规律 打表 , 2398 阅读

题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=107

题目大意:

      求出多少个N位数的平方末尾是987654321。

解题思路:

      因为末尾多少是跟后几位相乘有关的,所以先试探一些值,9位以下都没有解,9位有8个,10位有8*9个,递推下去11位以上就是8*9*(n-10)个。因为n的范围是10^6,所以用公式不能算,会溢出,这里用了一个小技巧,因为后面都是0,所以算10位后面就直接输出输出72然后加上(n-10)个0。

解题代码:

#include<iostream>
using namespace std;

int main(){
	int n;
	cin>>n;
	if(n<9)
		cout<<0<<endl;
	else if(n==9)
		cout<<8<<endl;
	else if(n==10)
		cout<<72<<endl;
	else{
		cout<<72;
		for(int i=1;i<=n-10;i++)
			cout<<0;
		cout<<endl;
	}
	return 0;
}

扩展知识:http://blog.csdn.net/ZERO2046/archive/2007/05/20/1618507.aspx

  • 无匹配

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter