呵呵,您看一下我的程式碼可能會比較知道我的問題出在哪~
#include<iostream>
#include<iomanip>
#include<cstring>
#include<fstream>
void copy( int [] , int [] , int []) ;
void input (int [] ) ;
bool less ( int [] , int [] ) ;
bool equal( int [] , int []) ;
bool isPrime( int [] , int [] ) ;
bool divides( int [] , int [] , int [] ) ;
void increment( int [] ) ;
void increment2( int [] ) ;
void output( int [] ) ;
bool zero( int [] ) ;
using namespace std ;
int main ()
{
int low[100] = {0} ;
int high[100] = {0} ;
int counter[100] = {0} ;
int i[100] = {0} ;
int w[100] = {0} ;
// ofstream outfile ("answer.txt" , ios: ut) ; // 創立一個檔案(file)
input ( low ) ;
input ( high ) ;
copy( low , i , w); // copy the value of low into i
while( less( i, high ) || equal( i, high ) )
{
if( isPrime( i , w ) ) // if i is prime
output( i ); // output i to the file
increment( i ); // increment i by 1
}
system("pause") ;
return 0 ;
}
void input (int input [])
{
char a[100] ;
int c = 0 , place ;
cin >> a ;
int count1 = strlen(a) ; // count1 為初始low的位數
for(int i = 0 ; i <= count1-1 ; i++) // 將char[]印至int[]
input = a - '0' ;
for(int p = count1-1 ; p >= count1/2 ; p--) // 反轉印
{
place = input[p] ;
input[p] = input[c] ;
input[c] = place ;
c++ ;
}
}
void copy( int low[] , int i[] , int w[]) // i 為 count1 位數
{
int count1 = 0 ; // low 的次數
for(int p = 99 ; p >= 0 ; p--)
if( low[p] != 0 )
{
count1 = p+1 ; // 倒回來找,遇到不是0的數的話,位數為p+1
break ;
}
for(int p = 0 ; p <= count1-1 ; p++)
i[p] = low[p] ;
for(int p = 0 ; p <= 99 ; p++) // 為了避免改掉i [] 內的值,所以用 w []做計算
w[p] = i[p] ;
}
bool less (int current[] , int compare [])
{
int current_length = 0 ; // i的位數
int compare_length = 0 ; // high位數
for(int p = 99 ; p >= 0 ; p--)
if(current[p] != 0)
{
current_length = p+1 ;
break ;
}
for(int p = 99 ; p >= 0 ; p--)
if(compare[p] != 0)
{
compare_length = p+1 ;
break ;
}
if( current_length < compare_length )
return true ;
if( current_length > compare_length )
return false ; //位數不小於或等於,就為大於,此時return false
for(int p = current_length ; p >= 0 ; p--) // 位數相等時
{
if(current[p] < compare[p]) // 從最高位開始比較,i[]是否小於high[] ,忽略 == 的狀況
return true ;
if(current[p] > compare[p]) // 從最高位開始比較,i[]是否大於high[] ,忽略 == 的狀況
return false ;
}
return false ; // 因為忽略相等,所以當for條件跑完,< 和 > 條件皆不成立的情況下,兩數必為相等 ,所以return false
}
bool equal (int i[] , int high[])
{
int i_length = 0 ; // i的位數
int high_length = 0 ; // high位數
for(int p = 99 ; p >= 0 ; p--)
if(i[p] != 0)
{
i_length = p+1 ;
break ;
}
for(int p = 99 ; p >= 0 ; p--)
if(high[p] != 0)
{
high_length = p+1 ;
break ;
}
if( i_length < high_length )
return false ;
if( i_length > high_length )
return false ;
for(int p = i_length ; p >= 0 ; p--)
{
if(i[p] < high[p]) // 從最高位開始比較,i[] 是否小於high[] ,忽略 == 的狀況
return false ;
if(i[p] > high[p]) // 從最高位開始比較,i[] 是否大於high[] ,忽略 == 的狀況
return false ;
}
return true ; // 因為忽略相等,所以當for條件跑完,< 和 > 條件皆不成立的情況下,兩數必為相等 ,所以return true
}
bool isPrime( int i[] , int w[] )
{
int j[100] = {0};
j[0] = 2 ; // set the value j to 2
while( less( j , i ) ) // while j is less than i
{
if( divides( j , i , w) ) // if j divides i
return false;
increment2( j ) ; // increment j by 1
}
return true ;
}
bool divides(int j[] , int i[] , int w[])
{
while(less ( j , w ))
{
for(int p = 0 ; p <= 99 ; p++)
{
w[p] = w[p] - j[p] ;
if( w[p] < 0 )
{
w[p+1]-- ;
w[p]+=10 ;
}
}
if( zero (w) ) // 檢查 w[] 是否每個位子都等於0,如果是...則非質數
return true ;
}
return false ;
}
void increment( int increment[] )
{
increment[0]++; // 個位數+1
for(int p = 0 ; p <= 98 ; p++) // 從第一位跑道倒數第二位,如j[p]超過9,則-10。且j[p+1]加1
{
if(increment[p] > 9)
{
increment[p] -= 10 ;
increment[p+1]++ ;
}
}
}
void increment2( int j[] )
{
j[0]++; // 個位數+1
for(int p = 0 ; p <= 98 ; p++) // 從第一位跑道倒數第二位,如j[p]超過9,則-10。且j[p+1]加1
{
if(j[p] > 9)
{
j[p] -= 10 ;
j[p+1]++ ;
}
}
}
void output( int i [] )
{
int count ; //質數的位數
for(int p = 99 ; p >=0 ; p--)
if( i[p] != 0)
{
count = p+1 ;
break ;
}
for(int p = count-1 ; p >= 0; p--)
cout << i [p] << ' ' ;
cout <<endl ;
}
bool zero( int w[] )
{
for(int p = 0 ; p <= 99 ; p++) // 檢查 w[] 是否有數字不等於0,如果有就可能不是質數
{
if( w[p] != 0)
{
return false ;
break ;
}
}
return true ; // 如果每位數都都等於0( 同i[] % j[] == 0 ) ,則不為質數
}
現在問題是
isPrime 一直傳true
divides一直傳false
導致每個數都會印出來(題目要求要質數)
ex : 我輸入 123 321 應該要跑出123~321中的質數,但卻是跑出...123 124 125.....321
isPrime傳true回來,所以就不斷的印出...(debug兩天,還是沒de出來,懷疑是isPrime divides zero 這幾個有可能出問題)
ps:題目還有要求要輸出到一個檔案(file),那個我先擱著,先看印出來東西對不對,對了後,在輸出至檔案!
本文最後由 kimiyoko 於 2017-9-28 16:53 編輯
|