【openjudge 计算概论(A)】[基础编程练习(控制成分)]

1:找和为K的两个元素

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[1010],n,k;
int main()
{
	int i,j;
	scanf("%d%d",&n,&k);
	for(i=1;i<=n;++i)scanf("%d",&a[i]);
	for(i=1;i<n;++i)
	 for(j=i+1;j<=n;++j)
	  {
	  	int x=a[i]+a[j];
	  	if(x==k) {printf("yes\n"); return 0;}
	  }
	printf("no\n");
	return 0;
}

2:求满足条件的3位数

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,ans;
int main()
{
	scanf("%d",&n);
	int i=10;
	while(n)
	 {
	 	ans=i*i;
	 	int sum=ans;
	 	int x=sum/100; sum%=100;
	 	int y=sum/10,z=sum%10;
	 	if(x==y||x==z||y==z) n--;
	 	i++;
	 }
	printf("%d\n",ans);
	return 0;
}

3:简单算术表达式求值(运算符前后可能有空格)

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char ch[10];
int a,b,tt;
int main()
{
	do{
		ch[++tt]=getchar();
		if(ch[tt]==' ') tt--;
	}while(ch[tt]!='\n');
	if(ch[3]=='+')
	 {
	 	a=(ch[1]-'0')*10+ch[2]-'0';
	 	b=(ch[4]-'0')*10+ch[5]-'0';
	 	printf("%d\n",a+b);
	 	return 0;
	 }
	if(ch[3]=='-')
	 {
	 	a=(ch[1]-'0')*10+ch[2]-'0';
	 	b=(ch[4]-'0')*10+ch[5]-'0';
	 	printf("%d\n",a-b);
	 	return 0;
	 }
	if(ch[3]=='*')
	 {
	 	a=(ch[1]-'0')*10+ch[2]-'0';
	 	b=(ch[4]-'0')*10+ch[5]-'0';
	 	printf("%d\n",a*b);
	 	return 0;
	 }
	if(ch[3]=='/')
	 {
	 	a=(ch[1]-'0')*10+ch[2]-'0';
	 	b=(ch[4]-'0')*10+ch[5]-'0';
	 	printf("%d\n",a/b);
	 	return 0;
	 }
	if(ch[3]=='%')
	 {
	 	a=(ch[1]-'0')*10+ch[2]-'0';
	 	b=(ch[4]-'0')*10+ch[5]-'0';
	 	printf("%d\n",a%b);
	 	return 0;
	 }
}

4:完美立方

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node{
	int a,b,c,d;
}ans[500];
int tot,n;
int tmp(node x,node y)
{
	if(x.a<y.a) return 1;
	if(x.a>y.a) return 0;
	if(x.b<y.b) return 1;
	if(x.b>y.b) return 0;
	if(x.c<y.c) return 1;
	if(x.c>y.c) return 0;
	if(x.d<y.d) return 1;
	return 0;
}
int main()
{
	int i,j,k,h;
	scanf("%d",&n);
	for(h=2;h<=n;++h)
	 {
	 	int sum=h*h*h,m=min(sum,n);
	 	for(i=2;i<=m;++i)
	 	 for(j=i;j<=m;++j)
	 	  {
	 	  	int s=i*i*i+j*j*j;
	 	  	if(s>sum) break;
	 	  	for(k=j;k<=m;++k)
	 	     {
	 	   	    int ss=i*i*i+j*j*j+k*k*k;
	 	   	    if(ss==sum) {tot++; ans[tot].a=h; ans[tot].b=i; ans[tot].c=j; ans[tot].d=k; break;	}
	 	   	    if(ss>sum) break;
			}
		   }
	 }
	sort(ans+1,ans+tot+1,tmp);
	for(i=1;i<=tot;++i) printf("Cube = %d, Triple = (%d,%d,%d)\n",ans[i].a,ans[i].b,ans[i].c,ans[i].d);
	return 0;
}

5:求特殊自然数

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[5],b[5],opt;
inline bool check()
{
	for(int i=1;i<=3;++i)
	 if(a[i]!=b[i]) return 0;
	return 1;
}
int main()
{
	int i;
	scanf("%d",&opt);
	for(i=81;i<=342;++i)
	 {
	    int x=i,y=i;
	    a[1]=x%9; x/=9;
	    a[2]=x%9; x/=9;
	    a[3]=x%9; x/=9;
	    b[3]=y%7; y/=7;
	    b[2]=y%7; y/=7;
	    b[1]=y%7; y/=7;
	    if(check()) break;
	 }
	if(opt==1) printf("%d\n",i);
	if(opt==2) printf("%d%d%d\n",b[1],b[2],b[3]);
	if(opt==3) printf("%d%d%d\n",a[3],a[2],a[1]);
	return 0;
}

6:称体重(手玩)

#include<cstdio>
using namespace std;
int main()
{
	printf("s 10\n");
	printf("z 20\n");
	printf("q 40\n");
	printf("l 50\n");
	return 0;
}

7:比饭量(手玩)

#include<cstdio>
using namespace std;
int main()
{
	printf("BCA\n");
	return 0;
}

8:最长平台

#include<cstdio>
#include<cstring>
using namespace std;
int num[5010],n,maxn;
int main()
{
	int i;
	while(scanf("%d",&n)==1&&n)
	 {
	 	memset(num,0,sizeof(num));
	 	maxn=0;
	 	for(i=1;i<=n;++i)
	 	 {
	 	 	int x;
			scanf("%d",&x);
			num[x]++;
			if(num[x]>maxn) maxn=num[x];
		  }
		printf("%d\n",maxn);
	 }
	return 0;
}









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值