zzuli oj 1167逆转数(指针专题)
生活随笔
收集整理的這篇文章主要介紹了
zzuli oj 1167逆转数(指针专题)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Description
任意給你一個(gè)整數(shù),這個(gè)數(shù)可能很大(最長不超過100位),你能求出它的逆轉(zhuǎn)數(shù)嗎??
逆轉(zhuǎn)數(shù)定義如下:?
1.一個(gè)末尾沒有0的整數(shù),它的逆轉(zhuǎn)數(shù)就是各位數(shù)字逆序輸出;?
2.一個(gè)負(fù)數(shù)的逆轉(zhuǎn)數(shù)仍是負(fù)數(shù);?
3.一個(gè)末尾有0的整數(shù),它的逆轉(zhuǎn)數(shù)如同下例:?
reverse (1200) = 2100?
reverse (-56) = -65?
要求定義并使用如下函數(shù):?
void reverse(char *str)?
{?
//函數(shù)求出str的逆轉(zhuǎn)數(shù)并存入str。?
}?
Input
輸入一個(gè)長整數(shù)str,不超過100位,輸入的整數(shù)不含前導(dǎo)0。
Output
輸出str的逆轉(zhuǎn)數(shù)。輸出占一行。
Sample Input
-123456789000Sample Output
-987654321000 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 using namespace std; 6 7 int main(int argc, char** argv) 8 { 9 char str[101]; 10 int i,flag=0,count=0; 11 gets(str); 12 /*輸出負(fù)號*/ 13 if(str[0]=='-') 14 printf("-"); 15 /*倒序輸出*/ 16 for(i=strlen(str)-1;i>=1;i--) 17 { 18 if(str[i]=='0'&&flag==0) 19 { 20 count++;//用來計(jì)數(shù)0的個(gè)數(shù) 21 continue;//跳過 22 } 23 if(str[i]!='0'||flag!=0) 24 { 25 printf("%c",str[i]); 26 flag=1;//標(biāo)記尾數(shù)是否為0 27 } 28 29 } 30 if(str[0]!='-') 31 printf("%c",str[0]);//最后輸出第一個(gè)數(shù) 32 /*輸出前面跳過的0*/ 33 for(i=1;i<=count;i++) 34 printf("0"); 35 return 0; 36 }?
轉(zhuǎn)載于:https://www.cnblogs.com/millerchan/p/5059783.html
總結(jié)
以上是生活随笔為你收集整理的zzuli oj 1167逆转数(指针专题)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 老李分享:Web Services 组件
- 下一篇: C++ 自由存储区是否等价于堆?