2017年申特杨建忠:求助C语言啊

来源:百度文库 编辑:杭州交通信息网 时间:2024/05/07 03:11:07
高手帮我编一个输出一个整数的全部素数因子的程序啊(例如,120的因子为2、2、2、3、5),要求使用最基础的语句,例如if,for,swith,continue等。
我只是学习参考,不是为了其他目的的!请各位高手帮忙啊!函数调用还没学过,czmtxz 老兄的看不懂啊!

c++/c:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

bool IsPrime(const int num)
{
int sqt = sqrt(num);
for (int i=2; i<=sqt; i++){
if (num % i == 0)return false;
}
return true;
}

void GetPrimeFactorial(const int num)
{
int sqt = sqrt(num);
for (int i=2; i<=sqt; i++){
if (num%i==0 && IsPrime(i))
cout << setw(5) << i;
}
if(IsPrime(num)) cout << setw(5) << num;
cout << endl;
}

void main()
{
unsigned int n; //5*7*29*4*36=146160
cout << "Input a number(such as 146160): ";
cin >> n;
GetPrimeFactorial(n);
}

运行示例:
Input a number(such as 146160): 140160
2 3 5 73
Press any key to continue

vb:
Private Sub Command1_Click()
s = 0 k = InputBox("请输入一个整数")
For j = 2 To k
If (k Mod j = 0 And Isprime(j))
Then
s = s + j
End If
Next j
Print s
End Sub
Private Function Isprime(n )
Dim flag As Boolean
flag = True
If n = 2 Then
Isprime = True
Else
For i = 2 To n - 1
If n Mod i = 0
Then flag = False
Exit For
End If
Next i
Isprime = True
End If
End Function

main()
{ int a,c=2;
scanf("%d",&a);
while (a/2 > 0) //确保值大于1
{ if (a%c==0)
{ a=a/c;
printf("%d",c);//输入一次因子
c=2;
}else{c=c+1;}
}
};
上面的程序是我即时
写出来的,由于好久没有用C语言了,没经过调式,不过想来一定不会有太大的问题,你用TC运行一次试试,要么就是语法,晕有上面那么复杂吗

//确保绝对正确!!!!!!!!!!!!!!!!!
//———————————————————————
#include <stdio.h>
#include <conio.h>
void main()
{ int num,i;
printf("请输入你要的数:");
scanf("%d",&num);
printf("因子为:");
for(;;)
{ if(num==0||num==1) {break;}
for(i=2;i<=8;i++)
{ if(i==8) {printf("%d ",num); num=0; break;}
if(num%i==0)
{ printf("%d ",i);
num=num/i;
break;
}
}
}
getch();
}

// PrimeNumber.cpp : Defines the entry point for the console application.

#include <iostream>
#include <cstdlib>

using namespace std;

int main(int argc, char* argv[])
{
int i = 0;
int a[100];

for (i=0; i<100; i++) {
a[i] = 0;
}

cout << "Please input the number:" << endl;
cin >> i;

a[0] = i;
i = 0;

div_t div_result;
while (a[i] != 0) {
for (int j=2; j<a[i]; j++) {
div_result = div(a[i], j);
if (div_result.rem == 0) {
a[i] = j;
a[i+1] = div_result.quot;
break;
}
}

i++;
}

i = 0;
while (a[i] != 0) {
cout << a[i] << " ";
i++;
}

cout << endl;

return 0;
}

经过调试,保证正确

楼上的这位仁兄,很明显,你的程序效率极低,当a很大时,你这个程序够呛!