蓝桥杯\2015-省赛-C语言大学A组 题解

  1. 用excel算日期
  2. 还可以用{ }括住一行代码。。。

A方程整数解

#include<iostream>
using namespace std;
int main(){
	int a=1;
	int b=1;int c= 1;
	for(a=1;a<31;a++){
		for(b=1;b<31;b++){
			for(c=1;c<31;c++){
				if(a*a+b*b+c*c==1000)cout<<a<<' '<<b<<' '<<c<<endl;
			}
		}
	}

}

6 8 30
6 30 8
8 6 30
8 30 6
10 18 24 //就填10
10 24 18
18 10 24
18 24 10
24 10 18
24 18 10
30 6 8
30 8 6
为什么不能是从0,甚至是负数?。

B星系炸弹

丑的人瞎算,帅的人excel,一拖就出来了
2017-08-05
注意格式,拖到1001

C奇妙的数字

#include<cstdio>
#include<iostream>
#include<sstream>
#include<cstring>
using namespace std;
int A[200];
int main(){
	stringstream ss;
	string s;
	for(int i =1 ;i<1000;i++){
		memset(A,0,sizeof(A));
		ss.clear();
		ss.str("");
		ss<<i*i<<i*i*i;
		ss>>s;
		int cnt=0;
		for(int i = 0 ;i<(int)s.size();i++){
			if(!A[(int )s[i]])cnt+=++A[(int)s[i]];
			else break;
		}
		if(cnt==10){
			cout<<s<<' ';
			cout<<i<<endl;
		}
	}
}

4761328509 69

C格子中输出

#include <stdio.h>
#include <string.h>

void StringInGrid(int width, int height, const char* s)
{
	int i,k;
	char buf[1000];
	strcpy(buf, s);
	if(strlen(s)>width-2) buf[width-2]=0;
	
	printf("+");
	for(i=0;i<width-2;i++) printf("-");
	printf("+\n");
	
	for(k=1; k<(height-1)/2;k++){
		printf("|");
		for(i=0;i<width-2;i++) printf(" ");
		printf("|\n");
	}
	
	printf("|");
	
	printf("%*s%s%*s",(width-2-strlen(buf))/2,"", buf, (width-2-strlen(buf))/2+(width-2-strlen(buf))%2,"");  //填空
	          //要注意偏左,保证和是width.这里的用法 数字加空字符串占位的方法算是新思路,还有%*s,后置长度
	printf("|\n");
	
	for(k=(height-1)/2+1; k<height-1; k++){
		printf("|");
		for(i=0;i<width-2;i++) printf(" ");
		printf("|\n");
	}	
	
	printf("+");
	for(i=0;i<width-2;i++) printf("-");
	printf("+\n");	
}

int main()
{
	StringInGrid(20,6,"abcd1234");
	return 0;
}

D九数组分数

#include <stdio.h>

void test(int x[])
{
	int a = x[0]*1000 + x[1]*100 + x[2]*10 + x[3];
	int b = x[4]*10000 + x[5]*1000 + x[6]*100 + x[7]*10 + x[8];
	
	if(a*3==b) printf("%d / %d\n", a, b);
}

void f(int x[], int k)
{
	int i,t;
	if(k>=9){
		test(x);
		return;
	}
	
	for(i=k; i<9; i++){
		{t=x[k]; x[k]=x[i]; x[i]=t;}
		f(x,k+1);
		{t=x[k]; x[k]=x[i]; x[i]=t;}// 填空处
	}
}
	
int main()
{
	int x[] = {1,2,3,4,5,6,7,8,9};
	f(x,0);	
	return 0;
}

5832 / 17496
5823 / 17469
直接复制。
担心字典序的问题,据说是考查回溯法,使用要还原状态。这是字典序?我怎么没看出来
123456789
123456798
123456879
123456897
看出来了,最初i=k是无效交换

牌型种数

int cnt= 0 ;
void dfs(int dep, int sum){
	if(sum>13)return ;
	else {
		if(dep==14){
			if(sum==13)cnt++;
		}
		else {
			for(int i = 0 ; i < 5; i++){
				dfs(dep+1,sum+i);
			}
		}
	}
}
void sol(){
	cout<<13*13*13*13*13<<endl;
	dfs(1,0);
	cout<<cnt<<endl;
}
int main()
{
	sol();
	return 0;
}

3598180

手链样式

1170
模拟翻转等操作,用set来计数,建议计数使用string或者状态压缩成一个整数,偷懒用vector也行,但type要有比较运算符。因为需要set比较运算符

饮料换购

#include<cstdio>
int main(){
	int n;
	int ans=0;
	scanf("%d", &n);
	ans+=n;
	while(1){
		ans+=n/3;
		if(!(n/3))break;
		n=n/3+n%3;
	}
	printf("%d\n", ans);
	
}

垒骰子

dp加矩阵快速幂,蓝桥杯/2015/初赛(省赛)/C语言A组/9/垒骰子

posted @ 2017-04-04 15:57  xsthunder  阅读(490)  评论(0编辑  收藏  举报