IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C语言日记 14 循环结构程序设计(循环嵌套) -> 正文阅读

[C++知识库]C语言日记 14 循环结构程序设计(循环嵌套)

例3-18 求100~200之间不能被3整除的数。

我编的:

#include<iostream>
using namespace std;
int main()
{
?? ?int i;
?? ?for (i = 100; i <= 200; i++)
?? ??? ?if (i % 3 != 0)
?? ?cout << i << endl;
?? ??? ?else continue;//和标准答案的主要区别就是这里
}

标准答案,源程序:

#include<iostream>
using namespace std;
int main()
{
?? ?for (int n = 100; n <= 200; n++)
?? ?{
?? ??? ?if (n % 3 != 0)
?? ??? ??? ?cout << n;
?? ?return 0;
?? ?}
}

从这里可以看出:

我们可以在for语句内部直接定义变量类型

例3-19 读入一系列整数,统计出正整数个数 i和负整数个数 j,读入0 则结束

我编的:

#include<iostream>
using namespace std;
int main()
{
?? ?int i,p=0,n=0;
?? ?//p:positive numbers
?? ?//n:negative numbers
?? ?cin >> i;
?? ?while (i!=0)
?? ?{
?? ??? ?if (i > 0)
?? ??? ??? ?p = p + 1;
?? ??? ?else
?? ??? ??? ?n = n + 1;
?? ?}
?? ?cout << "positive numbers:" << p
?? ??? ?<< "negative numbers" << n;
}

结果只能在里面输入一个数,然后就输入不下去了。哪里出了问题?

在输入(语句)过程的循环语句中忘记加入了输入i的语句指令。应改为:

#include<iostream>
using namespace std;
int main()
{
?? ?int i,p=0,n=0;
?? ?//p:positive numbers
?? ?//n:negative numbers
?? ?cin >> i;
?? ?while (i!=0)
?? ?{
?? ??? ?if (i > 0)
?? ??? ??? ?p = p + 1;
?? ??? ?else
?? ??? ??? ?n = n + 1;
?? ??? ?cin >> i;
?? ?}
?? ?cout << "positive numbers:" << p
?? ?<<endl<< "negative numbers" << n;
}?

标准答案,源程序:

#include<iostream>
using namespace std;
int main()
{
?? ?int i = 0, j = 0, n;
?? ?cout << "Enter some integers (enter 0 to quit): "<< endl;
?? ?cin >> n;
?? ?while (n != 0)
?? ?{
?? ??? ?if (n > 0)
?? ??? ??? ?i += 1;
?? ??? ?if (n < 0)
?? ??? ??? ?j += 1;
?? ??? ?cin >> n;
?? ?}
?? ?cout << "Count of positive integers:" << i << endl;
?? ?cout << "Count of negative integers:" << j << endl;
?? ?return 0;
}

例3-20 输入一个正整数,求出它的所有因子(即:公因数)。

我编的:

#include<iostream>
using namespace std;
int main()

?? ?int i , n;
?? ?cin >> i;
?? ?for (n = 1; n < i; n++)
?? ?{
?? ??? ?if (i % n == 0)
?? ??? ??? ?cout << n << endl;
?? ?}
?? ?return 0;
}

有时最好还是注意配置一下前端的环境,毕竟这个还是直接联(关)系到使用者的用户体验

标准答案,源程序:

#include<iostream>
using namespace std;
int main()
{
?? ?int n;
?? ?cout << "Enter a positive integer:";
?? ?cin >> n;
?? ?cout << "Number" << n << " ? ?Factors ? ? ";
?? ?for (int k = 1; k <= n; k++)
?? ??? ?if (n % k == 0)
?? ??? ??? ?cout << k << " ";
?? ?cout << endl;
?? ?return 0;
}

例3-21

编写程序输出以下图案。

?
本题具体的输入,探究研究过程,详见:

C语言日记 书P53 例 3-21:输出图案_宇 -Yu的博客-CSDN博客

?

标准答案,源程序:

#include<iostream>
using namespace std;
int main()
{
?? ?const int N = 4;//输出前4行图案
?? ?for (int i = 1; i <= N; i++)
?? ?{
?? ??? ?for (int j = 1; j <= 30; j++)
?? ??? ??? ?cout << ' ';
?? ??? ?//在图案左侧空 30列
?? ??? ?for (int j = 1; j <= 8 - 2 * i; j++)
?? ??? ??? ?cout << ' ';
?? ??? ?for (int j = 1; j <= 2 * i - 1; j++)
?? ??? ??? ?cout << '*';
?? ??? ?cout << endl;
?? ?}
?? ?//输出后 3 行图案
?? ?for (int i = 1; i <= N - 1; i++)
?? ?{
?? ??? ?for (int j = 1; j <= 30; j++)
?? ??? ??? ?cout << ' ';
?? ??? ?//在图案左侧空 30列
?? ??? ?for (int j = 1; j <= 7 - 2 * i; j++)
?? ??? ??? ?cout << '*';
?? ??? ?cout << endl;
?? ?}
?? ?return 0;
}

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-09-15 01:47:57  更:2022-09-15 01:48:58 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 16:01:14-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码