zoukankan      html  css  js  c++  java
  • HDU 1405 The Last Practice

    原题:

    Description

    Tomorrow is contest day, Are you all ready? 
    We have been training for 45 days, and all guys must be tired.But , you are so lucky comparing with many excellent boys who have no chance to attend the Province-Final. 

    Now, your task is relaxing yourself and making the last practice. I guess that at least there are 2 problems which are easier than this problem. 
    what does this problem describe? 
    Give you a positive integer, please split it to some prime numbers, and you can got it through sample input and sample output. 
     

    Input

    Input file contains multiple test case, each case consists of a positive integer n(1<n<65536), one per line. a negative terminates the input, and it should not to be processed.
     

    Output

    For each test case you should output its factor as sample output (prime factor must come forth ascending ), there is a blank line between outputs.
     

    Sample Input

    60 12 -1
     

    Sample Output

    Case 1. 2 2 3 1 5 1 Case 2. 2 2 3 1

    Hint

     60=2^2*3^1*5^1 
     
     
     
    分析:
    这题是裸的质因数分解,有小伙伴觉得自己结果对,可是就是wrong answer   那就是这题的格式,最后一个数后也有空格。
     
     
    代码:
    #include <stdio.h>
    #include <string.h>
    #include<iostream>
    using namespace std;
    
    int prime[1000000];
    
    void sushu(void)
    {
        fill(prime, prime + 1000000, 1);
        int i, j;
        for (i = 2; i <= 500000; i++)
        {
            if (prime[i])
            {
                for (j = i + i; j<1000000; j += i)
                {
                    prime[j] = 0;
                }
            }
        }
    }
    
    int main()
    {
        int n, cas = 1, k = 0;
        sushu();
        while (~scanf("%d", &n) && n >= 0)
        {
            int cnt = 0, i;
            if (k)
                printf("
    ");
            printf("Case %d.
    ", cas++);
            k++;
            if (prime[n])
            {
                printf("%d 1 
    ", n);
                continue;
            }
            else
            {
                for (i = 2; i <= n; i += 1)
                {
                    cnt = 0;
                    if (prime[i] && n%i == 0)
                    {
                        while (n%i == 0)
                        {
                            n /= i;
                            cnt++;
                        }
                        if (cnt)
                        {
                            printf("%d %d ", i, cnt);
                        }
                    }
                }
                printf("
    ");
            }
        }
    
        return 0;
    }
  • 相关阅读:
    Mosquitto搭建Android推送服务(一)MQTT简介
    Quartz定时任务简单实例
    Oracle基础知识(一)、简介与安装
    [2013-08-01]window.open
    C#中DataTable与泛型集合互转(支持泛型集合中对象包含枚举)
    C#代码安装Windows服务(控制台应用集成Windows服务)
    Node+Socket实现聊天室
    web前端架构
    Laravel-admin form 表单是增加或者修改
    Laravel-admin 消息提醒、播放音频、点击跳转
  • 原文地址:https://www.cnblogs.com/shawn-ji/p/4748912.html
Copyright © 2011-2022 走看看