abs( num) 返回整型的绝对值
fabs( num) 返回双精度的绝对值
exp( x) 返回指数函数ex的值
pow( x, y) 返回x^y的值
sqrt( x) 返回√x的值
printf()输出格式大全(附 - 示例代码)
printf()输出格式大全(附 - 示例代码)
现值计算
AcWing4891.现值计算
# include <bits/stdc++.h>
using namespace std;
double ans;
int main ( )
{
int n;
double i;
cin>> n>> i;
for ( int j= 0 ; j<= n; j++ )
{
int x;
cin>> x;
ans+= x* pow ( ( 1 + i) , - j) ;
}
cout<< ans<< endl;
return 0 ;
}
AcWing 4699. 如此编码
AcWing 4699. 如此编码
# include <bits/stdc++.h>
using namespace std;
const int N = 22 ;
int main ( )
{
int n, m;
int a[ N] ;
cin>> n>> m;
for ( int i= 1 ; i<= n; i++ ) cin>> a[ i] ;
for ( int i= 1 ; i<= n; i++ )
{
cout<< m% a[ i] << " " ;
m/= a[ i] ;
}
return 0 ;
}
AcWing 4509. 归一化处理(小数位数+根号函数)
AcWing 4509. 归一化处理
# include <bits/stdc++.h>
using namespace std;
const int N = 1010 ;
int n;
double w[ N] ;
int main ( )
{
cin>> n;
double sum= 0 ;
for ( int i= 0 ; i< n; i++ )
{
cin>> w[ i] ;
sum+= w[ i] ;
}
double avg= sum/ n;
double d= 0 ;
for ( int i= 0 ; i< n; i++ )
d+= pow ( ( w[ i] - avg) , 2 ) / n;
d= sqrt ( d) ;
for ( int i= 0 ; i< n; i++ )
printf ( "%.16lf\n" , ( w[ i] - avg) / d) ;
return 0 ;
}
AcWing 4454. 未初始化警告
AcWing 4454. 未初始化警告
# include <bits/stdc++.h>
using namespace std;
const int N= 1e5 + 10 ;
int n;
int k;
bool st[ N] ;
int x, y;
int ans= 0 ;
int main ( )
{
cin>> n>> k;
for ( int i= 0 ; i<= n; i++ ) st[ i] = false ;
st[ 0 ] = true ;
for ( int i= 0 ; i< k; i++ )
{
cin>> x>> y;
if ( ! st[ y] )
ans++ ;
st[ x] = true ;
}
cout<< ans<< endl;
return 0 ;
}
AcWing 4280. 序列查询
AcWing 4280. 序列查询
# include <bits/stdc++.h>
using namespace std;
const int N= 1e7 + 10 ;
int n, m;
int a[ N] ;
int ans;
int main ( )
{
cin>> n>> m;
for ( int i= 1 ; i<= n; i++ ) cin>> a[ i] ;
a[ n+ 1 ] = m;
for ( int i= 0 ; i<= n; i++ )
ans+= ( a[ i+ 1 ] - a[ i] ) * i;
cout<< ans<< endl;
return 0 ;
}
AcWing 4006. 数组推导(小陷阱)
AcWing 4006. 数组推导
# include <bits/stdc++.h>
using namespace std;
const int N= 1e2 + 10 ;
int b[ N] ;
int main ( )
{
int n;
cin>> n;
for ( int i= 0 ; i< n; i++ ) cin>> b[ i] ;
int maxs= 0 , mins= 0 ;
for ( int i= 0 ; i< n; i++ )
{
maxs+= b[ i] ;
if ( ! i|| b[ i] > b[ i- 1 ] )
mins+= b[ i] ;
}
cout<< maxs<< endl<< mins<< endl;
return 0 ;
}
AcWing 3292. 称检测点查询
AcWing 3292. 称检测点查询
题目需要首先按照距离排序,再按照编号排序
采用pair类型,将first设置为距离,second设置为编号
调用sort函数进行排序( sort对pair排序先排first,再排second)
存储距离时,不按照样例存储开根号的值,直接存储平方
# include <bits/stdc++.h>
using namespace std;
const int N= 200 + 10 ;
typedef pair< int , int > PII;
PII node[ N] ;
int X, Y;
int n;
int main ( )
{
cin>> n>> X>> Y;
for ( int i= 0 ; i< n; i++ )
{
int x, y;
cin>> x>> y;
node[ i] . first= ( X- x) * ( X- x) + ( Y- y) * ( Y- y) ;
node[ i] . second= i;
}
sort ( node, node+ n) ;
for ( int i= 0 ; i< 3 ; i++ )
{
cout<< node[ i] . second+ 1 << endl;
}
return 0 ;
}
AcWing 3287. 线性分类器(直线有关)
AcWing 3287. 线性分类器
用结构体存储点的信息,sumA记录类型A的点的个数,sumB记录类型B的点的个数
通过比较在直线一侧各种类型点的数量和sumA、sumB,当一侧全为A类型或者B类型时,满足分类条件
由于一个点只能位于直线的一侧,因此我们仅选取直线的上侧进行计数,即满足c+a*node[ j] .x+b*node[ j] .y> 0
# include <bits/stdc++.h>
using namespace std;
const int N= 1e3 + 10 ;
typedef long long LL;
LL n, m;
int sum_A= 0 ;
int sum_B= 0 ;
struct {
LL x;
LL y;
char type;
} node[ N] ;
int main ( )
{
cin>> n>> m;
for ( LL i= 0 ; i< n; i++ )
{
LL x, y;
char type;
cin>> x>> y>> type;
node[ i] . x= x;
node[ i] . y= y;
node[ i] . type= type;
if ( type== 'A' )
sum_A++ ;
else
sum_B++ ;
}
for ( LL i= 0 ; i< m; i++ )
{
LL a, b, c;
cin>> a>> b>> c;
LL sum_A_UP= 0 ;
LL sum_B_UP= 0 ;
for ( LL j= 0 ; j< n; j++ )
{
if ( a+ b* node[ j] . x+ c* node[ j] . y> 0 && node[ j] . type== 'A' )
sum_A_UP++ ;
if ( a+ b* node[ j] . x+ c* node[ j] . y> 0 && node[ j] . type== 'B' )
sum_B_UP++ ;
}
if ( ( sum_A== sum_A_UP&& sum_B_UP== 0 ) || ( sum_B== sum_B_UP&& sum_A_UP== 0 ) )
cout<< "Yes" << endl;
else
cout<< "No" << endl;
}
return 0 ;
}
AcWing 3282. 报数
AcWing 3282. 报数
# include <bits/stdc++.h>
using namespace std;
int res[ 4 ] ;
int main ( )
{
int n;
cin>> n;
int i= 1 ;
while ( n)
{
if ( i% 7 == 0 || to_string ( i) . find ( '7' ) != - 1 )
res[ i% 4 ] ++ ;
else
n-- ;
i++ ;
}
for ( int i= 1 ; i< 4 ; i++ )
cout<< res[ i] << endl;
cout<< res[ 0 ] << endl;
return 0 ;
}
AcWing 3277. 小明种苹果
AcWing 3277. 小明种苹果
# include <bits/stdc++.h>
using namespace std;
int n, m;
int main ( )
{
cin>> n>> m;
int T= 0 , K= 0 , P= - 1 ;
for ( int i= 1 ; i<= n; i++ )
{
int total= 0 ;
int sum= 0 ;
cin>> total;
for ( int j= 0 ; j< m; j++ )
{
int x;
cin>> x;
sum+= abs ( x) ;
}
T+= total- sum;
if ( sum> P)
K= i, P= sum;
}
cout<< T<< " " << K<< " " << P<< endl;
return 0 ;
}
AcWing 3272. 小中大(小数位数)
AcWing 3272. 小中大
# include <bits/stdc++.h>
using namespace std;
const int N = 100010 ;
int n;
int q[ N] ;
int main ( )
{
scanf ( "%d" , & n) ;
for ( int i = 0 ; i < n; i ++ ) scanf ( "%d" , & q[ i] ) ;
int res[ 3 ] ;
res[ 0 ] = q[ 0 ] * 2 , res[ 1 ] = q[ n - 1 ] * 2 ;
if ( n % 2 ) res[ 2 ] = q[ n / 2 ] * 2 ;
else res[ 2 ] = q[ n / 2 - 1 ] + q[ n / 2 ] ;
sort ( res, res + 3 ) ;
for ( int i = 2 ; i >= 0 ; i -- )
if ( res[ i] % 2 ) printf ( "%.1lf " , res[ i] / 2.0 ) ;
else printf ( "%d " , res[ i] / 2 ) ;
return 0 ;
}
AcWing 3257. 跳一跳
AcWing 3257. 跳一跳
# include <bits/stdc++.h>
using namespace std;
int main ( )
{
int x;
int ans= 0 ;
int score= 0 ;
while ( cin>> x, x)
{
if ( x== 1 )
{
ans+= 1 ;
score= 0 ;
}
else
{
score+= 2 ;
ans+= score;
}
}
cout<< ans<< endl;
return 0 ;
}
打酱油(贪心算法)
思路:(贪心算法)
1 .尽可能多的选择买5瓶送2两瓶的方式,然后再选择买3瓶送1瓶,最后才选择10块钱1瓶的方式
# include <bits/stdc++.h>
using namespace std;
const int N= 400 ;
int n;
int main ( )
{
int fifty;
int thirty;
int ten;
cin>> n;
fifty= n/ 50 ;
thirty= ( n- fifty* 50 ) / 30 ;
ten= ( n- fifty* 50 - 30 * thirty) / 10 ;
cout<< 7 * fifty+ 4 * thirty+ ten<< endl;
return 0 ;
}
AcWing 1216. 饮料换购(数学推理)
共喝饮料数res,当前瓶盖数 n
赋值给n,且res= n
一直循环直到瓶盖n< 3 ,每次res += n / 3 ,n变成了新的饮料/3加上剩下的n%3
# include <bits/stdc++.h>
using namespace std;
int main ( )
{
int n;
cin>> n;
int res= n;
while ( n>= 3 )
{
res+= n/ 3 ;
n= n% 3 + n/ 3 ;
}
cout<< res<< endl;
return 0 ;
}
AcWing1219.移动距离
AcWing1219.移动距离
# include <iostream>
using namespace std;
int main ( )
{
int w, m, n;
cin>> w>> m>> n;
m-- , n-- ;
int x1= m/ w, x2= n/ w;
int y1= m% w, y2= n% w;
if ( x1% 2 == 0 )
y1= w- y1- 1 ;
if ( x2% 2 == 0 )
y2= w- y2- 1 ;
cout<< abs ( x1- x2) + abs ( y1- y2) << endl;
return 0 ;
}
AcWing 3242. 分蛋糕—双指针—看
AcWing 3242. 分蛋糕
# include <bits/stdc++.h>
using namespace std;
const int N= 1e3 + 10 ;
int a[ N] ;
int ans;
int main ( )
{
int n, k;
cin>> n>> k;
for ( int i= 1 ; i<= n; i++ ) cin>> a[ i] ;
for ( int i= 1 ; i<= n; i++ )
{
int j= i, sum= 0 ;
while ( j<= n && sum< k) sum+= a[ j++ ] ;
ans++ ;
i= j- 1 ;
}
cout<< ans<< endl;
return 0 ;
}
AcWing 3237. 中间数—看
AcWing 3237. 中间数
# include <iostream>
# include <cstring>
# include <algorithm>
using namespace std;
const int N = 1010 ;
int n;
int q[ N] ;
int main ( )
{
cin >> n;
for ( int i = 0 ; i < n; i ++ ) cin >> q[ i] ;
for ( int i = 0 ; i < n; i ++ )
{
int d = 0 , u = 0 ;
for ( int j = 0 ; j < n; j ++ )
if ( q[ j] < q[ i] ) d ++ ;
else if ( q[ j] > q[ i] ) u ++ ;
if ( u == d)
{
cout << q[ i] << endl;
return 0 ;
}
}
puts ( "-1" ) ;
return 0 ;
}
AcWing 3217. 数列分段
AcWing 3217. 数列分段
# include <bits/stdc++.h>
using namespace std;
const int N= 1e3 + 10 ;
int n;
int a[ N] ;
int main ( )
{
cin>> n;
int ans= 1 ;
for ( int i= 0 ; i< n; i++ )
{
cin>> a[ i] ;
}
for ( int i= 1 ; i< n; i++ )
{
if ( a[ i- 1 ] != a[ i] )
ans++ ;
}
cout<< ans<< endl;
return 0 ;
}
# include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10 ;
int n;
int a[ 1001 ] ;
int main ( )
{
cin>> n;
for ( int i= 0 ; i< n; i++ )
cin>> a[ i] ;
cout<< unique ( a, a+ n) - a;
return 0 ;
}
AcWing 3197.相反数
AcWing 3197.相反数
# include <bits/stdc++.h>
using namespace std;
const int N= 1e3 + 10 ;
int n;
int cnt[ N] ;
int main ( )
{
cin>> n;
while ( n-- )
{
int x;
cin>> x;
cnt[ abs ( x) ] ++ ;
}
int ans= 0 ;
for ( int i= 0 ; i< N; i++ )
{
if ( cnt[ i] == 2 )
ans++ ;
}
cout<< ans<< endl;
}