HDOJ 1339 A Simple Task(简单数学题,暴力)

Problem Description
Given a positive integer n and the odd integer o and the nonnegative integer p such that n = o2^p.

Example

For n = 24, o = 3 and p = 3.

Task

Write a program which for each data set:

reads a positive integer n,

computes the odd integer o and the nonnegative integer p such that n = o2^p,

writes the result.

Input
The first line of the input contains exactly one positive integer d equal to the number of data sets, 1 <= d <= 10. The data sets follow.

Each data set consists of exactly one line containing exactly one integer n, 1 <= n <= 10^6.

Output
The output should consists of exactly d lines, one line for each data set.

Line i, 1 <= i <= d, corresponds to the i-th input and should contain two integers o and p separated by a single space such that n = o2^p.

Sample Input
1
24

Sample Output
3 3

思路:
就是一个公式: n = o*2^p.
n是输入的,o和p是我们需要求的。
需要注意的是o必须是奇数!
0<=p的。

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while(t-->0){
            int n = sc.nextInt();
            int a=0;
            int o=0;
            for(int p=0;p<n;p++){
                a=(int)Math.pow(2, p);
                if(a>n){
                    break;
                }
                if(n%a==0){
                    o=n/a;
                    if(o%2==0){
                        continue;
                    }
                    a=p;
                    break;
                }
            }
            System.out.println(o+" "+a);
        }

    }

}
时间: 2024-09-20 14:38:50

HDOJ 1339 A Simple Task(简单数学题,暴力)的相关文章

HDOJ 1326 Box of Bricks(简单题)

Problem Description Little Bob likes playing with his box of bricks. He puts the bricks one upon another and builds stacks of different height. Look, I've built a wall!'', he tells his older sister Alice.Nah, you should make all stacks the same heigh

HDOJ 1076 An Easy Task(闰年计算)

Problem Description Ignatius was born in a leap year, so he want to know when he could hold his birthday party. Can you tell him? Given a positive integers Y which indicate the start year, and a positive integer N, your task is to tell the Nth leap y

HDOJ(HDU) 1976 Software Version(简单判断)

Problem Description 相信大家一定有过在网上下载软件而碰到多个不同版本的情况. 一般来说,软件的版本号由三个部分组成,主版本号(Major Version Number),子版本号(Minor Version Number)和修订号(Revision_Number).当软件进行了重大的修改时,主版本号加一:当软件在原有基础上增加部分功能时,主版本号不变,子版本号加一:当软件仅仅修正了部分bug时,主版本号和子版本号都不变,修正号加一. 在我们比较软件的两个版本的新旧时,都是先比

HDOJ(HDU) 1994 利息计算(简单题目)

Problem Description 为自行解决学费,chx勤工俭学收入10000元以1年定期存入银行,年利率为3.7% .利率 按年计算,表示100元存1年的利息为3.7元.实际上有时提前有时推迟取,因此实际利息按天 计算,1年按365天计算,因此Q天的利息是 本金*3.7/100 *Q/365 存了100天后1年定期年利息提高到3.9%.如将存款提前全取出,再存1年定期.那么前面的 100天只能按活期利息1.7%计算. 100天的利息和本金:10000(1+1.7/100*100/365)

HDOJ/HDU 2203 亲和串(简单的判断~Java的indexOf()方法秒)

Problem Description 人随着岁数的增长是越大越聪明还是越大越笨,这是一个值得全世界科学家思考的问题,同样的问题Eddy也一直在思考,因为他在很小的时候就知道亲和串如何判断了,但是发现,现在长大了却不知道怎么去判断亲和串了,于是他只好又再一次来请教聪明且乐于助人的你来解决这个问题. 亲和串的定义是这样的:给定两个字符串s1和s2,如果能通过s1循环移位,使s2包含在s1中,那么我们就说s2 是s1的亲和串. Input 本题有多组测试数据,每组数据的第一行包含输入字符串s1,第二

.Net设计模式之简单工厂模式(Simple Factory Pattern)

一.简单工厂模式简介(Bref Introduction) 简单工厂模式(Simple Factory Pattern)的优点是,工厂类中包含了必要的逻辑判断,根据客户端的选择条件动态实例化相关的类,对于客户端来说,去除了与具体产品的依赖 二.解决的问题(What To Solve) 客户实例化对象时不需要关心该对象是由哪个子类实例化的. 三.简单工厂模式分析(Analysis) 1.简单工厂模式结构 IProduct接口:抽象产品类 ConcreteProduct类:产品类的具体实现 Simp

推荐一个简单、轻量、功能非常强大的C#/ASP.NET定时任务执行管理器组件–FluentScheduler

原文:推荐一个简单.轻量.功能非常强大的C#/ASP.NET定时任务执行管理器组件–FluentScheduler 在C#WINFORM或者是ASP.NET的WEB应用程序中,根据各种定时任务的需求,比如:每天的数据统计,每小时刷新系统缓存等等,这个时候我们得应用到定时器这个东东. .NET Framework有自带的timer,但这个类只能完成一些简单的定时操作,比如间隔多久执行什么操作.遇到一些复杂的定时任务,如从当前时间开始,多少时间后间隔重复执行,timer类处理起来就相对困难了.经过多

ZOJ 简单题集合

这部分题由于过于简单,属于白送题目,因此把所有特别简单题的合集于此. 1048:统计某人12个月的银行帐户余额的平均数.(简单的令人汗!) Code_1048/*ZOJ 1048: 统计小数的平均数!居然就是12个double求平均数..汗!*/ #include <stdio.h>double balance[12];int main() {  int i;  double sum=0,aver;  for(i=0;i<12;i++)  {   scanf("%lf"

hdu 1077 Catching Fish 计算几何+暴力枚举

   简单的暴力枚举,枚举两个点在圆上,用向量法求下圆心.复杂度o(n^3),但数据量只有300 /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please indicate the source** */ #include <iostream> #include <cstdio> #include <cstdlib> #include <c