C++学习记录 (20180208)
2018-02-28 11:18
197 查看
#include "stdafx.h" #include <iostream> #include <iomanip> using namespace std; int main() { double tuition = 10000; int year = 0; for (year = 0; tuition < 20000; year++) { tuition *= 1.07; } cout << "Tuition will be doubled in " << year << "years" << endl; cout << setprecision(2) << fixed << showpoint << "Tuition will be $" << tuition << " in " << year << " years " << endl; //精度设置 return 0; 精度设置: setprecision(x)<<fixed<<showpoint<< x 为精度#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
int main()
{
const int NUMBER_OF_TRIALS = 1000000;
int numberOfHits = 0;
srand(time(0));
for (int i=0; i < NUMBER_OF_TRIALS; i++)
{
double x = rand()*2.0 / RAND_MAX - 1;
double y = rand()*2.0 / RAND_MAX - 1;//RAND_MAX是rand()能够返回的最大值,所以rand()*2.0/RAND_MAX -1 为-1至1的随机数
if (x*x + y * y <= 1)
numberOfHits++;
}
double pi = 4.0*numberOfHits / NUMBER_OF_TRIALS;
cout << " pi is " <<setprecision(5)<<fixed<<showpoint<< pi << endl;
return 0;
}
实例二:十进制转十六进制#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <string> //if u use string u must include this
using namespace std;
int main()
{
cout << "Enter a decimal number: ";
int decimal;
cin >> decimal;
string hex = "";
while (decimal != 0)
{
int hexValue = decimal % 16;
char hexChar = (hexValue <= 9 && hexValue >= 0) ?
static_cast<char>(hexValue + '0'):
static_cast<char>(hexValue - 10 + 'A');
hex = hexChar + hex;
decimal = decimal / 16;
}
cout << "The hex number is " << hex << endl;
return 0;
}
实例:检查回文#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <string> //if u use string u must include this
using namespace std;
int main()
{
while (true)
{
cout << "Please input a string" << endl;
string s;
getline(cin, s);
int low = 0;
int high = s.length() - 1;
bool isPalindrome = true;
while (low < high)
{
if (s[low] != s[high])
{
isPalindrome = false;
break;
}
low++;
high--;
}
if (isPalindrome)
{
cout << s << " is a palindrome" << endl;
}
else
{
cout << s << "is not a palindrome" << endl;
}
}
return 0;
}
#include "stdafx.h" #include <iostream> #include <iomanip> //if u use string u must include this using namespace std; int main() { const int NUMBER_OF_PRIMES = 50; const int NUMBER_OF_PRIMES_PER_LINE = 10; int count = 0; int number = 2; cout << "The first 50 prime numbers are\n"; while (count < NUMBER_OF_PRIMES) { bool isPrime = true; for (int divisor = 2; divisor <= number / 2 && isPrime; divisor++) { if (number % divisor == 0) { isPrime = false; } } if (isPrime) { count++; if (count %NUMBER_OF_PRIMES_PER_LINE == 0) cout << setw(4) << number << endl; //每打印十个数字,换一次行 else cout << setw(4) << number; } number++; } return 0; }
打印前50个素数
exercise1:
#include "stdafx.h" #include <iostream> #include <iomanip> //if u use string u must include this using namespace std; int main() { cout << "Enter an integer, the input ends if it is 0: "; int posCount = 0; int negCount = 0; double total = 0; double num=1; double sum=0; double actualTotal = 0; while (num != 0) { cin >> num; if (num > 0) { total++; actualTotal++; posCount++; sum += num; } else if (num < 0) { total++; actualTotal++; negCount++; sum += num; } else if (total==1 && num==0) { cout << "No numbers are entered except 0 "; break; } else { total++; } } double average = sum / actualTotal; cout << "The number of positives is " << posCount << endl; cout << "The number of negatives is " << negCount << endl; cout << "The total is " << total << endl; cout << "The average is " <<average << endl; return 0; }
相关文章推荐
- 孙鑫C++教程学习记录
- C++学习总结概要记录
- C++ 学习记录1
- C++ 对象模型学习记录(1)--- 第2章 构造函数语义学
- C++ 学习记录之静态变量static
- 算法学习记录十二(C++)--->链表题目集合
- C++学习记录一
- C++学习记录
- Effective C++学习记录之Things To Remember
- C++ VARIANT 学习小记录
- C++学习记录(20180212)
- 完成了C++作业,本博客现在开始全面记录acm学习历程,真正的acm之路,现在开始
- C++学习记录16--VS新版本中iostream库的变化
- Lua和C++交互 学习记录之七:C++全局函数注册为Lua模块
- C++文本文件读写|学习记录
- Lua和C++交互 学习记录之八:C++类注册为Lua模块
- C++中数组学习要点记录
- C++学习记录(留坑)
- C++ primer plus学习记录
- C++学习、成长记录(2011-2012第一学期第18周)