標籤:華為上機題
簡要描述:給出一個不多於5位的整數, 進行反序處理
要求:
1、求出它是幾位元
2、分別輸出每一位元字
3、按逆序輸出各位元字,例如原數為321,應輸出123(僅數字間以空格間隔, 負號與數字之間不需要間隔)
例如:輸入:12345
輸出:5
1 2 3 4 5
54321
注意:如果是負數,負號加在第一個數字之前, 與數字沒有空格間隔
比如
輸入:-12345
輸出:5
-1 2 3 4 5
-54321
函數原型:
/*
輸入:
iInput: 位元不大於5的整數
輸出:
iNum: 整數位元
strRst: 空格間隔輸出結果
iOutput:逆序整數
返回:
0 成功
-1 輸入無效
*/
int ProcessX(int iInput, int& iNum, char * strRst, int& iOutput)
#include <stdlib.h>#include "oj.h"/*功能:給出一個不多於5位的整數,要求:1、求出它是幾位元 2、分別輸出每一位元字 3、按逆序輸出各位元字,例如原數為321,應輸出123輸入:整型 iInput,位元不大於5的整數輸出: 整數位元iNum空格間隔輸出結果 strRst逆序整數iOutput返回:0 成功-1 輸入無效*/int ProcessX(int iInput, int& iNum, char * strRst, int& iOutput){ if(iInput>99999||iInput<-99999)return -1;iNum=0;iOutput=0; int temp=0; //輸入為0if(iInput==0) { iNum=1; strRst[0]='0'; strRst[1]='\0'; iOutput=0; } else if(iInput>0) { int temp=iInput; while(temp>0) { iOutput=iOutput*10+temp%10; iNum++; temp=temp/10; } int t=0; temp=iOutput; while(temp>0) { strRst[t++]=temp%10+'0'; strRst[t++]=' '; temp=temp/10; } strRst[--t]='\0'; } else if(iInput<0){iInput=-iInput; int temp=iInput; while(temp>0) { iOutput=iOutput*10+temp%10; iNum++; temp=temp/10; } iOutput=-iOutput; int t=1; temp=-iOutput; strRst[0]='-'; while(temp>0) { strRst[t++]=temp%10+'0'; strRst[t++]=' '; temp=temp/10; }strRst[--t]='\0';}return 0;}
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
整型反序