請選擇 進入手機版 | 繼續訪問電腦版

BLOG記事用論壇

 找回密碼
 註冊
搜索
熱搜:
查看: 4262|回復: 0

測定11的倍數

[複製鏈接]
發表於 2014-2-26 23:58:29 | 顯示全部樓層 |閱讀模式

題目:從檔案讀取數字,可以判斷1000位數的數字是否為11的倍數
Description

Your job is, given a positive number N, determine if it is a multiple of eleven.

Input

The input is a file such that each line contains a positive number. A line containing the number 0 is the end of the input. The given numbers can contain up to 1000 digits.

Output

The output of the program shall indicate, for each input number, if it is a multiple of eleven or not.


Sample Input
112233
30800
2937
323455693
5038297
112234
0
Sample Output
112233 is a multiple of 11.
30800 is a multiple of 11.
2937 is a multiple of 11.
323455693 is a multiple of 11.
5038297 is a multiple of 11.
112234 is not a multiple of 11.
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5. int main()
  6. {
  7.     //ifstream cin("numbers.txt",ios::in);
  8.     string s;
  9.     while (cin >> s) {
  10.         if(s=="0") break;
  11.         int sum[2]={0,0};
  12.         for (int i=0; i<s.length(); i++) {
  13.             int n1 = s[i];
  14.             sum[i%2] = sum[i%2] + n1 -'0';
  15.         }
  16.         if ((sum[0]-sum[1])%11==0)
  17.            cout <<  s << " is a multiple of 11.\n";
  18.         else
  19.             cout <<  s << " is not a multiple of 11.\n";
  20.     }
  21.     //system("pause");
  22.     return 0;
  23. }
複製代碼

您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

手機版|Archiver|綜合討論區

GMT+8, 2019-12-12 01:41 , Processed in 0.131294 second(s), 6 queries , File On.

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回復 返回頂部 返回列表