枚举
abc
#include<iostream>
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 ((a * 100 + b * 10 + c) + (b * 100 + c * 10 + c) == 532) {
printf("%d %d %d\n", a, b, c);
}
}
}
}
return 0;
}
反序数
#include<iostream>
using namespace std;
int reverse(int n)
{
int sum=0;
while(n)
{
sum=sum*10+n%10;
n=n/10;
}
return sum;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
printf("%d\n",reverse(n));
}
return 0;
}
对称平方数
#include<iostream>
using namespace std;
int reverse(int n) {
int sum = 0;
while (n) {
sum = sum * 10 + n % 10;
n = n / 10;
}
return sum;
}
int main() {
for (int a = 1; a < 256; a++) {
if (a * a == reverse(a*a)) {
printf("%d\n", a);
}
}
return 0;
}
模拟
图形问题
输出梯形
输入一个高度h,输出一个高为h,上底边为h的梯形。
#include <iostream>
using namespace std;
int main() {
int h;
while (scanf("%d", &h) != EOF) {
int row = h;//总计行数
int col = h + 2 * (h - 1);//总计列数
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");
}
}
}
输出三角形
#include <iostream>
using namespace std;
int main() {
int h;
while (scanf("%d", &h) != EOF) {
int row = h;//总计行数
int col = 1 + 2 * (h - 1);//总计列数
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (j < col - (1 + 2 * i)) {
printf(" ");
} else {
printf("*");
}
}
printf("\n");
}
}
}
HDU-2074-叠框
-
注意:
例如:
scanf("%c%c%c",&a,&b,&c);
输入为:
d e f
则把’d’赋予a, ’ ’ 赋予b,'e’赋予c。
只有当输入为:
def
时,才能把’d’赋于a,'e’赋予b,'f’赋予c。
如果在格式控制中加入空格作为间隔,
如:
scanf ("%c %c %c",&a,&b,&c);
则输入时各数据之间可加空格。 -
测试代码:
#include<iostream> using namespace std; int main() { int n,m; char a, b,c; bool firstcase = true; while (99) { // scanf("%d%d", &n, &m); // printf("输出:%d %d\n", n, m); // scanf("%d %c %c\n", &n, &a, &b); // printf("输出:%d %c %c\n", n, a, b); // scanf("%c%c%c",&a,&b,&c); // printf("输出:%c %c %c\n", a, b, c); } }
#include <iostream>
using namespace std;
char matrix[80][80];
int main() {
int n;
char a, b;
bool firstcase = true;
while (scanf("%d %c %c", &n, &a, &b) != EOF) {
if (firstcase) {
firstcase = false;
} else {
printf("\n");
}
for (int i = 0; i <= n / 2; i++) {
int j = n - 1 - i;//右下角顶点坐标(j,j)
int length = n - 2 * i; //每一轮边框的长度
char c;
//确定用哪个字符,n/2为中心坐标,i为左上角坐标
if ((n / 2 - i) % 2 == 0) {
c = a;
} else {
c = b;
}
//绘制框框
for (int k = 0; k < length; k++) {
matrix[i][i + k] = c; //上面一行
matrix[i + k][i] = c; //左边一列
matrix[j][j - k] = c; //下面一行
matrix[j - k][j] = c; //右边一列
}
}
if (n > 1) {
//去除四个角
matrix[0][0] = ' ';
matrix[0][n - 1] = ' ';
matrix[n - 1][0] = ' ';
matrix[n - 1][n - 1] = ' ';
}
//输出图形
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%c", matrix[i][j]);
}
printf("\n");
}
}
return 0;
}
日期问题
今年的第几天?
#include<iostream>
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;
}
//输入
//1990 9 20
//2000 5 1
//输出
//263
//122
打印日期
题目描述:
给出年份m和一年中的第n天,算出第n天是几月几号。
#include<iostream>
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;
int number;
while (scanf("%d%d",&year,&number)!=EOF)
{
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);
// printf("%4d-%2d-%2d\n",year,month,day);
}
return 0;
}
其他模拟
手机键盘
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | 2 | 3 | 1 | 2 | 3 | 1 | 2 | 3 | 1 | 2 | 3 | 1 | 2 | 3 |
P | Q | R | S | T | U | V | W | X | Y | Z | ||||
1 | 2 | 3 | 4 | 1 | 2 | 3 | 1 | 2 | 3 | 4 |
#include<iostream>
using namespace std;
//26个字母需要按键的次数
int keytab[26] = {
1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3
, 1, 2, 3, 4, 1, 2, 3, 1, 2, 3, 4
};
int main() {
string str;
while(cin>>str){
int time=0;
for(int i=0;i<str.size();i++){
time+=keytab[str[i]-'a'];
//判断连续两次按键是否在同一个键盘上,如果是要停顿两秒
if(i!=0&&(str[i]-str[i-1])==(keytab[str[i]-'a']-keytab[str[i-1]-'a']))
{
time+=2;
}
}
printf("%d\n",time);
}
return 0;
}