30,228
社区成员




1221是一个非常特殊的数,它从左边读和从右边读是一样的
#include<iostream>
using namespace std;
int main()
{
for(int i = 1000; i < 10000; i ++){
int a = 0 ,b = i;
while(b){
a = a * 10 + b % 10;
b /= 10;
}
if(a == i) cout << i << endl;
}
return 0;
}
要求数字要满足三个条件:
#include<iostream>
using namespace std;
int main()
{
int n;
//scanf("%d",&n);
cin >> n;
for(int i = 10000; i < 1000000; i ++){
//判断是否满足各位数相加等于n
int a = i;
int sum = 0;
while(a){
sum += a % 10;
a /= 10;
}
//再判断是否是回文数
if(sum == n){
int b = i;
int tmp = 0;
while(b){
tmp = tmp * 10 + b % 10;
b /= 10;
}
if(tmp == i ){
cout << i << endl;
}
}
}
return 0;
}
题目描述:
153是一个非常特殊的数,它等于它的每位数字的立方和,即153=1*1*1+5*5*5+3*3*3。编程求所有满足这种条件的三位十进制数。
AC代码:
#include<bits/stdc++.h>
using namespace std;
bool method(int x)
{
int tmp = x;
int s = 0;
while(tmp){
int a = tmp % 10;
s += a * a * a;
tmp /= 10;
}
if(s == x) return true;
else return false;
}
int main()
{
for(int i = 100; i < 1000; i ++)
{
if(method(i)){
cout << i << endl;
}
}
return 0;
}
题目:
杨辉三角形又称Pascal三角形,它的第i+1行是(a+b)i的展开式的系数。
它的一个重要性质是:三角形中的每个数字等于它两肩上的数字相加。
下面给出了杨辉三角形的前4行:
1
1 1
1 2 1
1 3 3 1
给出n,输出它的前n行。
#include<bits/stdc++.h>
using namespace std;
const int N = 35;
int a[N][N];
int main()
{
int n;
cin >> n;
a[0][0] = 1;
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= i; j ++){
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
cout << a[i][j] << " ";
}
cout << endl;
}
return 0;
}
题目:
给出一个包含n个整数的数列,问整数a在数列中的第一次出现是第几个。
输入格式
第一行包含一个整数n。
第二行包含n个非负整数,为给定的数列,数列中的每个数都不大于10000。
第三行包含一个整数a,为待查找的数。
思路就是简单的遍历.
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int N = 1001;
int a[N];
int main()
{
int n, x, j = -1;
cin >> n;
for(int i = 1; i <= n; i ++){
cin >> a[i];
}
cin >> x;
for(int i = 1; i <= n; i ++){
if(a[i] == x){
cout << i << endl;
j = i;
break;
}
}
if(j == -1) cout << j << endl;
return 0;
}
题目:
给出n个数,找出这n个数的最大值,最小值,和。
AC代码
#include<bits/stdc++.h>
using namespace std;
const int N = 10001;
int a[N];
int main()
{
int n,min = 1e5 + 10, max = -100002, sum = 0;
cin >> n;
for(int i = 0; i < n; i ++){
cin >> a[i];
if(a[i] > max) max = a[i];
if(a[i] < min) min = a[i];
sum += a[i];
}
cout << max << endl;
cout << min << endl;
cout << sum << endl;
return 0;
}
题目:
利用字母可以组成一些美丽的图形,下面给出了一个例子:
ABCDEFG
BABCDEF
CBABCDE
DCBABCD
EDCBABC
这是一个5行7列的图形,请找出这个图形的规律,并输出一个n行m列的图形。
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int N = 27;
char a[N];
int main()
{
int n, m;
cin >> n >> m;
for(int i = 0; i < n; i ++){//输出n行
for(int j = 0; j < m; j ++){//每行有m个字母
cout <<(char) ('A' + abs(i - j));//这里是重点
}
cout << endl;
}
return 0;
}
感觉自己还是太死板了,想法不灵活.
题目:
对于长度为5位的一个01串,每一位都可能是0或1,一共有32种可能。它们的前几个是:
00000
00001
00010
00011
00100
请按从小到大的顺序输出这32种01串。
AC代码:(写的也挺离谱的,但好理解)
#include<bits/stdc++.h>
using namespace std;
bool f1, f2, f3, f4, f5;
int main()
{
for(int i = 0; i < 2; i ++){
for(int j = 0; j < 2; j ++){
for(int m = 0; m < 2; m ++){
for(int n = 0; n < 2; n ++){
for(int g = 0; g < 2; g ++){
cout << (int) f1 << (int) f2 << (int) f3 << (int) f4 << (int) f5 << endl;
f5 = !f5;
}
f4 = !f4;
}
f3 = !f3;
}
f2 = !f2;
}
f1 = !f1;
}
return 0;
}
题目:
给定一个年份,判断这一年是不是闰年。
当以下情况之一满足时,这一年是闰年:
1. 年份是4的倍数而不是100的倍数;
2. 年份是400的倍数。
其他的年份都不是闰年。
AC代码:(简单实现)
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
if(n % 100){//不是100的倍数
if(n % 4) cout << "no" << endl;
else cout << "yes" << endl;
}else{//100的倍数
if(n % 400) cout << "no" <<endl;
else cout << "yes" << endl;
}
return 0;
}
题目:
Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1。
当n比较大时,Fn也非常大,现在我们想知道,Fn除以10007的余数是多少。
AC代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
/*
Fn = Fn-1 + Fn-2;
*/
long long fn = 1, fn1 = 1, fn2 = 1;
for(int i = 3; i <= n; i ++){//从3开始循环
fn = fn1 + fn2;
//注意这里的取模,要不数值过大导致计算出错
fn1 = fn2 % 10007;
fn2 = fn % 10007;
}
cout << fn % 10007 << endl;
return 0;
}
11.圆的面积
要求四舍五入保留七位小数fixed << setprecision(7)
AC代码
#include<bits/stdc++.h>
using namespace std;
const double PI = 3.14159265358979323;
int main()
{
int x;
cin >> x;
cout << fixed << setprecision(7) << x * x * PI << endl;
return 0;
}
12.数列求和
注意数据的范围:数据规模与约定:1 <= n <= 1,000,000,000
int类型很容易超限
AC代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long n;
cin >> n;
cout << (long long )(1 + n) * n / 2 << endl;
return 0;
}
文末总结:思路太少(刷题太少),想法不灵活,代码不够细节,总是出现小错误,常用算法不熟练