题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
#include<bits/stdc++.h>
using namespace std;
int main()
{
for(int i=1;i<=100000;i++)
{
int x,y;
x=sqrt(i+100);
y=sqrt(i+168);
if((x*x==(i+100))&&(y*y==(i+168)))
{
cout<<i<<endl;
}
}
}
题目:输入某年某月某日,判断这一天是这一年的第几天?
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a1[12]={31,28,31,30,31,30,31,31,30,31,30,31}; //这里是12;
int a2[12]={31,29,31,30,31,30,31,31,30,31,30,31};
int year,month,day;
int sum=0;
cin>>year>>month >>day;
if((year%400==0)||(year%4==0&&year%100!=0))
{
for(int i=0;i<month;i++)
{
sum+=a2[i];
}
}
else{
for(int i=0;i<month;i++)
{
sum+=a1[i];
}
}
sum=sum+day;
cout<<sum;
}
个数,从小到大输出。
题目:输入三个整数x,y,z,请把这三个数由小到大输出。
//运用swap函数
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b,c;
cin>>a>>b>>c;
if(a>b)
{
swap(a,b);
}
if(a>c)
{
swap(a,c);
}
if(b>c)
{
swap(b,c);
}
cout<<a<<"\t"<<b<<"\t"<<c;
}
题目:将一个正整数分解质因数。例如:输入90,打印出90=233*5
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,t;
cin>>n;
t=n;
int j=0;
int i,a[1001];
for(i=2;i<=n;i++)
{
while(n%i==0)
{
a[j]=i;
j++;
n=n/i;
}
}
cout<<t<<"=";
for(i=0;i<j-1;i++)
{
cout<<a[i]<<"*";
}
cout<<a[j-1];
}
两个乒乓球队进行比赛
题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定
比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出
三队赛手的名单。
#include<bits/stdc++.h>
using namespace std;
int main()
{
char a,b,c;
for(a='x';a<='z';a++)
{
for(b='x';b<='z';b++)
{
if(b!=a)
{
for(c='x';c<='z';c++)
{
if(c!=a&&c!=b)
{
if(a!='x'&&c!='x'&&c!='z')
{
cout<<"a---"<<a<<endl;
cout<<"b---"<<b<<endl;
cout<<"c---"<<c<<endl;
}
}
}
}
}
}
return 0;
}
猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个。第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。
//递归实现
#include<bits/stdc++.h>
using namespace std;
int fun(int day)
{
if(day==10)
return 1;
else
return (fun(day+1)+1)*2;
}
int main()
{
cout<<fun(1);
}
一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?
#include<bits/stdc++.h>
using namespace std;
int main()
{
double sum=100;
double h=100;
int n;
cin>>n;
for(int i=1;i<=n-1;i++)
{
h=h/2.0;
sum+=h*2.0;
}
h=h/2;
cout<<"共经过了"<<sum<<"米"<<endl;
cout<<"第"<<n<<"次反弹的高度为:"<<h<<endl;
}
一个数如果恰好等于它的因子之和,这个数就称为“完数”。例如6=1+2+3,找出1000以内的所有完数
#include<bits/stdc++.h>
using namespace std;
int main()
{
int i,j;
for(i=1;i<=1000;i++)
{
int k=0,sum=0;
int t=i;
for(j=1;j<i;j++) //j<i,不要写成j<=i;
{
if(i%j==0)
{
sum+=j;
}
}
if(sum==t)
{
cout<<t<<endl;
}
}
}
求s=a+aa+aaa+aaaa+aa…a的值
题目:求s=a+aa+aaa+aaaa+aa…a的值,其中a是一个数字。例如2+22+222+2222+22222(此时)
//相同位数的数可以通过乘以10相加来实现
#include<bits/stdc++.h>
using namespace std;
int main()
{
int sum=0;
int a,n;
cin>>a>>n;
sum=a;
int t=a;
for(int i=1;i<=n-1;i++)
{
a=10*a+t;
sum+=a;
}
cout<<sum;
}
输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数
// getline 读入每一行字符串
//int main()
{
string line
while(getline(cin,line))
cout<<line<<endl;
return 0;
}
//在不设置的情况下系统默认该字符为’\n’,也就是回车换行符(遇到回车停止读入)
#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;
int main()
{
string s;
getline(cin, s);
int digit = 0, space = 0, letter = 0, other = 0;
for (int i = 0; i < s.size(); i++)
{
if (s[i] == ' ')
space++;
else if (s[i] >= '1'&&s[i] <= '9')
digit++;
else if (s[i] >= 'a'&&s[i] <= 'z' || s[i] >= 'A'&&s[i] <= 'Z')
letter++;
else
other++;
}
cout << "数字:" << digit << endl;
cout << "字母:" << letter << endl;
cout << "空格:" << space << endl;
cout << "其他:" << other << endl;
return 0;
}
输出一个菱形
#include<bits/stdc++.h>
using namespace std;
int main()
{
int i,j;
for(i=1;i<=4;i++)
{
for(j=1;j<=4-i;j++)
{
cout<<" ";
}
for(j=1;j<=2*i-1;j++)
{
cout<<"*";
}
cout<<endl;
}
for( i=1;i<=3;i++)
{
for( j=1;j<=i;j++)
{
cout<<" ";
}
int t=2*(3-i)+1;
while(t)
{
cout<<"*";
t--;
}
cout<<endl;
}
}
求数列2/1,3/2,5/3,8/5,13/8,21/13,…前20项和Sn
#include<bits/stdc++.h>
using namespace std;
int main()
{
double sum=0;
double f1=2.0,f2=1.0,t=0;
for(int i=1;i<=20;i++)
{
//要用到 中间变量t否则其中一个值会改变
sum+=f1/f2;
t=f1;
f1=f1+f2;
f2=t;
}
cout<<sum<<endl;
}
题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第
3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后
问第一个人,他说是10岁。请问第五个人多大?
#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;
int fun(int n)
{
if (n == 1)
return 10;
else
return fun(n - 1) + 2;
}
int main()
{
cout << fun(5) << endl;
return 0;
}
题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字
//直接取,然后输出
#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;
void fun(int n)
{
int cnt = 0;
while (n)
{
cout << n % 10 << " ";
cnt++;
n = n / 10;
}
cout << "位数:" << cnt << endl;
}
int main()
{
int n;
cin >> n;
fun(n);
return 0;
}
题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。
#include<bits/stdc++.h>
using namespace std;
bool fun(char a[])
{
int n = strlen(a);
for (int i = 0; i < n / 2; i++)
{
if (a[i] != a[n - i - 1])
return false;
}
return true;
}
int main()
{
char a[20];
cin >> a;
if (fun(a))
cout << "是回文!" << endl;
else
cout << "不是回文!" << endl;
return 0;
}