课堂
# include
using namespace std;
int main ( )
{
int n;
cin >> n;
bool is_prime = true ;
for ( int i = 2 ; i< n; i++ )
{
if ( n% i== 0 )
{
cout<< i << endl;
is_prime = false ;
break ;
}
}
if ( is_prime) cout<< "yes" << endl;
else cout<< "not" << endl;
return 0 ;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
2. 矩阵(难题)
# include
using namespace std;
int main ( )
{
int n;
cin >> n;
5
int k = 1 ;
for ( int i = 1 ; i <= n; i++ )
{
for ( int j = 1 ; j <= n; j++ , k++ )
{
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
3. 菱形 (难题)
# include
# include
using namespace std;
int main ( )
{
int n;
cin >> n;
int cx = n/ 2 , cy = n/ 2 ;
for ( int i= 0 ; i< n; i++ )
{
for ( int j = 0 ; j< n; j++ )
{
if ( abs ( i- cx) + abs ( j- cy) <= n/ 2 )
cout<< "*" ;
else
cout<< ' ' ;
}
cout<< endl;
}
return 0 ;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
例题
1. 偶数
Acwing 708.偶数
# include
# include
using namespace std;
int main ( )
{
for ( int i= 2 ; i<= 100 ; i+= 2 )
cout << i << endl;
return 0 ;
}
2. 奇数
Acwing 709.奇数
# include
using namespace std;
int main ( )
{
int n;
cin>> n;
for ( int i = 1 ; i<= n; i+= 2 )
cout<< i<< endl;
return 0 ;
}
3. 正数
Acwing 712.正数
# include
using namespace std;
int main ( )
{
int cnt = 0 ;
for ( int i = 0 ; i< 6 ; i++ )
{
double x;
cin >> x;
if ( x > 0 )
cnt ++ ;
}
cout << cnt << " positive numbers" << endl;
return 0 ;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
4. 连续奇数的和1
Acwing 714.连续奇数的和1
# include
# include
using namespace std;
int main ( )
{
int x, y;
cin >> x >> y;
if ( x > y) swap ( x, y) ;
int sum = 0 ;
int i = x+ 1 ;
while ( i < y)
{
if ( i % 2 )
sum += i;
i++ ;
}
cout << sum << endl;
return 0 ;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
5. 最大数和它的位置
Acwing 716.最大数和它的位置
# include
using namespace std;
int main ( )
{
int max = 0 ;
int position ;
for ( int i = 1 ; i <= 100 ; i ++ )
{
int x;
cin >> x;
if ( x > max)
{
max = x;
position = i;
}
}
cout << max << endl;
cout << position << endl;
return 0 ;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
6. 递增排列
Acwing 721.递增排列
# include
using namespace std;
int main ( )
{
int x;
while ( true )
{
cin >> x;
if ( ! x) break ;
for ( int i = 1 ; i <= x; i++ )
cout << i << ' ' ;
cout << endl;
}
return 0 ;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
7. 连续整数相加(难点一直读数据)
Acwing 720.连续整数相加
while(cin>>n,n<=0);// 如果为小等于0的数,则一直读入数据
# include
using namespace std;
int main ( )
{
int a, n;
cin >> a;
while ( cin>> n, n<= 0 ) ;
int s = 0 ;
for ( int i = 0 ; i< n; i++ )
{
s+= a+ i;
}
cout<< s<< endl;
return 0 ;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
8. 约数
Acwing 724.约数
# include
using namespace std;
int main ( )
{
int a;
cin>> a;
for ( int i = 1 ; i<= a; i++ )
{
if ( a% i== 0 )
{
cout<< i<< endl;
}
}
return 0 ;
}
9. PUM
Acwing 723.PUM
# include
using namespace std;
int main ( )
{
int n, m;
int k = 1 ;
cin >> n >> m;
for ( int i = 0 ; i < n; i++ )
{
for ( int i = 1 ; i <= m; i++ , k++ )
{
if ( i!= m)
{
cout << k << ' ' ;
}
else
{
cout << "PUM" ;
cout << endl;
}
}
}
return 0 ;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
习题
1. 余数
Acwing 715.余数
# include
using namespace std;
int main ( )
{
int n;
cin >> n;
for ( int i = 1 ; i < 10000 ; i++ )
{
if ( i% n== 2 )
cout << i << endl;
}
return 0 ;
}
2. 六个奇数
Acwing 710.六个余数
# include
using namespace std;
int main ( )
{
int n;
cin >> n;
if ( n% 2 == 0 ) n++ ;
for ( int i= 0 ; i< 6 ; i++ ) cout<< n+ i* 2 << endl;
}
3. 乘法表
Acwing 711.乘法表
# include
using namespace std;
int main ( )
{
int n;
cin >> n;
for ( int i = 1 ; i <= 10 ; i++ )
{
cout << i << " x " << n << " = " << i* n << endl;
}
return 0 ;
}
4. 实验
Acwing 718.实验
# include
# include
using namespace std;
int main ( )
{
int n;
cin >> n;
int c = 0 , f = 0 , r = 0 ;
for ( int i= 0 ; i< n; i++ )
{
int k;
char t;
cin >> k >> t;
if ( t== 'C' ) c+= k;
else if ( t== 'F' ) f+= k;
else if ( t== 'R' ) r+= k;
}
int s = c + r + f;
printf ( "Total: %d animals\n" , s) ;
printf ( "Total coneys: %d\n" , c) ;
printf ( "Total rats: %d\n" , r) ;
printf ( "Total frogs: %d\n" , f) ;
printf ( "Percentage of coneys: %.2lf %%\n" , ( double ) c / s * 100 ) ;
printf ( "Percentage of rats: %.2lf %%\n" , ( double ) r / s * 100 ) ;
printf ( "Percentage of frogs: %.2lf %%\n" , ( double ) f / s * 100 ) ;
return 0 ;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
5. 区间2
Acwing 713.区间2
# include
using namespace std;
int main ( )
{
int n;
cin >> n;
int x = 0 , y = 0 ;
while ( n-- )
{
int t;
cin >> t;
if ( t>= 10 && t<= 20 ) x ++ ;
else y++ ;
}
printf ( "%d in\n%d out" , x, y) ;
return 0 ;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
6. 连续奇数的和2
Acwing 连续奇数的和2
# include
# include
using namespace std;
int main ( )
{
int n;
cin >> n;
while ( n-- )
{
int a, b;
cin >> a >> b;
if ( a > b) swap ( a, b) ;
int s = 0 ;
for ( int i = a + 1 ; i < b; i ++ )
if ( i % 2 )
s += i;
cout << s << endl;
}
return 0 ;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
7. 简单斐波那契
Acwing 717.简单斐波那契
# include
using namespace std;
int main ( )
{
int n;
cin >> n;
int a = 0 , b = 1 ;
for ( int i = 0 ; i < n; i ++ )
{
cout << a << ' ' ;
int c = a + b;
a = b;
b = c;
}
cout << endl;
return 0 ;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
8. 数字序列和它的和
while(cin >> a >> b,a > 0 && b > 0)
Acwing 722.数字序列和它的和
# include
using namespace std;
int main ( )
{
int a, b;
while ( cin >> a >> b, a > 0 && b > 0 )
{
if ( a > b) swap ( a, b) ;
int sum = 0 ;
for ( int i = a; i<= b; i++ )
{
sum += i;
cout << i << ' ' ;
}
cout<< "Sum=" << sum<< endl;
}
return 0 ;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
8. 完全数
Acwing 725. 完全数
# include
# include
using namespace std;
int main ( )
{
int n;
cin >> n;
while ( n -- )
{
int x;
cin >> x;
int s = 0 ;
for ( int i = 1 ; i * i <= x; i ++ )
if ( x % i == 0 )
{
if ( i < x) s += i;
if ( i != x / i && x / i < x) s += x / i;
}
if ( s == x) printf ( "%d is perfect\n" , x) ;
else printf ( "%d is not perfect\n" , x) ;
}
return 0 ;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
9. 质数
Acwing 726.质数
# include
# include
using namespace std;
int main ( )
{
int n;
cin >> n;
while ( n-- )
{
int a;
cin >> a;
bool is_prime = true ;
for ( int i = 2 ; i* i <= a; i++ )
{
if ( a % i == 0 )
{
is_prime = false ;
break ;
}
}
if ( is_prime)
cout<< a << " is prime" << endl;
else
cout<< a << " is not prime" << endl;
}
return 0 ;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
10. 菱形(难点)
Acwing 727.菱形
可以使用曼哈顿距离 n = 5时,图形如下
# include
# include
using namespace std;
int main ( )
{
int n;
cin >> n;
int a = n/ 2 , b = n/ 2 ;
for ( int i = 0 ; i < n; i ++ )
{
for ( int j = 0 ; j < n; j ++ )
{
if ( abs ( i- a) + abs ( j- b) <= n/ 2 )
cout << "*" ;
else
cout << ' ' ;
}
cout<< endl;
}
return 0 ;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23