语法基础课听课笔记
- 第一讲变量,输入输出,表达式与顺序语句
- 第二讲 判断语句
- 第五讲 字符串
-
- AcWing760.字符串长度
- AcWing761.字符串中的数字个数
- AcWing763.循环相克令
- AcWing765.字符串加空格
- AcWing769.替换字符
- AcWing773.字符串插入
- AcWing772.只出现一次的字符
- AcWing762.字符串匹配
- AcWing768.忽略大小写比较字符串大小
- AcWing817.数组去重
- AcWing809.最小公倍数
- AcWing766.去掉多余的空格
- AcWing767.信息加密
- AcWing764.输出字符串
- AcWing770.单词替换
- AcWing771.字符串中最长的连续出现的字符
- AcWing774.最长单词
- AcWing775.倒排单词
- AcWing776.字符串移位包含问题
- 第六讲 函数
- 第八讲 STL容器位运算与常用库函数
第一讲变量,输入输出,表达式与顺序语句
#include<cstdio>
using namespace std;
int main()
{
double a, b;
scanf("%lf %lf", &a, &b);
printf("MEDIA = %.5lf\n", (a * 3.5 + b * 7.5) / 11) ;
return 0;
}
单精度(float)在计算机中存储占用4字节,32位,有效位数为7位(6位小数+小数点)。
双精度(double)在计算机中存储占用8字节,64位,有效位数为16位(15位小数+小数点)。
#include <iostream>
using namespace std;
int main()
{
int n, a[7] = {
100, 50, 20, 10, 5, 2, 1};
cin >> n;
printf("%d\n", n);
for (int i = 0; i < 7; i ++ )
{
printf("%d nota(s) de R$ %d,00\n", n / a[i], a[i]);
n %= a[i];
}
return 0;
}
c++ 控制输出格式的方法
cout.setf(ios_base::fixed); // 若不加这一句,则输出为科学计数法表示的数
cout.precision(2);
// 放大100倍,避免小数的问题。
#include <iostream>
#include <cstdio>
using namespace std;
int a[12] = {
10000, 5000, 2000, 1000, 500, 200, 100, 50, 25, 10, 5, 1};
int ans[12];
int main()
{
double n;
cin >> n;
int m = (int)(n * 100);
for (int i = 0; i < 12; i++)
{
ans[i] = m / a[i];
m %= a[i];
}
puts("NOTAS:");
for (int i = 0; i < 6; i++)
printf("%d nota(s) de R$ %.2lf\n", ans[i], (double)a[i] / 100);
puts("MOEDAS:");
for (int i = 6; i < 12; i++)
printf("%d moeda(s) de R$ %.2lf\n", ans[i], (double)a[i] / 100);
return 0;
}
第二讲 判断语句
#include <cstdio>
double price[6] = {
0, 4, 4.5, 5, 2, 1.5};
int x, y;
int main() {
scanf("%d %d", &x, &y);
printf("Total: R$ %.2f", price[x] * y);
return 0;
}
第五讲 字符串
AcWing760.字符串长度
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
string a;
getline(cin,a);
cout<<a.size()<<endl;
return 0;
}
AcWing761.字符串中的数字个数
#include <iostream>
using namespace std;
string s;
int cnt;
int main()
{
getline(cin,s);
for (int i = 0; i < s.size(); i ++)
if(s[i]>='0'&&s[i]<='9') cnt++;
cout << cnt;
}
AcWing763.循环相克令
#include<iostream>
using namespace std;
int get(string s)
{
if(s == "Hunter") return 0;
if(s == "Bear") return 1;
return 2;
}
int main()
{
int n;
cin >> n;
while(n --)
{
string a,b;
cin >> a >> b;
int x = get(a), y = get(b);
if(x == y) cout << "Tie" <<endl;
else if((x + 1) % 3 == y) cout << "Player2"<< endl;
else cout << "Player1" << endl;
}
}
AcWing765.字符串加空格
#include<iostream>
using namespace std;
int main()
{
string a;
getline(cin, a);
string b;
for (auto c : a) b = b + c + ' '; // for (auto c : a) b += c + ' ';如果这样写的话输出会乱码。
b.pop_back();
cout << b << endl;
return 0;
}
AcWing769.替换字符
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
string a;
char s;
getline(cin,a);
cin>>s;
for(int i = 0;i < a.size();i ++)
{
if(a[i] == s) cout << "#";
else cout<<a[i];
}
return 0;
}
AcWing773.字符串插入
#include<iostream>
using namespace std;
int