C++ 实例 - 输出 "Hello, World!"
#include <iostream>
using namespace std; int main()
{
cout << "Hello, World!";
return ;
}
C++ 实例 - 标准输入输出
#include <iostream>
using namespace std; int main()
{
int number; cout << "输入一个整数: ";
cin >> number; cout << "输入的数字为: " << number;
return ;
}
C++ 实例 - 实现两个数相加
#include <iostream>
using namespace std; int main()
{
int firstNumber, secondNumber, sumOfTwoNumbers; cout << "输入两个整数: ";
cin >> firstNumber >> secondNumber; // 相加
sumOfTwoNumbers = firstNumber + secondNumber; // 输出
cout << firstNumber << " + " << secondNumber << " = " << sumOfTwoNumbers; return ;
}
C++ 实例 - 求商及余数
#include <iostream>
using namespace std; int main()
{
int divisor, dividend, quotient, remainder; cout << "输入被除数: ";
cin >> dividend; cout << "输入除数: ";
cin >> divisor; quotient = dividend / divisor;
remainder = dividend % divisor; cout << "商 = " << quotient << endl;
cout << "余数 = " << remainder; return ;
}
C++ 实例 - 查看 int, float, double 和 char 变量大小
#include <iostream>
using namespace std; int main()
{
cout << "char: " << sizeof(char) << " 字节" << endl;
cout << "int: " << sizeof(int) << " 字节" << endl;
cout << "float: " << sizeof(float) << " 字节" << endl;
cout << "double: " << sizeof(double) << " 字节" << endl; return ;
}
C++ 实例 - 交换两个数
#include <iostream>
using namespace std; int main()
{
int a = , b = , temp; cout << "交换之前:" << endl;
cout << "a = " << a << ", b = " << b << endl; temp = a;
a = b;
b = temp; cout << "\n交换之后:" << endl;
cout << "a = " << a << ", b = " << b << endl; return ;
}
C++ 实例 - 判断一个数是奇数还是偶数
#include <iostream>
using namespace std; int main()
{
int n; cout << "输入一个整数: ";
cin >> n; if ( n % == )
cout << n << " 为偶数。";
else
cout << n << " 为奇数。"; return ;
}
C++ 实例 - 判断元音/辅音
#include <iostream>
using namespace std; int main()
{
char c;
int isLowercaseVowel, isUppercaseVowel; cout << "输入一个字母: ";
cin >> c; // 小写字母元音
isLowercaseVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'); // 大写字母元音
isUppercaseVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'); // if 语句判断
if (isLowercaseVowel || isUppercaseVowel)
cout << c << " 是元音";
else
cout << c << " 是辅音"; return ;
}
C++ 实例 - 判断三个数中的最大数
#include <iostream>
using namespace std; int main()
{
float n1, n2, n3; cout << "请输入三个数: ";
cin >> n1 >> n2 >> n3; if(n1 >= n2 && n1 >= n3)
{
cout << "最大数为: " << n1;
} if(n2 >= n1 && n2 >= n3)
{
cout << "最大数为: " << n2;
} if(n3 >= n1 && n3 >= n2) {
cout << "最大数为: " << n3;
} return ;
}
C++ 实例 - 求一元二次方程的根
#include <iostream>
#include <cmath>
using namespace std; int main() { float a, b, c, x1, x2, discriminant, realPart, imaginaryPart;
cout << "输入 a, b 和 c: ";
cin >> a >> b >> c;
discriminant = b*b - *a*c; if (discriminant > ) {
x1 = (-b + sqrt(discriminant)) / (*a);
x2 = (-b - sqrt(discriminant)) / (*a);
cout << "Roots are real and different." << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
} else if (discriminant == ) {
cout << "实根相同:" << endl;
x1 = (-b + sqrt(discriminant)) / (*a);
cout << "x1 = x2 =" << x1 << endl;
} else {
realPart = -b/(*a);
imaginaryPart =sqrt(-discriminant)/(*a);
cout << "实根不同:" << endl;
cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl;
cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl;
} return ;
}
C++ 实例 - 计算自然数之和
#include <iostream>
using namespace std; int main()
{
int n, sum = ; cout << "输入一个正整数: ";
cin >> n; for (int i = ; i <= n; ++i) {
sum += i;
} cout << "Sum = " << sum;
return ;
}
C++ 实例 - 判断闰年
#include <iostream>
using namespace std; int main()
{
int year; cout << "输入年份: ";
cin >> year; if (year % == )
{
if (year % == )
{
// // 这里如果被 400 整数是闰年
if (year % == )
cout << year << " 是闰年";
else
cout << year << " 不是闰年";
}
else
cout << year << " 是闰年";
}
else
cout << year << " 不是闰年"; return ;
}
C++ 实例 - 求一个数的阶乘
#include <iostream>
using namespace std; int main()
{
unsigned int n;
unsigned long long factorial = ; cout << "输入一个整数: ";
cin >> n; for(int i = ; i <=n; ++i)
{
factorial *= i;
} cout << n << " 的阶乘为:"<< " = " << factorial;
return ;
}
C++ 实例 - 创建各类三角形图案
#include <iostream>
using namespace std; int main()
{
int rows; cout << "输入行数: ";
cin >> rows; for(int i = ; i <= rows; ++i)
{
for(int j = ; j <= i; ++j)
{
cout << "* ";
}
cout << "\n";
}
return ;
}
#include <iostream>
using namespace std; int main()
{
char input, alphabet = 'A'; cout << "输入最后一个大写字母: ";
cin >> input; for(int i = ; i <= (input-'A'+); ++i)
{
for(int j = ; j <= i; ++j)
{
cout << alphabet << " ";
}
++alphabet; cout << endl;
}
return ;
}
#include <iostream>
using namespace std; int main()
{
int rows; cout << "输入行数: ";
cin >> rows; for(int i = rows; i >= ; --i)
{
for(int j = ; j <= i; ++j)
{
cout << "* ";
}
cout << endl;
} return ;
}
#include <iostream>
using namespace std; int main()
{
int rows; cout << "输入行数: ";
cin >> rows; for(int i = rows; i >= ; --i)
{
for(int j = ; j <= i; ++j)
{
cout << j << " ";
}
cout << endl;
} return ;
}
#include <iostream>
using namespace std; int main()
{
int space, rows; cout <<"输入行数: ";
cin >> rows; for(int i = , k = ; i <= rows; ++i, k = )
{
for(space = ; space <= rows-i; ++space)
{
cout <<" ";
} while(k != *i-)
{
cout << "* ";
++k;
}
cout << endl;
}
return ;
}
#include <iostream>
using namespace std; int main()
{
int rows, count = , count1 = , k = ; cout << "输入行数: ";
cin >> rows; for(int i = ; i <= rows; ++i)
{
for(int space = ; space <= rows-i; ++space)
{
cout << " ";
++count;
} while(k != *i-)
{
if (count <= rows-)
{
cout << i+k << " ";
++count;
}
else
{
++count1;
cout << i+k-*count1 << " ";
}
++k;
}
count1 = count = k = ; cout << endl;
}
return ;
}
#include <iostream>
using namespace std; int main()
{
int rows; cout << "输入行数: ";
cin >> rows; for(int i = rows; i >= ; --i)
{
for(int space = ; space < rows-i; ++space)
cout << " "; for(int j = i; j <= *i-; ++j)
cout << "* "; for(int j = ; j < i-; ++j)
cout << "* "; cout << endl;
} return ;
}
#include <iostream>
using namespace std; int main()
{
int rows, coef = ; cout << "Enter number of rows: ";
cin >> rows; for(int i = ; i < rows; i++)
{
for(int space = ; space <= rows-i; space++)
cout <<" "; for(int j = ; j <= i; j++)
{
if (j == || i == )
coef = ;
else
coef = coef*(i-j+)/j; cout << coef << " ";
}
cout << endl;
} return ;
}
#include <iostream>
using namespace std; int main()
{
int rows, number = ; cout << "输入行数: ";
cin >> rows; for(int i = ; i <= rows; i++)
{
for(int j = ; j <= i; ++j)
{
cout << number << " ";
++number;
} cout << endl;
} return ;
}
C++ 实例 - 求两数的最大公约数
#include <iostream>
using namespace std; int main()
{
int n1, n2; cout << "输入两个整数: ";
cin >> n1 >> n2; while(n1 != n2)
{
if(n1 > n2)
n1 -= n2;
else
n2 -= n1;
} cout << "HCF = " << n1;
return ;
}
C++ 实例 - 求两数最小公倍数
#include <iostream>
using namespace std; int main()
{
int n1, n2, max; cout << "输入两个数: ";
cin >> n1 >> n2; // 获取最大的数
max = (n1 > n2) ? n1 : n2; do
{
if (max % n1 == && max % n2 == )
{
cout << "LCM = " << max;
break;
}
else
++max;
} while (true); return ;
}
C++ 实例 - 实现一个简单的计算器
#include <iostream>
using namespace std; int main()
{
char op;
float num1, num2; cout << "输入运算符:+、-、*、/ : ";
cin >> op; cout << "输入两个数: ";
cin >> num1 >> num2; switch(op)
{
case '+':
cout << num1+num2;
break; case '-':
cout << num1-num2;
break; case '*':
cout << num1*num2;
break; case '/':
cout << num1/num2;
break; default:
// 如果运算符不是 +, -, * 或 /, 提示错误信息
cout << "Error! 请输入正确运算符。";
break;
} return ;
}
猴子吃桃问题
一只小猴子一天摘了许多桃子,第一天吃了一半,然后忍不住又吃了一个;第二天又吃了一半,再加上一个;后面每天都是这样吃。到第10天的时候,小猴子发现只有一个桃子了。问小猴子第一天共摘了多少个桃子。
#include <iostream>
using namespace std;
int main()
{
int x, y, n;
for (x=, n=; n<; y=(x+)*, x=y, n++);
cout<<"第一天共摘的桃子数量为 "<<x<<endl;
return ;
}

