十二周——杭电——1004The Last Practice

问题及代码:

The Last Practice

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 56   Accepted Submission(s) : 16
Font: Times New Roman | Verdana | Georgia
Font Size: ← →

Problem 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
<div style='font-family:Times New Roman;font-size:14px;background-color:F4FBFF;border:#B7CBFF 1px dashed;padding:6px'><div style='font-family:Arial;font-weight:bold;color:#7CA9ED;border-bottom:#B7CBFF 1px dashed'><i>Hint</i></div>
60=2^2*3^1*5^1
</div>
#include <iostream>
#include <cstdio>
#define MAX 65536//定义宏,方便后面处理
using namespace std;
int prime_list[MAX];//定义质数表,
int kprime=0;//统计质数的个数
void printprime()
{
    int i,j;
    for(i=2; i<MAX; i++)
    {
        for(j=0; j<kprime; j++)
        {
            if(i%prime_list[j]==0)
                break;
        }
        if(j==kprime)
            prime_list[kprime++]=i;
    }
}
int main()
{
    int n;
    int count=1;
    bool first=true;
    printprime();
    while(cin>>n&&n>=0)
    {
        if(!first)
            cout<<endl;
        else
            first=false;
        cout<<"Case "<<count++<<"."<<endl;
        for(int i=0; i<kprime; i++)
        {
            int count=0;
            while(n%prime_list[i]==0)
            {
                count++;
                n/=prime_list[i];
            }
            if(count)
                cout<<prime_list[i]<<" "<<count<<" ";
        }
        cout<<endl;
    }
    return 0;
}


运行结果:

资料总结:

关于#define 的用法

有的时候为了程序的通用性,可以使用#define预处理宏定义命令,它的具体作用,就是方便程序段的定义和修改。
举个例子吧,如果说你在做一个项目,这个项目的代码很长,有5000多行,而且基本上裏面的同一个常量N就占了3000多行,如果说今后你要是想对这个常量N的值进行修改,如果在程序的开始没有定义一个#define N,那么改起来不是很麻烦,3000多行啊,那不是要晕过去了~如果你事先定义了一个#define N ‘常量值’,那么当你要修改程序内部的常量值时,只要修改N后面的常量值就等于把程序内部的要修改的相同常量值都改了,那就很方便了
搜索在程序执行期间其值可以改变的量称为变量,变量类型又分为系统变量和用户自定义变量,用户自定义变量其实和系统变量含义是一样的,在学到结构化编程的时候,你就会明白自定义变量的意思了。系统变量分为四种,int整形变量,用户存放整数的变量,占4个字节的大小;float单精度浮点型,就是存放小数点的数值,占4个字节大小;double高精度浮点型,相对float而言,double保畱的小数点位数更精确,占8个字节的大小;char字符变量,主要存放字母,占1个字节的大小

关于程序的总结:

首先感谢王兄上机课的知道,在王兄的无情打击下让我知道了比较简单的方法就是县打印质数表,然后把输入的数进行处理,扫描质数表,然后输出扫描质数表,输出质数表中对应的质数

下面是我自己对程序中的打印质数表的解释

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值