解题思路: 拆成一个一个字符存入数组借由数组下标对其输出,以下附代码及运行图:
注意事项: 限制条件一定要明确,此题要注意 len<=4 !
参考代码:
#include<iostream> using namespace std; int main() { char ch; int arr[5] = {0}; int len = 0; while (((ch = getchar()) != '\n')&&len <= 4) { arr[len++] = ch - 48; } cout << len << endl; for (int i = 0; i < len - 1; i++)cout << arr[i] << " "; cout << arr[len - 1]; cout <<endl; for (int i = len - 1; i >= 0; i--) cout << arr[i]; return 0; }
0.0分
3 人评分