最新文章

  1. hibernate配置文件详细解析
  2. ae学习
  3. 可展开的列表组件——ExpandableListView深入解析
  4. 802.1x协议&amp;eap类型
  5. Angular学习(4)- 数组双向梆定
  6. 数组去重算法,quickSort
  7. WPF TextBox 的 EventTrigger &amp; 重写控件
  8. ETL中的数据增量抽取机制
  9. linux脚本:shell, 判断输入参数的个数(命令行)
  10. 最常用的UML工具介绍
  11. Jsp运行环境——Tomcat
  12. windows 下运行angualr/material2 项目
  13. 【一天一道LeetCode】#100. Same Tree(100题大关)
  14. 面试心得随谈&amp;线程并发的总结
  15. Gitlab_ansible_jenkins三剑客⑥Jenkins和ansible集成
  16. webpack配置css相关loader注意先后顺序
  17. MySql 三大知识点,索引、锁、事务,原理分析
  18. cf276E 两棵线段树分别维护dfs序和bfs序,好题回头再做
  19. lucene 4.0 - Facet demo
  20. X86主要的几种寻址方式

热门文章

  1. 【转载】将Centos的yum源更换为国内的阿里云源
  2. vbox虚拟机vdi文件用VMware打开
  3. 报错信息 Context []startup failed due to previous errors
  4. JavaScript之bind方法实现代码分析
  5. JForum项目搭建
  6. 落谷p1325雷达安装(计算几何)
  7. upload-labs-env文件上传漏洞 11-19关
  8. 2.13 阶段实战 使用layui重构选课系统
  9. C++面试常见问题——13结构体与共用体的sizeof
  10. Solr查询和过滤器执行顺序剖析