c++编程:例如输入5 显示 1 2 3 4 5 5 1 2 3 4 4 5 1 2 3 3 4 5 1 2 2 3 4 5 1
#include<iostream>usingnamespacestd;intmain(){intn,i;cout<<"Pleaseinputanumber:";cin>...
#include <iostream>
using namespace std;
int main(){
int n,i;
cout <<"Please input a number: ";
cin >> n;
if (n>9) {
cout <<"Please input a number less than 10.";
}
else {
for (i=1;i<=n;i++){
cout << i<<" " ;
}
cout <<endl;
int m(n),a(1);
for (;m>=2;m--){
for (;m<=n;m++){
cout << m<<" ";
}
for (;a<m-1;a++){
cout << a<<" ";
}
cout << endl;
}
}
system("PAUSE");
}
请问错在哪里了?刚学 见笑了,求达人详细解释(不要新的代码,指出我的错误就行)
题目应该是 例如输入5 显示: 12345
51234
45123
34512
23451
不好意思 题目不能换行 展开
using namespace std;
int main(){
int n,i;
cout <<"Please input a number: ";
cin >> n;
if (n>9) {
cout <<"Please input a number less than 10.";
}
else {
for (i=1;i<=n;i++){
cout << i<<" " ;
}
cout <<endl;
int m(n),a(1);
for (;m>=2;m--){
for (;m<=n;m++){
cout << m<<" ";
}
for (;a<m-1;a++){
cout << a<<" ";
}
cout << endl;
}
}
system("PAUSE");
}
请问错在哪里了?刚学 见笑了,求达人详细解释(不要新的代码,指出我的错误就行)
题目应该是 例如输入5 显示: 12345
51234
45123
34512
23451
不好意思 题目不能换行 展开
展开全部
改动的地方标示出来了,不理解的话再说。另附自己编的版本。
#include <iostream>
using namespace std;
int main()
{
int n=0,i=0;
cout <<"Please input a number: ";
cin >> n;
if (n>9)
cout <<"Please input a number less than 10.";
else
{
for (i=1;i<=n;i++)
{
cout << i<<" " ;
}
cout <<endl;
int m=n,a=1,m1=0;
for (;m>=2;m--)
{
m1=m; //这里
for (;m1<=n;m1++) //这里
{
cout << m1<<" "; //这里
}
for (;a<m;a++)
{
cout << a<<" ";
}
cout << endl;
a=1; //这里
}
}
system("PAUSE");
return 0; //规范
}
代码优化版:
#include <iostream>
using namespace std;
int main()
{
int n=0,i=0,j=0,k=0;
cout <<"Please input a number: ";
cin >> n;
if (n>9)
cout <<"Please input a number less than 10.";
else
{
for (i=0;i<n;i++)
{
for(k=0;k<n;j++,k++)
cout<<j%n+1<<" ";
cout<<"\n";
j=(--j)%n;
}
}
system("PAUSE");
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int n=0,i=0;
cout <<"Please input a number: ";
cin >> n;
if (n>9)
cout <<"Please input a number less than 10.";
else
{
for (i=1;i<=n;i++)
{
cout << i<<" " ;
}
cout <<endl;
int m=n,a=1,m1=0;
for (;m>=2;m--)
{
m1=m; //这里
for (;m1<=n;m1++) //这里
{
cout << m1<<" "; //这里
}
for (;a<m;a++)
{
cout << a<<" ";
}
cout << endl;
a=1; //这里
}
}
system("PAUSE");
return 0; //规范
}
代码优化版:
#include <iostream>
using namespace std;
int main()
{
int n=0,i=0,j=0,k=0;
cout <<"Please input a number: ";
cin >> n;
if (n>9)
cout <<"Please input a number less than 10.";
else
{
for (i=0;i<n;i++)
{
for(k=0;k<n;j++,k++)
cout<<j%n+1<<" ";
cout<<"\n";
j=(--j)%n;
}
}
system("PAUSE");
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询