输出 “Hello, World!”
#include <iostream>
using namespace std;
int main ( )
{
cout << "Hello, World!" ;
return 0 ;
}
标准输入输出
#include <iostream>
using namespace std;
int main ( )
{
int number;
cout << "输入一个整数: " ;
cin >> number;
cout << "输入的数字为: " << number;
return 0 ;
}
实现两个数相加
#include <iostream>
using namespace std;
int main ( )
{
int firstNumber, secondNumber, sumOfTwoNumbers;
cout << "输入两个整数: " ;
cin >> firstNumber >> secondNumber;
sumOfTwoNumbers = firstNumber + secondNumber;
cout << firstNumber << " + " << secondNumber << " = " << sumOfTwoNumbers;
return 0 ;
}
求商及余数
#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 0 ;
}
查看 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 0 ;
}
交换两个数
#include <iostream>
using namespace std;
int main ( )
{
int a = 5 , b = 10 , 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 0 ;
}
#include <iostream>
#include <iostream>
using namespace std;
int main ( )
{
int a = 5 , b = 10 ;
cout << "交换之前:" << endl;
cout << "a = " << a << ", b = " << b << endl;
a = a + b;
b = a - b;
a = a - b;
cout << "\n交换之后:" << endl;
cout << "a = " << a << ", b = " << b << endl;
return 0 ;
}
判断一个数是奇数还是偶数
#include <iostream>
using namespace std;
int main ( )
{
int n;
cout << "输入一个整数: " ;
cin >> n;
if ( n % 2 == 0 )
cout << n << " 为偶数。" ;
else
cout << n << " 为奇数。" ;
return 0 ;
}
#include <iostream>
using namespace std;
int main ( )
{
int n;
cout << "输入一个整数: " ;
cin >> n;
( n % 2 == 0 ) ? cout << n << " 为偶数。" : cout << n << " 为奇数。" ;
return 0 ;
}
判断元音/辅音
#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 ( isLowercaseVowel || isUppercaseVowel)
cout << c << " 是元音" ;
else
cout << c << " 是辅音" ;
return 0 ;
}
判断三个数中的最大数
#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 0 ;
}
#include <iostream>
using namespace std;
int main ( )
{
float n1, n2, n3;
cout << "请输入三个数: " ;
cin >> n1 >> n2 >> n3;
if ( ( n1 >= n2) && ( n1 >= n3) )
cout << "最大数为: " << n1;
else if ( ( n2 >= n1) && ( n2 >= n3) )
cout << "最大数为: " << n2;
else
cout << "最大数为: " << n3;
return 0 ;
}
#include <iostream>
using namespace std;
int main ( )
{
float n1, n2, n3;
cout << "请输入三个数: " ;
cin >> n1 >> n2 >> n3;
if ( n1 >= n2)
{
if ( n1 >= n3)
cout << "最大数为: " << n1;
else
cout << "最大数为: " << n3;
}
else
{
if ( n2 >= n3)
cout << "最大数为: " << n2;
else
cout << "最大数为: " << n3;
}
return 0 ;
}
求一元二次方程的根
#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 - 4 * a* c;
if ( discriminant > 0 ) {
x1 = ( - b + sqrt ( discriminant) ) / ( 2 * a) ;
x2 = ( - b - sqrt ( discriminant) ) / ( 2 * a) ;
cout << "Roots are real and different." << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
}
else if ( discriminant == 0 ) {
cout << "实根相同:" << endl;
x1 = ( - b + sqrt ( discriminant) ) / ( 2 * a) ;
cout << "x1 = x2 =" << x1 << endl;
}
else {
realPart = - b/ ( 2 * a) ;
imaginaryPart = sqrt ( - discriminant) / ( 2 * a) ;
cout << "实根不同:" << endl;
cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl;
cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl;
}
return 0 ;
}
计算自然数之和
#include <iostream>
using namespace std;
int main ( )
{
int n, sum = 0 ;
cout << "输入一个正整数: " ;
cin >> n;
for ( int i = 1 ; i <= n; ++ i) {
sum + = i;
}
cout << "Sum = " << sum;
return 0 ;
}
判断闰年
#include <iostream>
using namespace std;
int main ( )
{
int year;
cout << "输入年份: " ;
cin >> year;
if ( year % 4 == 0 )
{
if ( year % 100 == 0 )
{
if ( year % 400 == 0 )
cout << year << " 是闰年" ;
else
cout << year << " 不是闰年" ;
}
else
cout << year << " 是闰年" ;
}
else
cout << year << " 不是闰年" ;
return 0 ;
}
求一个数的阶乘
#include <iostream>
using namespace std;
int main ( )
{
unsigned int n;
unsigned long long factorial = 1 ;
cout << "输入一个整数: " ;
cin >> n;
for ( int i = 1 ; i <= n; ++ i)
{
factorial * = i;
}
cout << n << " 的阶乘为:" << " = " << factorial;
return 0 ;
}
创建各类三角形图案
#include <iostream>
using namespace std;
int main ( )
{
int rows;
cout << "输入行数: " ;
cin >> rows;
for ( int i = 1 ; i <= rows; ++ i)
{
for ( int j = 1 ; j <= i; ++ j)
{
cout << "* " ;
}
cout << "\n" ;
}
return 0 ;
}
#include <iostream>
using namespace std;
int main ( )
{
int rows;
cout << "输入行数: " ;
cin >> rows;
for ( int i = 1 ; i <= rows; ++ i)
{
for ( int j = 1 ; j <= i; ++ j)
{
cout << j << " " ;
}
cout << "\n" ;
}
return 0 ;
}
#include <iostream>
using namespace std;
int main ( )
{
char input, alphabet = 'A' ;
cout << "输入最后一个大写字母: " ;
cin >> input;
for ( int i = 1 ; i <= ( input- 'A' + 1 ) ; ++ i)
{
for ( int j = 1 ; j <= i; ++ j)
{
cout << alphabet << " " ;
}
++ alphabet;
cout << endl;
}
return 0 ;
}
#include <iostream>
using namespace std;
int main ( )
{
int rows;
cout << "输入行数: " ;
cin >> rows;
for ( int i = rows; i >= 1 ; -- i)
{
for ( int j = 1 ; j <= i; ++ j)
{
cout << "* " ;
}
cout << endl;
}
return 0 ;
}
#include <iostream>
using namespace std;
int main ( )
{
int rows;
cout << "输入行数: " ;
cin >> rows;
for ( int i = rows; i >= 1 ; -- i)
{
for ( int j = 1 ; j <= i; ++ j)
{
cout << j << " " ;
}
cout << endl;
}
return 0 ;
}
#include <iostream>
using namespace std;
int main ( )
{
int space, rows;
cout << "输入行数: " ;
cin >> rows;
for ( int i = 1 , k = 0 ; i <= rows; ++ i, k = 0 )
{
for ( space = 1 ; space <= rows- i; ++ space)
{
cout << " " ;
}
while ( k != 2 * i- 1 )
{
cout << "* " ;
++ k;
}
cout << endl;
}
return 0 ;
}
#include <iostream>
using namespace std;
int main ( )
{
int rows, count = 0 , count1 = 0 , k = 0 ;
cout << "输入行数: " ;
cin >> rows;
for ( int i = 1 ; i <= rows; ++ i)
{
for ( int space = 1 ; space <= rows- i; ++ space)
{
cout << " " ;
++ count;
}
while ( k != 2 * i- 1 )
{
if ( count <= rows- 1 )
{
cout << i+ k << " " ;
++ count;
}
else
{
++ count1;
cout << i+ k- 2 * count1 << " " ;
}
++ k;
}
count1 = count = k = 0 ;
cout << endl;
}
return 0 ;
}
#include <iostream>
using namespace std;
int main ( )
{
int rows;
cout << "输入行数: " ;
cin >> rows;
for ( int i = rows; i >= 1 ; -- i)
{
for ( int space = 0 ; space < rows- i; ++ space)
cout << " " ;
for ( int j = i; j <= 2 * i- 1 ; ++ j)
cout << "* " ;
for ( int j = 0 ; j < i- 1 ; ++ j)
cout << "* " ;
cout << endl;
}
return 0 ;
}
#include <iostream>
using namespace std;
int main ( )
{
int rows, coef = 1 ;
cout << "Enter number of rows: " ;
cin >> rows;
for ( int i = 0 ; i < rows; i++ )
{
for ( int space = 1 ; space <= rows- i; space++ )
cout << " " ;
for ( int j = 0 ; j <= i; j++ )
{
if ( j == 0 || i == 0 )
coef = 1 ;
else
coef = coef* ( i- j+ 1 ) / j;
cout << coef << " " ;
}
cout << endl;
}
return 0 ;
}
#include <iostream>
using namespace std;
int main ( )
{
int rows, number = 1 ;
cout << "输入行数: " ;
cin >> rows;
for ( int i = 1 ; i <= rows; i++ )
{
for ( int j = 1 ; j <= i; ++ j)
{
cout << number << " " ;
++ number;
}
cout << endl;
}
return 0 ;
}
求两数的最大公约数
#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 0 ;
}
#include <iostream>
using namespace std;
int main ( ) {
int n1, n2, hcf;
cout << "输入两个整数: " ;
cin >> n1 >> n2;
if ( n2 > n1) {
int temp = n2;
n2 = n1;
n1 = temp;
}
for ( int i = 1 ; i <= n2; ++ i) {
if ( n1 % i == 0 && n2 % i == 0 ) {
hcf = i;
}
}
cout << "HCF = " << hcf;
return 0 ;
}
求两数最小公倍数
#include <iostream>
using namespace std;
int main ( )
{
int n1, n2, max;
cout << "输入两个数: " ;
cin >> n1 >> n2;
max = ( n1 > n2) ? n1 : n2;
do
{
if ( max % n1 == 0 && max % n2 == 0 )
{
cout << "LCM = " << max;
break ;
}
else
++ max;
} while ( true ) ;
return 0 ;
}
#include <iostream>
using namespace std;
int main ( )
{
int n1, n2, hcf, temp, lcm;
cout << "输入两个数: " ;
cin >> n1 >> n2;
hcf = n1;
temp = n2;
while ( hcf != temp)
{
if ( hcf > temp)
hcf - = temp;
else
temp - = hcf;
}
lcm = ( n1 * n2) / hcf;
cout << "LCM = " << lcm;
return 0 ;
}
实现一个简单的计算器
#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 0 ;
}
猴子吃桃问题
#include <iostream>
using namespace std;
int main ( )
{
int x, y, n;
for ( x= 1 , n= 0 ; n< 9 ; y= ( x+ 1 ) * 2 , x= y, n++ ) ;
cout<< "第一天共摘的桃子数量为 " << x<< endl;
return 0 ;
}