c ++通过给定输入显示接下来的5个数字的更短方法?
c++ shorter way to display the next 5 numbers by given inputs?
我刚学了cpp,想知道有没有更短的方式来显示序号。
这是我的代码
#include<iostream>
using namespace std;
int main(){
int n;
cout<<"Input number: ";
cin>>n;
cout<<"The next 5 rows of number is :"<<n+1<<n+2<<n+3<<n+4<<n+5;
}
一个简单的循环应该可以解决您的问题:
int main(){
int n;
cout << "Input number: ";
cin >> n;
cout << "The next 5 rows of number are: ";
for (int i = n + 1; i <= n + 5; i++) {
cout << i << ' ';
}
}
我刚学了cpp,想知道有没有更短的方式来显示序号。
这是我的代码
#include<iostream>
using namespace std;
int main(){
int n;
cout<<"Input number: ";
cin>>n;
cout<<"The next 5 rows of number is :"<<n+1<<n+2<<n+3<<n+4<<n+5;
}
一个简单的循环应该可以解决您的问题:
int main(){
int n;
cout << "Input number: ";
cin >> n;
cout << "The next 5 rows of number are: ";
for (int i = n + 1; i <= n + 5; i++) {
cout << i << ' ';
}
}