• 数学--数论-- HDU6298 Maximum Multiple 打表找规律


    Given an integer nn, Chiaki would like to find three positive integers xx, yy and zzsuch that: n=x+y+zn=x+y+z, x∣nx∣n, y∣ny∣n, z∣nz∣n and xyzxyz is maximum. 
    

    Input

    There are multiple test cases. The first line of input contains an integer TT (1≤T≤1061≤T≤106), indicating the number of test cases. For each test case: 
    The first line contains an integer nn (1≤n≤1061≤n≤106). 
    

    Output

    For each test case, output an integer denoting the maximum xyzxyz. If there no such integers, output −11 instead. 
    

    Sample Input

    3
    1
    2
    3
    

    Sample Output

    -1
    -1
    1 
    

    只有因子中有4或者有3才能被拆成 X+Y+Z=N,然后打了表验证。
    最后wa了好几次,是因为int和int计算之后还是int就算赋值给long long .
    打表代码

    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
    	int T;
    	scanf("%d", &T);
    	while (T--)
    	{
    		int n;
    		for (int n = 1; n <= 100; n++)
    		{
    			int maxt = -1;
    			int a, b, c;
    			for (int x = 1; x <= n; x++)
    			{
    				for (int y = 1; y <= n - x; y++)
    				{
    					int z = n - x - y;
    					if (z && n % x == 0 && n % y == 0 && n % z == 0)
    					{
    						if (maxt < x * y * z)
    						{
    							a = x;
    							b = y;
    							c = z;
    						}
    						maxt = max(maxt, x * y * z);
    					}
    				}
    			}
    			printf("%d:%5d %d %d %d
    ", n, maxt, a, b, c);
    			if (n % 12 == 0)
    				printf("
    ");
    		}
    	}
    	return 0;
    }
    

    AC

    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
    	int T;
    	long long  n,x,y,z;
    	long long sum;
    	scanf("%d", &T);
    	while (T--)
    	{
    		scanf("%lld", &n);
    		if ((n % 3) == 0)
    		{
    			x = y = z = n / 3;
    			sum = x * y * z;
    			if (x + y + z == n)
    				printf("%lld
    ", sum);
    			else
    				puts("-1");
    		}
    		else if ((n % 4) == 0)
    		{
    			x = y = n / 4, z = n / 2;
    			sum = x * y * z;
    			if (x + y + z == n)
    				printf("%lld
    ", sum);
    			else
    				puts("-1");
    		}
    		else
    			puts("-1");
    	}
    }
    
  • 相关阅读:
    Python中的除法
    使用Windows Live Writer 2012和Office Word 2013 发布文章到博客园全面总结
    单例模式(Singleton)的6种实现
    Linux关机命令总结
    Java中构造函数执行顺序的问题
    SVN四部曲之SVN命令精通
    SVN四部曲之SVN简单使用教程入门
    SVN四部曲之SVN使用详解进阶
    用正则表达式判断一个二进制数是否能被3整除
    栈帧%ebp,%esp详解
  • 原文地址:https://www.cnblogs.com/lunatic-talent/p/12798459.html
Copyright © 2020-2023  润新知