浙大1390 素数问题

news/2025/2/9 14:23:51/

浙大1390 素数问题

1390素数问题
Time Limit:1000MS  Memory Limit:32768K


Description:
任何一个整数,都可以有多个素数相乘,现在给你一个数N(1< N<=65535),请你把它分成多个素数相乘。

Input:
输入一个整数N,输入0表示结束.
Output:
输出相应的结果.
Sample Input:
2
12
16
65535
0
Sample Output:
2
2*2*3
2*2*2*2
3*5*17*257

解答:
#include <iostream>
#include <cmath>
using namespace std;
bool prime(int x)
{
  if(x==2) return true;
  for(int i=2;i<=sqrt((float) x);i++)
  //如果为i<t,输入16输出为2*2*4
  //因为,sqrt(4)等于2时就退出循环了,于是程序将4也当作了素数
  {
    if(x%i==0)
    return false;
        
  }
   return true;

void f(int x)
{ int tag=2,flag=0;
  for(;;)
 {
   if(prime(x))
    {
    if(flag>0) cout<<'*';
    cout<<x<<endl;
    return;//跳到无限循环的唯一地方
    }
   if(x%tag==0)
    {
     if(flag>0) cout<<'*';         
     cout<<tag;
     x/=tag;
     flag++;
    }
   else
   {   tag++;//没有这一句,输入65535进入死循环
 
       while(!prime(tag))
       tag++;
   }         
 }


     

int main()
{int t;
while(cin>>t)
{
 if(!t) break;
 f(t);
             
            
}
  system("pause");
   return 0;
}
//用VC出现编译错误,用GCC提交成功,
//因为sqrt函数给的参数要转化为浮点数

 

解答二:

#include <iostream>
#include <cmath>
using namespace std;
bool prime(int x)
{
 if(x==2) return true;
 int q=sqrt( (double)x );//注意这里的转化
 for(int i=2;i<=q;++i)
  if(x%i==0) return false;
  return true;
}
void f(int x)
{
 int ans=0;
 int tag=2;
 while(1)//变化后的数要重新拿去用if语句做判断就需要一个循环
    {
  if(prime(x))
  {
   if(ans>0) cout<<'*';
   cout<<x<<endl;
   return ;
  }
  if(x%tag==0)//不是素数跳到这里,先除以最小的素数2
        //如果既不是素数,也不是被2整除,再跳到else部分,让除数自增到一个较大的素数
  {
   if(ans>0) cout<<'*';
   cout<<tag;
   x/=tag;
   ++ans;//用来控制什么时候输出*这个符号
  }
  else
  {
   ++tag;
   while(!prime(tag))//当除数不是素数时将其自加直到为素数为止
    ++tag;
  }
 }
}
int main()
{
 int n,tag=0,i;
 while(cin>>n)
 {
  if(!n) break;
  f(n);
 }
 return 0;
}

转载于:https://www.cnblogs.com/qnbs1/articles/1691077.html


http://www.ppmy.cn/news/576635.html

相关文章

AI提高软件外包开发效率

最近几年AI技术取得了很大的进步&#xff0c;在一些领域甚至有突破性的进展&#xff0c;虽然无法预测未来AI会如何影响到人们的生活&#xff0c;但可以确定的是AI会在方方面面影响到大家的生活方式&#xff0c;也许未来五年内就会有一个明显的变化。今天和大家分享AI如何提高软…

GUI简单介绍

GUI编程介绍 组件&#xff1a; 窗口文本框面板弹窗列表框按钮图片监听事件鼠标键盘事件破解工具 简介 GUI的核心技术&#xff1a;Swing AWT 1、因为界面不美观 2、需要jre环境 为什么我们还去学习呢&#xff1f; 1、可以写出自己心中想要的一些小工具 2、工作时候&#xff0c…

[工具] Mysql GUI工具

转载&#xff1a; http://www.iteye.com/news/16083?page3#comments

GUI收集

嵌入式GUI Microwindows由Century Software的CEO Greg Haerr主持开发的一个公开源码(LGPL)的项目。Microwindows致力于为一些小型设备和平台提供现代图形窗口环境。Microwindows支持许多硬件平台&#xff0c;移植性很强。Microwindows的主要目的之一便是运行在嵌入式Linux上&a…

GUI-03

例&#xff1a; import java.awt.*; import java.awt.event.*; class MouseAndKeyEvent { //定义该图形中所需的组件的引用 private Frame f; private Button but; private TextField tf; MouseAndKeyEvent()//构造函数 { init(); } public void init() { f …

Python GUI

Tkinter 内置库&#xff0c;如果没有运行 sudo apt-get install python-tk3.0一下用import Tkinter3.0及以上用import tkinter, tkinter.Tk() from Tkinter import * a Tk() a.wm_title("my friend") b Label(a, text "my friend is she") b.pack() a.m…

GUI(十)

1.什么是GUI&#xff0c;GUI和CLI有什么区别&#xff1f; GUI( Graphical user interface,图形用户接口):用类似windows图形界面的方式完成与用户交互&#xff1b; CLI(Command line User Interface ,命令行用户接口&#xff09;&#xff1a;需要通过用户通过命令和计算机交…

GUI介绍

GUI介绍 文章来源&#xff1a;http://en.wikipedia.org/wiki/Graphical_user_interface GUI定义&#xff1a; Graphical user interface is a type of user interface, it offers graphical icons and visual indicators as opposed to text-based interfaces, typed comman…