hdu 短,hdu
短 Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)Total Submission(s) : 172 Accepted Submission(s) : 7Font: Times New Roman | Verdana | Georgia Font Size: ← →Problem Description輸出[L, R]中能被9整除的數的個數 Input多組測試資料
每組測試資料包含兩個數L和R
所有資料都在int範圍內。 Output每組資料輸出一個整數,表示區間[L, R]中能被9整除的數的個數 Sample Input
1 91 10
Sample Output
11
/*題解:
有點略坑的題,考慮完所有的情況再Wrong了一次的情況下A了。
注意點:
輸入資料為int範圍內的整數,所以可以是負數,例如-18、-19
輸入資料不一定L<=R,所以先判斷大小並交換位置。(L<=R則不用交換)
0也可以被9整除
【0,0】,【9,9】,【-9,-9】能被整除個數均為1
提供幾組資料:
【0,19】 3
【9,18】 2
【-18,-9】 2
【-20,9】 4
【-18,-7】 2
*/
#include<cstdio>#include<cmath>#include<stdlib.h>int main(){ int l,r,t; while(scanf("%d %d",&l,&r)!=EOF) { if(l>r)//不交換會Output Limit Exceeded { t=l; l=r; r=t; } if(l==r)//情況一、l==r { if(l%9==0) { printf("1\n"); } else printf("0\n"); } else { if(l>0)//l>0,r>0 { printf("%d\n",r/9-(l-1)/9); } else if(l==0)//l==0,r>0 { printf("%d\n",r/9+1); } else if(l<0) { if(r==0)//l<0,r==0 { printf("%d\n",abs(l/9)+1); } else if(r<0)//l<0,r<0 { printf("%d\n",abs(l/9)-(abs(r)-1)/9); } else if(r>0)//l<0,r>0 { printf("%d\n",r/9+abs(l/9)+1); } } } } return 0;}