竞赛
考级
读取输入的整数n; 用for循环从1开始,每次步长为2(确保只遍历奇数); 输出时,第一个数直接打印,后续数前加空格,保证格式正确; 最后输出换行,若n<1则直接输出空行,符合题目要求。
思路:for套if,if找奇数。
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ 都看到这里了,点个关注呗,点个赞也行啊!!!!!!!!!!!!!!!!!
本题考查对for循环的应用,我们可以循环1-n,然后加一个 最后,在i的后面输出一个空格。 题解: 时间复杂度:O(n)
都看到这了,点个赞再走嘛!
#include<bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; for(int i = 1;i <= n;i++){ if(i % 2 == 1){ cout << i << " "; } } return 0; }
简单,看哥的
#include<bits/stdc++.h>//submitRecord// using namespace std; //const int N=1e7+10; int main(){ //freopen("","r",stdin); //freopen("","w",stdout); ios::sync_with_stdio(); cin.tie(nullptr); cout.tie(nullptr); int n; cin>>n; for(int i=1;i<=n;i+=2) cout<<i<<" "; //fclose(stdin); //fclose(stdout); return 0; }
提交答案之后,这里将显示提交结果~