一,求一N维数组,对角线的和。
二,字符串先输入,后倒序输出。
用C++ 语言写。
一,求一N维数组,对角线的和。
二,字符串先输入,后倒序输出。
用C++ 语言写。
#include<iostream>
#include<list>
using namespace std;
int main()
{
const MAX=100;
char str[MAX];
cout<<"Enter sentences:";
cin.get(str,MAX);
list<char> theList;
for(int j=0;j<MAX;j++)
theList.push_back( str[j] );
list<char>::reverse_iterator revit;
revit = theList.rbegin();
while( revit != theList.rend() )
cout<<*revit++<<" ";
cout<<endl;
return 0;
}
这个程序可以实现
但有个小问题,就是J循环的最大限制不好限制。我用了MAX所以会出现“?”
#include<iostream>
#include<string>
using namespace std;
void main()
{
char *p;
int n;
cout<<"How many words do you want to input?";
cin>>n;
p=new char[n];
for(int i=0;i<=n-1;i++)
cin>>p[i];
for(i=n-1;i>=0;i--)
{cout<<p[i];
}
}
这样不用限定最大的数目,想输多少输多少
[CODE]#include <iostream>
#include <stack>
using namespace std;
int main()
{
stack<char> a;
char ch;
while((ch=cin.get())!='\n')
a.push(ch);
while(!a.empty())
{
cout<<a.top();
a.pop();
}
system("pause");
return 0;
}[/CODE]