B2066
注意点见代码
#include<iostream>
#include<cmath>
using namespace std;
int main(){
double x,y,n,count,s=0;
cin>>n;
for(int i=1;i<=n;++i){
cin>>x>>y>>count;
s+=1.5*count+2*sqrt(x*x+y*y)/50; //考虑来回
}
cout<<int(ceil(s)); //ceil返回的是浮点型数字
}
B2078
string统计
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(){
string s;
int k;
cin>>s>>k;
if(count(s.begin(),s.end(),'3')==k)
cout<<"YES";
else
cout<<"NO";
}
简单判断
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
long long s;
int k;
int main(){
cin>>s>>k;
int count = 0;
while(s!=0){
if(s%10==3) count++;
s /= 10;
}
if(count==k)
cout<<"YES";
else
cout<<"NO";
}
B3650
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int n;
int main(){
cin>>n;
long long cnt=0;
for(int i=1;i<=n;i++){
cnt+=i;
printf("%lld\n",cnt);
}
}
B3699
注意一定是long long,不然只有70分
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main() {
long long x;
cin >> x;
int flag = 0;
if (x % 62 == 0) {
flag = 1;
} else {
while (x) {
if (x % 100 == 62) {
flag = 1;
break;
}
x /= 10;
}
}
if (flag)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
string的做法
#include <iostream>
#include <string>
using namespace std;
int main() {
long long x;
cin >> x;
// 将整数x转换为字符串
string str = to_string(x);
// 检查字符串是否包含子串"62"
if (str.find("62") != string::npos || x % 62 == 0) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
}
B3915
几个注意点:
1.单独讲特殊情况拎出来讨论;
2.跳格子起始点记做一次,若能达到Yes的条件,最后跳到尾端不计入次数即保证c==i&&i!=n的时候,cnt++;
3.只有c==i&&i!=n的情况下,才能加和
#include <iostream>
#include <string>
const int MAX = 1000000+7;
int N,a[MAX ];
using namespace std;
int main() {
long long n,x;
cin>>n;
int c =1,cnt=0;
if(n==1){
printf("Yes\n0\n");
return 0;
}
else{
for(int i=1;i<=n;i++){
if(c==i&&i!=n) ++cnt;
cin>>x;
if(c==i&&i!=n) c+=x;
}
if(c==n){
cout<<"Yes"<<endl;
}
else {
cout<<"No"<<endl;
}
cout<<cnt<<endl;
}
}
如果采用char型数组,代码简介一些
#include <iostream>
using namespace std;
const int MAX = 1000000 + 7;
int a[MAX];
int main() {
int n;
cin >> n;
int c = 1, cnt = 0;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
while (c < n) {
c += a[c];
cnt++;
if (c > n) {
break;
}
}
if (c == n) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
cout << cnt << endl;
return 0;
}
B2073(看)
计算小数点后的第n位算法
这个算法的关键在于通过不断地取余和乘以 10 来 "移动" 小数点,从而逐步获取到我们想要的小数位。
#include<iostream>
using namespace std;
int a,b,n;
int main()
{
scanf("%d%d%d",&a,&b,&n);
for(int i=1;i<=n;i++) a%=b,a*=10;
a/=b;
printf("%d",a);
return 0;
}
B3721
#include<iostream>
#include<cmath>
using namespace std;
int main() {
int T;
cin>>T;
for(int i=1;i<=T;i++){
long long x,y;
cin>>x>>y;
while(x!=0&&y!=0){
if (x < y) ++x;
else x=floor(x/2);
if (x == 0) break; //不可遗漏
if (x > y) ++y;
else y=floor(y/2);
}
cout<<x<<" "<<y<<endl;
}
return 0;
}
B3898
错误代码,原因,是每行中的完全平方数
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int n;
cin >> n;
int count = 1; // 跟踪当前的字符数
for (int i = 1; i <= n; ++i) {
// 输出空格
for (int j = 0; j < n - i; ++j) {
cout << " ";
}
// 输出 # 或 !
for (int j = 0; j < 2 * i - 1; ++j) {
if (sqrt(count) == floor(sqrt(count))) {
cout << "!";
} else {
cout << "#";
}
++count;
}
cout << "\n";
}
return 0;
}
AC代码
sqrt(count) == floor(sqrt(count))
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
// 输出空格
for (int j = 0; j < n - i; ++j) {
cout << " ";
}
// 输出 # 或 !
int count = 1; // 对于每一行,重新开始计数
for (int j = 0; j < 2 * i - 1; ++j) {
if (sqrt(count) == floor(sqrt(count))) {
cout << "!";
} else {
cout << "#";
}
++count;
}
cout << "\n";
}
return 0;
}
B3919(看)
#include <iostream>
using namespace std;
int main() {
unsigned int n;
int q;
cin >> n >> q;
unsigned long long ans = 0;
while (q--) {
long long k;
cin >> k;
--k; // 将 k 转换为从 0 开始的索引
if (n & (1LL << k)) continue; // 如果第 k 位已经是 1,则跳过
long long c = n % (1LL << k);
ans += (1LL << k) - c;
n += (1LL << k) - c;
}
cout << ans << endl;
return 0;
}