AcWing 717. 简单斐波那契
#include <iostream>
#include <cstring>
#include <algorithm>
typedef long long ll;
int main()
{
int n;
ll a = 0,b = 1,c;
scanf("%d", &n);
while (n -- ){
printf("%lld ",a);
c = a + b;
a = b;
b = c;
}
return 0;
}
AcWing 722. 数字序列和它的和
#include <bits/stdc++.h>
using namespace std;
int m, n, s;
int main() {
while (cin >> m >> n) {
if (n < 1 || m < 1)
break;
s = 0;
if (m > n)
swap(n, m);
for (int i = m; i <= n; i++) {
cout << i << ' ';
s += i;
}
cout << "Sum=" << s << endl;
}
}
AcWing 725. 完全数
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main(){
int m;
scanf("%d",&m);
while(m--){
int n;
scanf("%d",&n);
int sum = 0;
for(int i=1;i*i<=n;i++)//对于n来说,若存在任意一个除数i,就一定存在n/i;假设i大,则i^2<=n
{
if(n%i==0)
{
if(i<n) sum+=i;
if(i !=n/i && n/i<n) sum+=n/i;
//因为枚举少了一半,这里就要加回来。但若出现i=n/i,其实上一步就已经加了
}
}
if(sum==n) printf("%d is perfect\n",n);
else printf("%d is not perfect\n",n);
}
return 0;
}
AcWing 726. 质数
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
int N;
cin >> N;
while (N -- ){
int flag = 0,a= 0;
scanf("%d", &a);
for (int i = 2; i <= sqrt(a); i ++ ){
if(a % i == 0) {flag = 1;break;}
}
if(flag == 0) printf("%d is prime\n",a);
else
printf("%d is not prime\n",a);
}
return 0;
}
AcWing 727. 菱形
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n / 2 + 1; i ++ ){
for (int j = 1; j <= n / 2 + 1 - i; j ++ ){
printf(" ");
}
for (int j = 1; j <= 2*i-1; j ++ ){
printf("*");
}
printf("\n");
}
for (int i = 1; i <= n / 2; i ++ ){
for (int j = 1; j <= i; j ++ ){
printf(" ");
}
for (int j = 1; j <= (n/2-i)*2+1; j ++ ){
printf("*");
}
printf("\n");
}
return 0;
}