You are given a number n with a length of no more than 105 . You can perform the…
- Assistant
Hello! How can I assist you today?
- User
You are given a number n with a length of no more than 105 .
You can perform the following operation any number of times: choose one of its digits, square it, and replace the original digit with the result. The result must be a digit (that is, if you choose the digit x , then the value of x2 must be less than 10 ).
Is it possible to obtain a number that is divisible by 9 through these operations?
Input The first line contains an integer t (1≤t≤104 ) — the number of test cases.
The only line of each test case contains the number n , without leading zeros. The length of the number does not exceed 105 .
It is guaranteed that the sum of the lengths of the numbers across all test cases does not exceed 105 .
Output For each test case, output "YES" if it is possible to obtain a number divisible by 9 using the described operations, and "NO" otherwise.
You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer.
Example InputCopy 9 123 322 333333333333 9997 5472778912773 1234567890 23 33 52254522632 OutputCopy NO YES YES NO NO YES NO YES YES Note In the first example, from the integer 123 , it is possible to obtain only 123 , 143 , 129 , and 149 , none of which are divisible by 9 .
- Assistant
- User
c++ code
- Assistant
Sure! Below is the equivalent C++ code for solving the problem of determining if a number can be transformed to be divisible by 9 through the squaring of its digits.
#include <iostream> #include <string> using namespace std; bool canFormDivisibleBy9(const string &n) { int digitSum = 0; bool canSquareToZero = false; for (char digit : n) { digitSum += digit - '0'; // Convert char to int and accumulate // Check if the digit can become 0 mod 9 when squared (3, 6, or 9) if (digit == '3' || digit == '6' || digit == '9') { canSquareToZero = true; } } digitSum %= 9; // Get the sum mod 9 // If the sum is already 0 modulo 9, return true if (digitSum == 0) { return true; } // If we can square a digit to become 0 (mod 9), return true return canSquareToZero; } int main() { int t; cin >> t; while (t--) { string n; cin >> n; if (canFormDivisibleBy9(n)) { cout << "YES" << endl; } else { cout << "NO" << endl; } } return 0; } - User
remove comments
- Assistant
Sure! Below is the same C++ code without comments:
#include <iostream> #include <string> using namespace std; bool canFormDivisibleBy9(const string &n) { int digitSum = 0; bool canSquareToZero = false; for (char digit : n) { digitSum += digit - '0'; if (digit == '3' || digit == '6' || digit == '9') { canSquareToZero = true; } } digitSum %= 9; if (digitSum == 0) { return true; } return canSquareToZero; } int main() { int t; cin >> t; while (t--) { string n; cin >> n; if (canFormDivisibleBy9(n)) { cout << "YES" << endl; } else { cout << "NO" << endl; } } return 0; }This version of the code retains the functionality while removing all the comments. You can compile and run it in the same way as previously described.
- User
make it look like a human wrote it
- Assistant
Sure! Here's a more human-readable version of the same C++ code. I've made some minor adjustments to formatting and naming conventions to enhance clarity and readability while keeping the code functional:
#include <iostream> #include <string> using namespace std; bool canBeDivisibleBy9(const string &number) { int sumOfDigits = 0; bool hasSquareToZeroMod9 = false; for (char digit : number) { sumOfDigits += digit - '0'; if (digit == '3' || digit == '6' || digit == '9') { hasSquareToZeroMod9 = true; } } sumOfDigits %= 9; if (sumOfDigits == 0) { return true; // Already divisible by 9 } return hasSquareToZeroMod9; // Check if there's a digit that can square to zero mod 9 } int main() { int testCases; cin >> testCases; while (testCases--) { string number; cin >> number; if (canBeDivisibleBy9(number)) { cout << "YES" << endl; } else { cout << "NO" << endl; } } return 0; }