2-16 HDO1106
生活随笔
收集整理的這篇文章主要介紹了
2-16 HDO1106
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這題寒假也沒搞出來,但今天花了一小時終于搞定。
?
題意是輸入一串數字字符,把‘5’當作空格,然后把被分割開的數字進行排序輸出。
?
首先是字符串輸入,按照高精度的處理方法,數值低位放到數組低位。(字符串型的S轉到整形的A)
?
然后,開始對A數組遍歷,將第i位的數值乘以10的i次方,累加到B數組(整形)。遇到5則continue,當然還要對累加時的變量進行重置。
?
上述的做法有兩個點要特別注意到,如果A數組的最后一位不是5,而整個數組里5的數量不為0,則B數組的個數要加1,二是如果數組里5的數量為0,則B的個數也要加1。
?
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 int main() 9 { 10 char s[1005]; 11 int a[1050],b[300],i,j,slen,ilen,pos,top,dou,sum; 12 while(scanf("%s",s)!=EOF) 13 { 14 memset(b,0,sizeof(b)); 15 slen = strlen(s); 16 sum = 0; 17 for(i=slen-1,j=0; i>=0; i--) 18 { 19 a[j] = s[i] - '0'; 20 if(a[j] == 5) 21 sum ++; 22 j++; 23 } 24 if(a[0] == 5) 25 { 26 i = 1; 27 } 28 else 29 { 30 i = 0; 31 } 32 for(top=0,dou=1; i<slen; i++) 33 { 34 if(a[i]==5 && a[i-1] == 5) 35 continue; 36 if(a[i] == 5) 37 { 38 pos = i; 39 dou = 1; 40 top++; 41 continue; 42 } 43 else 44 { 45 b[top] += a[i]*dou; 46 dou*=10; 47 } 48 } 49 if(a[slen-1] != 5 && sum!=0) 50 top++; 51 if(sum == 0) 52 top++; 53 sort(b,b+top); 54 for(i=0; i<top-1; i++) 55 printf("%d ",b[i]); 56 printf("%d\n",b[top-1]); 57 } 58 return 0; 59 }?
轉載于:https://www.cnblogs.com/catdrivedragon/p/3551824.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的2-16 HDO1106的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ext.Net学习笔记01:在ASP.N
- 下一篇: 【Android Developers