1.1枚举
1、设a,b,c均是0到9之间的数字,abc,bbc是两个三位数,而且abc+bbc=532
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
for(int a = 0;a <= 9;a++)
{
for(int b = 0;b <= 9;b++)
{
for(int c = 0;c <= 9;c++)
{
if(100*a+10*b+c+100*a+10*c+c == 532)
printf("%d\n%d\n%d\n",a,b,c);
}
}
}
return 0;
2、设N是一个4位数,他的9倍恰好是其反位数(如1234的反位数是4321)求N的值。
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
for(int i = 1000;i <= 9999;i ++)
{
int a = i / 1000;
int b = i % 1000 / 100;
int c = i % 1000 % 100 / 10;
int d = i % 1000 % 100 % 10;
if(i * 9 == 1000*d + 100*c + 10*b + a )
printf("%d\n",i);
}
return 0;
}
#include<iostream>
#include<cstdio>
using namespace std;
int reverse(int x)
{
int rev = 0;
while(x != 0)
{
rev *= 10;
rev += x % 10;
x /= 10;
}
return rev;
}
int main()
{
for(int i = 1000; i <=9999; i++)
{
if(i * 9 == reverse(i))
{
printf("%d\n",i);
}
}
return 0;
}
3、打印所有不超过256,其平方具有对称性质的数。如2和11就是这样的数,因为22=4,1111=121。
#include<iostream>
#include<cstdio>
using namespace std;
int reverse(int x)
{
int rev = 0;
while(x != 0)
{
rev *= 10;
rev += x % 10;
x /= 10;
}
return rev;
}
int main()
{
for(int i = 0;i <= 256; ++i)
{
if(i*i == reverse(i*i))
{
printf("%d\n",i);
}
}
return 0;
}
1.2模拟
1、输入一个高度h,输出一个高度为h、上底边长度为h的梯形。
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int h;
while(scanf("%d",&h) != EOF)
{
int row = h;
int col = h + (h-1)*2;
for(int i = 0;i < row; i++)
{
for(int j = 0;j < col; j++)
{
if(j < col - (h+2*i))
{
printf(" ");
}
else
{
printf("*");
}
}
printf("\n");
}
}
return 0;
}
2、输入年月日,计算该天是本年第几天。
#include<iostream>
#include<cstdio>
using namespace std;
int daytab[2][13] = {{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}};
bool isleapyear(int year)
{
return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
}
int main()
{
int year,month,day;
while(scanf("%d%d%d",&year,&month,&day) != EOF)
{
int number = 0;
int row = isleapyear(year);
for(int i = 0;i < month; i++)
{
number +=daytab[row][i];
}
number +=day;
printf("%d\n",number);
}
return 0;
}
3、给出年份m和一天中的第n天,算出第n天是几月几号。
#include<iostream>
#include<cstdio>
using namespace std;
int daytab[2][13] = {{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}};
bool isleapyear(int year)
{
return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
}
int main()
{
int year,number,day,month;
while(scanf("%d%d",&year,&number) != EOF)
{
int month = 0;
int row = isleapyear(year);
while(number > daytab[row][month])
{
number -=daytab[row][month];
month++;
}
day = number;
printf("%04d-%02d-%02d\n",year,month,day);
}
return 0;
}