c++ - 使用递归将整数值数组以相反的顺序输出到屏幕
问题描述
void IntegerReversed(int* a, int n)
{
if (n < 1) {
return;
}
else {
cout << a[n - 1] << endl;
Integer(a, n - 1);
}
}
int main()
{
int* a;
int n;
cout << "Input n: ";
cin >> n;
a = new int[n];
for (int i = 0;i < n;i++) {
cin >> *(a + i);
}
cout << "Integer values reversed in array: " << endl;
IntegerReversed(a, n);
}
嗨,这是我的代码,用于使用递归以相反的顺序将整数值数组输出到屏幕。
但它只打印第一个正确的元素
输入:a[4]={1,2,3,4}
但是输出: 4 , 1 , 2 , 3
我想打印: 4 , 3 , 2 , 1 你能帮我修复这个代码吗
解决方案
我测试了你的程序..
void IntegerReversed(int* a, int n)
{
if (n < 1) {
return;
}
else {
cout << a[n - 1] << endl;
IntegerReversed(a, n - 1);
}
}
这只是一个拼写错误..在递归调用中..
推荐阅读
- xamarin.forms - 如何在以 Xamarin 形式流式传输 rtsp 时拍摄快照
- ubuntu - 如何从 systemd 服务从同一台 Ubuntu 机器启动新的 XSession
- ruby - rspec:如何期望该页面具有所需的 html
- guidewire - 索赔中心:在 FNOL 向导期间,当被保险人是公司且报告人是人时,MainContact 值不会持续存在
- ios - 在 Xamarin.iOS 中使用嵌入式框架验证应用程序时出现问题
- .net-core - 在 .Net Core 中按项目对 Swagger 进行分组
- laravel - 如何在 Laravel 的网页中显示 README.md?
- python - 在几种方法中重新抛出自定义异常 C++11
- python - python http服务器日志记录设置
- google-chrome - Chrome 在调试 PhpStorm/Xdebug 期间重新加载页面