HDU ACM 1041Computer Transformation(大数模拟+找规律)

分析:大数模拟和找规律。

#include<iostream>
using namespace std;

char f[1001][501];

/*
从下面的步骤中可以看出,下一步的后半部分是前一步的整个串,所以对于后半部分有f[n]=f[n-1];
从前半部分和整体可以看出,另一部分有f[n]=f[n-1]-1(奇数步),f[n]=f[n-1]+1(偶数步)。
step0:1                       f[0]=0
step1:01                      f[1]=0
step2:10 01                   f[2]=1 =2*f[1]+1
step3:0110 1001               f[3]=1 =2*f[2]-1
step4:10010110 01101001       f[4]=3 =2*f[3]+1
step5:0110100110010110 1001011001101001    f[5]=5 =2*f[4]-1
step6:10010110011010010110100110010110 01101001100101101001011001101001  f[6]=11 =2*f[5]+1
最终有f[n]=2*f[n-1]+1(n为偶数),f[n]=2*f[n-1]-1(n为奇数)
  */

void createtable()
{
	int i,j;
	int c,fa,sum;

	memset(f,0,sizeof(f));

	for(i=2;i<=1000;i++)
	{
		if(i%2==0)
		{
			for(j=500,c=0;j>=0;j--)
			{
				fa=f[i-1][j]*2+c;
				f[i][j]=fa%10;
				c=fa/10;
			}
			j=500;
			sum=f[i][j]+1;
			f[i][j]=sum%10;
			c=sum/10;
			while(c)
			{
				sum=f[i][j-1]+c;
				f[i][j-1]=sum%10;
				c=sum/10;
				j--;
			}
		}
		else
		{
			for(j=500,c=0;j>=0;j--)
			{
				fa=f[i-1][j]*2+c;
				f[i][j]=fa%10;
				c=fa/10;
			}
			j=500;
			sum=f[i][j]+-1;
			if(sum==-1)
			{
				f[i][j]=9;
				c=-1;
			}
			else
			{
				f[i][j]=sum;
				c=0;
			}
			while(c==-1)
			{
				sum=f[i][j-1]+c;
				if(sum==-1)
				{
			    	f[i][j]=9;
		     		c=-1;
				}
		    	else
				{
		    		f[i][j]=sum;
		    		c=0;
				}
				j++;
			}
		}
	}
}

int main()
{
	int n,j;

	createtable();
	while(cin>>n)
	{
		if(n==1)
			cout<<"0"<<endl;
		else
		{
			j=0;
			while(f[n][j]==0) j++;
			for(;j<=500;j++)
				putchar(f[n][j]+'0');
			putchar('\n');
		}
	}
    return 0;
}
时间: 2024-10-11 21:53:04

HDU ACM 1041Computer Transformation(大数模拟+找规律)的相关文章

HDU 4588 Count The Carries(找规律,模拟)

题目 大意: 求二进制的a加到b的进位数. 思路: 列出前几个2进制,找规律模拟. #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <math.h> #include <stack> #include <vector> using namespace std; int main() { int

hdu4952 Number Transformation(数学题 | 找规律)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4952 Number Transformation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 261    Accepted Submission(s): 117 Problem Description Teacher Mai has

HDU 6154 CaoHaha&#39;s staff 思维 找规律

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6154 题目描述: 围成一个面积不小于S的多边形, 最少需要多少根儿线段, 线段可以为单元格边或者对角线 解题思路: 最大的面积肯定是由根号2为边长的正方形围成了, 那么我们把所有正方形都遍历一遍, 找出S介于N, N+1的那个上界N+1设为max, 因为MAX所围成的多边形面积和MAX-1, MAX-2, MAX-3围成的多边形面积, 找出满足条件的最小的一个即可 代码: #include <io

HDU 4349 Xiao Ming&#39;s Hope 找规律

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4349 Xiao Ming's Hope Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1723    Accepted Submission(s): 1144 Problem Description Xiao Ming likes coun

hdu 2147 kiki&#39;s game(找规律)

kiki's game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 40000/10000 K (Java/Others)Total Submission(s): 10656    Accepted Submission(s): 6455 Problem Description Recently kiki has nothing to do. While she is bored, an idea appears in his

HDU 1564 Play a game (博弈&amp;&amp;找规律)

Play a game Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1430    Accepted Submission(s): 1168 Problem Description New Year is Coming! ailyanlu is very happy today! and he is playing a chessbo

HDU 5351 MZL&#39;s Border(找规律+高精度)

MZL's Border Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 895    Accepted Submission(s): 287 Problem Description As is known to all, MZL is an extraordinarily lovely girl. One day, MZL was p

HDU 1517 A Multiplication Game (博弈&amp;&amp;找规律)

A Multiplication Game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3691    Accepted Submission(s): 2097 Problem Description Stan and Ollie play the game of multiplication by multiplying an in

题解报告:hdu 1564 Play a game(找规律博弈)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1564 Problem Description New Year is Coming! ailyanlu is very happy today! and he is playing a chessboard game with 8600.The size of the chessboard is n*n. A stone is placed in a corner square. They play