問題一:
設計一個函式,可以將一個數的質因數印出來。如 12=2*2*3。
問題二:
要怎麼寫出 n的m次方=n*n的m-1次方。
2007-06-10 09:30:35 · 2 個解答 · 發問者 IVT 2 in 電腦與網際網路 ➔ 程式設計
//Power by Visual Studio 2005
#include
#include
#include
#ifndef _MAX_ENV
#define _MAX_ENV 32767
#endif
int isprime(int number){//Check number
int i,retBoolean=1;
//1 is True, 0 is False
for(i=2;i<=(int)sqrt(number);i++){
if((number%i)==0){
retBoolean=0;//false
break;
}
}
return retBoolean;//return boolean value
}
int main(int argc, char* argv[]){
//==========START==========//
int i,num,temp;
printf("(1)Input a number > 2: "),scanf("%d",&num);
if(num>2){
printf("%d= ",num);
for(i=2,temp=num;i<_MAX_ENV;i++)
if(isprime(i)){
while((temp%i)==0){
printf("%d",i),temp/=i;
if(temp!=1)
printf("*");
}
if(temp==1){
printf("\n");
break;
}
}
}
printf("(2)Input N: "),scanf("%d",&num);
printf("Input M: "),scanf("%d",&temp);
printf("%d ^ %d = %d*%d ^ (%d)\n",num,temp,num,num,temp-1);
//==========END==========//
system("PAUSE");
return 0;
}
2007-06-10 12:11:08 · answer #1 · answered by Big_John-tw 7 · 0⤊ 0⤋
問題一
#include
#include
typedef struct prime {
int nPrime;
prime* next;
} stru_prime;
typedef struct prime* pstru_prime;
void insert(pstru_prime* ptr, pstru_prime node, int number);
int main(void)
{
pstru_prime pPrimeList = NULL;
pstru_prime divisor = NULL;
int input;
scanf("%d", &input);
bool bIsFound = false;
for (int i = 2; i <= input; i++)
{
for (int j = 2; j < i; j++)
{
if (i % j == 0)
{
bIsFound = true;
break;
}
}
if (bIsFound)
bIsFound = false;
else
insert(&pPrimeList, pPrimeList, i);
}
int quotient = input;
divisor = pPrimeList;
while (quotient != 1)
{
if (quotient % divisor->nPrime == 0)
{
quotient = quotient / divisor->nPrime;
printf("%d", divisor->nPrime);
if (quotient != 1)
printf("*");
}
else if (divisor->next != NULL)
divisor = divisor->next;
}
return 0;
}
void insert(pstru_prime* ptr, pstru_prime node, int number)
{
pstru_prime tmp;
tmp = (pstru_prime)malloc(sizeof(stru_prime));
tmp->nPrime = number;
if (*ptr)
{
tmp->next = node->next;
node->next = tmp;
}
else
{
tmp->next = NULL;
*ptr = tmp;
}
}
2007-06-10 11:55:21 · answer #2 · answered by 這個世界越來越爛 4 · 0⤊ 0⤋