C++ 键盘输入10个数字 然后逆序输出 样例输入 1 2 3 4 5 6 7 8 9 0 样例输出 0 9 8 7 6 5 4 3 2 1

如题所述

第1个回答  2011-05-03
#include <iostream>
using namespace std;
int main() {
int arr [10];
for ( int i = 0 ; i < 10 ; i ++ ) { cin >> arr[i]; }
for ( i = 9 ; i >= 0 ; i -- ) { cout << arr[i] << " "; }
cout << endl;

return 0;
}

楼上的 i 重定义了..吧第二个int i 的 int 删掉就行了
第2个回答  2011-05-03
楼上得回答完全是正确得!int arr [10]; 定义一个 数组用来存你输入的数字!for ( int i = 0 ; i < 10 ; i ++ ) { cin >> arr[i]; } 让你输入10个数字 for ( int i = 9 ; i >= 0 ; i -- ) { cout << arr[i] << " "; }
输出你输入的10个数字
cout << endl; 换行!
第3个回答  推荐于2018-05-06
#include <iostream>
using namespace std;
int main() {
int arr [10];
for ( int i = 0 ; i < 10 ; i ++ ) { cin >> arr[i]; }
for ( int i = 9 ; i >= 0 ; i -- ) { cout << arr[i] << " "; }
cout << endl;

return 0;
}追问

第一个人编的是什么?

追答

输入 + 输出

追问

是C++?怎么不对呢

追答

gcc 4.5.2 , gentoo 通过,如果你的编译器有问题,可以把第二句的 int i 改成 i ,去掉声明就行了

本回答被网友采纳
第4个回答  2011-05-03
//-------------------------------------方法一:采用数组下标
#include <iostream>
using namespace std;
int main() {
int arr [10];
for ( int i = 0 ; i < 10 ; i ++ ) { cin >> arr[i]; }
for ( int i = 9 ; i >= 0 ; i -- ) { cout << arr[i] << " "; }
cout << endl;

return 0;
}

//-----------------------------------------------方法二:采用STL中的迭代器
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void display(int i)
{
cout<<i<<" ";
}
void main()
{
vector<int> Intnum;
int i;
cout<<"Ctrl+Z to end input"<<endl;
while(cin>>i)
{
Intnum.push_back(i);
}
for_each(Intnum.rbegin(),Intnum.rend(),display);
cout<<endl;
}

lz:虽然用STL感觉繁琐了很多,我别无他意,只是想让楼主了解STL的强大功能,在以后遇到复杂的问题时可以考虑运用STL。