1. 程式人生 > >整除的尾數(語言訓練題)

整除的尾數(語言訓練題)

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 51968 Accepted Submission(s): 22013

Problem Description 一個整數,只知道前幾位,不知道末二位,被另一個整數除盡了,那麼該數的末二位該是什麼呢?
Input 輸入資料有若干組,每組資料包含二個整數a,b(0<a<10000, 10<b<100),若遇到0 0則處理結束。
Output 對應每組資料,將滿足條件的所有尾數在一行內輸出,格式見樣本輸出。同組資料的輸出,其每個尾數之間空一格,行末沒有空格。
Sample Input
200 40
1992 95
0 0
Sample Output
00 40 80
15

題目分析:
運算上不難,關鍵是格式,博主也是submit了好多次,最後才ac了。
程式分析:
呼叫標頭檔案,使用vector類儲存數量不定的子數字,用size()返回有效元素的個數s,用for迴圈輸出s-1個元素,並輸出空格,最後一個元素單獨輸出,後面不帶空格。
程式實現:

#include <iostream>
#include<vector>
using namespace std;
int main()
{
 int a,b,c,d,i=0;
 while (cin >> a >> b)
 {
  if (a == 0 &&
b == 0) { break; } vector <int> s; a = a * 100; for (c = 0; c < 100; c++) { if ((a + c) % b == 0) s.push_back(c); } d = s.size(); for ( i = 0; i < d - 1; i++) { if (s[i] < 10) cout << "0" << s[i] << " "; else cout << s[
i] << " "; } if (s[i] < 10) cout << "0" << s[i] ; else cout << s[i] ; cout << endl; } }