美N鱼澡堂
资源下载链接
资源详情介绍
**Solution Explanation**\n\nFor the task we are given a 10?×?10 matrix that contains only the values `0` and `1`. \nWe have to count how many `1`’s occur in the whole matrix and print that number.\n\nThe input is written as 10 lines, each line containing 10 integers separated by\nwhitespace. \nThe output is a single integer – the number of `1`’s.\n\n\n\n--------------------------------------------------------------------\n\n#### Algorithm\n```\ncount ← 0\nfor each of the 100 numbers read from the input\n if the number is 1\n count ← count + 1\nprint count\n```\n\n--------------------------------------------------------------------\n\n#### Correctness Proof \n\nWe prove that the algorithm outputs the correct number of `1`’s.\n\n*Let `M` be the 10?×?10 matrix given in the input and let `ones(M)` be the\nnumber of entries of `M` that equal `1`.*\n\nThe algorithm scans all 100 numbers of `M` exactly once.\nDuring the scan it increments `count` **iff** the current number equals `1`.\nTherefore after processing the whole matrix\n\n```\ncount = |{ i | the i?th number of M equals 1 }| = ones(M)\n```\n\nThus the algorithm outputs `ones(M)`, which is exactly the required result. ?\n\n\n\n--------------------------------------------------------------------\n\n#### Complexity Analysis\n\nThe algorithm processes 100 numbers, each in constant time.\n\n* Time complexity: `O(100)` → `O(1)` \n* Memory usage: only the counter `count` → `O(1)`\n\n\n\n--------------------------------------------------------------------\n\n#### Reference Implementation (Python?3)\n\n```python\nimport sys\n\ndef solve() -\u003e None:\n # Read all integers from stdin\n data = sys.stdin.read().strip().split()\n # Count how many of them are '1'\n ones = sum(1 for x in data if x == '1')\n # Output the result\n print(ones)\n\nif __name__ == "__main__":\n solve()\n```\n\nThe program follows exactly the algorithm proven correct above.。
电视剧
最新各类电视剧免费下载,电视剧资源
友情链接