7 条题解
-
2
三种方法
1.数位拆分
直接用a%10取到最后一位,a/10去除最后一位,直到a=0
#include <bits/stdc++.h> using namespace std; int a; int main() { cin>>a; while (a){ cout<<a%10<<" "; a/=10; } return 0; }
不过这有缺点,如果a超出数据范围,就不能用。
2.字符串
直接输入字符串,在从后向前遍历字符,逐个输出
#include <bits/stdc++.h> using namespace std; string s; int lens; int main() { cin>>s; lens=s.size(); for (int i=lens-1;i>=0;i--){ cout<<s[i]<<" "; } return 0; }
可以输入的数的长度大大增加。
3.字符
逐个输入字符,从后向前输出。
#include <bits/stdc++.h> using namespace std; vector<char> ve; char s; int main(){ while (cin>>s){//cin如果得到输入会返回true,否则返回false ve.push_back(s); } for (int i=ve.size()-1;i>=0;i--) cout<<ve[i]<<" "; return 0; }
- 1
信息
- ID
- 184
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 1
- 标签
- 递交数
- 132
- 已通过
- 88
- 上传者