zoukankan      html  css  js  c++  java
  • 2015 HUAS Summer Trainning #6~J

    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 
    解题思路:题目的意思是给你一个正整数n,把它进行质因数分解,输入包含多组案例,当输入一个负数的是时候表示不再输入,结束程序.首先判断2是不是n的质因数,当判断是质因数
    时候就要统计要他的个数,用count统计起来。然后从3开始循环到n,逐个判断是不是n的质因子,如果是就同理用count统计起来,但是循环的时候要注意i+=2,这个题目要注意最后
    一个输出也有空格。
    程序代码:
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    
    int main()
    {
        int n;
        int x=1,count;
        while(scanf("%d",&n)&&n>0)
        {
            if(x!=1)
                printf("
    ");
            count=0;
            printf("Case %d.
    ",x++);
            if(n%2==0)
            {
                printf("2 ");
                while(n%2==0)
                {
                    count++;
                    n=n/2;
    			}
    			printf("%d ",count);
            }
    		for(int i=3; i<=n; i+=2)
            {
                count=0;
                if(n%i==0)
                {
                    printf("%d ",i);
                    while(n%i==0)
                    {
                        count++;
                        n/=i;
                    }
                    printf("%d ",count);
    			}
    		}
            printf("
    ");
    	}
        return 0;
    }  
    
    
  • 相关阅读:
    拷贝构造函数的参数类型必须是引用
    [设计模式] 23 访问者模式 visitor Pattern
    [设计模式] 20 状态模式 State Pattern
    [设计模式] 19 观察者模式 Observer Pattern
    [设计模式] 18 备忘录模式Memento Pattern
    [设计模式] 17 中介者模式 Mediator Pattern
    [设计模式] 16 迭代器模式 Iterator Pattern
    [设计模式] 15 解释器模式 Interpreter
    [设计模式] 21 策略模式 Strategy
    图灵測试,測的究竟是什么?
  • 原文地址:https://www.cnblogs.com/chenchunhui/p/4743301.html
Copyright © 2011-2022 走看看