程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 打印從1到最大的n位數

打印從1到最大的n位數

編輯:C++入門知識

面試題:輸入數值n,打印從1到最大的n位數,例如輸入n=3,則從1,2,3,一直打印到999   該題會考查大整數的加法,我之前做大整數運算時,為了圖方便,用整數數組來表示,但這會浪費空間,這次我用字符數組來表示,並且存數的時候是按正常順序來存取   在判斷是否到達上限時,通過判斷最高位是否需要做進位運算來判斷,具體代碼如下:    

#include <iostream>   
#include <string.h>   
#include <fstream>   
#include <stack>   
#include <exception>   
using namespace std;  
void print(char *num,int n)  
{  
bool shouldStart=false;  
for(int index=0;index<n;index++)  
{  
if(!shouldStart && num[index]!='0')  
{  
shouldStart=true;  
}  
if(shouldStart)  
{  
cout<<num[index];  
}  
}  
cout<<endl;  
}  
bool incNum(char *num,int n)  
{  
bool overFlow=false;  
int carryBit=0;  
for(int index=n-1;index>=0;index--)  
{  
int sum=num[index]-'0'+carryBit;  
if(index==n-1)  
{  
sum++;  
}  
if(sum>=10)  
{  
if(index==0)  
{  
overFlow=true;  
}else  
{  
sum=sum%10;  
carryBit=1;  
num[index]=sum+'0';  
}  
}else  
{  
carryBit=0;  
num[index]=sum+'0';  
break;  
}  
}  
return overFlow;  
}  
void printAllNum(int n)  
{  
if(n<=0)  
{  
//throw new exception("the input is error");   
}  
char *num=new char[n+1];  
memset(num, '0', n);  
num[n]='\0';  
while(!incNum(num,n))  
{  
print(num,n);  
}  
delete []num;  
}  
  
int main()  
{  
int n;  
cin>>n;  
printAllNum(n);  
//getchar();   
return 0;  
}  

#include <iostream>
#include <string.h>
#include <fstream>
#include <stack>
#include <exception>
using namespace std;
void print(char *num,int n)
{
bool shouldStart=false;
for(int index=0;index<n;index++)
{
if(!shouldStart && num[index]!='0')
{
shouldStart=true;
}
if(shouldStart)
{
cout<<num[index];
}
}
cout<<endl;
}
bool incNum(char *num,int n)
{
bool overFlow=false;
int carryBit=0;
for(int index=n-1;index>=0;index--)
{
int sum=num[index]-'0'+carryBit;
if(index==n-1)
{
sum++;
}
if(sum>=10)
{
if(index==0)
{
overFlow=true;
}else
{
sum=sum%10;
carryBit=1;
num[index]=sum+'0';
}
}else
{
carryBit=0;
num[index]=sum+'0';
break;
}
}
return overFlow;
}
void printAllNum(int n)
{
if(n<=0)
{
//throw new exception("the input is error");
}
char *num=new char[n+1];
memset(num, '0', n);
num[n]='\0';
while(!incNum(num,n))
{
print(num,n);
}
delete []num;
}

int main()
{
int n;
cin>>n;
printAllNum(n);
//getchar();
return 0;
}

 

第二種解法   n位數相當於對做個全排列,在輸出時,把前導的0去掉,就會輸出正確結果    
#include <iostream>   
#include <string.h>   
#include <fstream>   
//#include <stack>   
//#include <exception>   
using namespace std;  
void printFinal(char *num,int n)  
{  
 bool shouldStart=false;  
 for(int index=0;index<n;index++)  
 {  
  if(!shouldStart && num[index]!='0')  
  {  
   shouldStart=true;  
  }  
  if(shouldStart)  
  {  
   cout<<num[index];  
  }  
 }  
 if(shouldStart)  
 {  
 cout<<endl;  
 }  
}  
void print(char *num, int n,int start)  
{  
    if(start==n)  
 {  
  printFinal(num,n);  
  return;  
 }  
 for(int val=0;val<10;val++)  
 {  
  num[start]=val+'0';  
  print(num,n,start+1);  
 }  
}  
void printAllNum(int n)  
{  
 if(n<=0)  
 {  
  //throw new exception("the input is error");   
 }  
 char *num=new char[n+1];  
 memset(num, '0', n);  
 num[n]='\0';  
 print(num,n,0);  
}  
  
  
int main()  
{  
 int n;  
 cin>>n;  
 printAllNum(n);  
 //getchar();   
 return 0;  
}  

#include <iostream>
#include <string.h>
#include <fstream>
//#include <stack>
//#include <exception>
using namespace std;
void printFinal(char *num,int n)
{
 bool shouldStart=false;
 for(int index=0;index<n;index++)
 {
  if(!shouldStart && num[index]!='0')
  {
   shouldStart=true;
  }
  if(shouldStart)
  {
   cout<<num[index];
  }
 }
 if(shouldStart)
 {
 cout<<endl;
 }
}
void print(char *num, int n,int start)
{
    if(start==n)
 {
  printFinal(num,n);
  return;
 }
 for(int val=0;val<10;val++)
 {
  num[start]=val+'0';
  print(num,n,start+1);
 }
}
void printAllNum(int n)
{
 if(n<=0)
 {
  //throw new exception("the input is error");
 }
 char *num=new char[n+1];
 memset(num, '0', n);
 num[n]='\0';
 print(num,n,0);
}


int main()
{
 int n;
 cin>>n;
 printAllNum(n);
 //getchar();
 return 0;
}

 

     

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved