《算法笔记》3.1小节——入门模拟->简单模拟

    

问题 B: A+B

时间限制: 1 Sec   内存限制: 32 MB
提交: 530   解决: 329
[ 提交][ 状态][ 讨论版][命题人: 外部导入]

题目描述

给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开。
现在请计算A+B的结果,并以正常形式输出。

输入

输入包含多组数据数据,每组数据占一行,由两个整数A和B组成(-10^9 < A,B < 10^9)。

输出

请计算A+B的结果,并以正常形式输出,每组数据占一行。

样例输入

-234,567,890 123,456,789
1,234 2,345,678

样例输出

-111111101
2346912

    思路是按字符格式输入,只留下[48,57]即['0','9']的字符,char转int用ca[i]-'0',然后判断正负。

#include<iostream> 
#include<string.h>
using namespace std;
//过滤所有非数字字符 注意(ca[i]-'0')char转int 得到完整数字后判断再判断符号 算过了9个010位+3个‘,’+±一共15位,其实用20就行。
int main(){
	int a,b,i,s=0;
	char ca[15]={},cb[15]={};
	while(scanf("%s%s",ca,cb)!=EOF)
	{
		a=0;
		for(i=0;i<strlen(ca);i++)
		{
			if(ca[i]>='0'&&ca[i]<='9')
			{
				a=a*10+ca[i]-'0';
			}
		}
		if(ca[0]=='-')
			a=-a;
	
		b=0;
		for(i=0;i<strlen(cb);i++)
		{
			if(cb[i]>='0'&&cb[i]<='9')
			{
				b=b*10+cb[i]-'0';
			}
		}
		if(cb[0]=='-')
			b=-b;

		printf("%d\n",a+b);
	}
	return 0;
}


问题 C: 特殊乘法

时间限制: 1 Sec   内存限制: 32 MB
提交: 404   解决: 300
[ 提交][ 状态][ 讨论版][命题人: 外部导入]

题目描述

写个算法,对2个小于1000000000的输入,求结果。特殊乘法举例:123 * 45 = 1*4 +1*5 +2*4 +2*5 +3*4+3*5

输入

 两个小于1000000000的数

输出

 输入可能有多组数据,对于每一组数据,输出Input中的两个数按照题目要求的方法进行运算后得到的结果。

样例输入

24 65
42 66666
3 67

样例输出

66
180
39
    这题没有考虑负数,难道是样例输入暗示了?反正我时间不多了,这题就直接上了。
#include<iostream> 
#include<string.h>
using namespace std;
//looks like didn't consider negative number the sample indicate it?
int main(){
	int a,b,i,j,sum=0;
	char ca[20]={},cb[20]={};
	while(scanf("%s%s",ca,cb)!=EOF)
	{
		a=0,b=0,sum=0;
		for(i=0;i<strlen(ca);i++)
		{
			for(j=0;j<strlen(cb);j++)
			{
				sum+=(ca[i]-'0')*(cb[j]-'0');
			}
		}

		printf("%d\n",sum);
	}
	return 0;
}


问题 D: 比较奇偶数个数

时间限制: 1 Sec   内存限制: 32 MB
提交: 404   解决: 281
[ 提交][ 状态][ 讨论版][命题人: 外部导入]

题目描述

第一行输入一个数,为n,第二行输入n个数,这n个数中,如果偶数比奇数多,输出NO,否则输出YES。

输入


输入有多组数据。
每组输入n,然后输入n个整数(1<=n<=1000)。


输出


如果偶数比奇数多,输出NO,否则输出YES。


样例输入

1
67 
7
0 69 24 78 58 62 64 

样例输出

YES
NO
    没什么好说的,以后这种题就不贴了。
#include<iostream> 
#include<string.h>
using namespace std;
//奇数 odd number 偶数even number 每个数字直接用%判定即可
int main(){
	int n,i,x,odd,even;
	while(scanf("%d",&n)!=EOF)
	{
		odd=0,even=0;
		for(i=0;i<n;i++)
		{
			scanf("%d",&x);
			if(x%2==1)
				odd++;
			else
				even++;
		}
		if(even>odd)
			printf("NO\n");
		else
			printf("YES\n");
	}
	return 0;
}


问题 E: Shortest Distance (20)

时间限制: 1 Sec   内存限制: 32 MB
提交: 624   解决: 213
[ 提交][ 状态][ 讨论版][命题人: 外部导入]

题目描述

The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits.

输入

Each input file contains one test case. For each case, the first line contains an integer N (in [3, 10 5 ]), followed by N integer distances D 1  D 2  ... D N , where D i  is the distance between the i-th and the (i+1)-st exits, and D N  is between the N-th and the 1st exits. All the numbers in a line are separated by a space. The second line gives a positive integer M (<=10 4 ), with M lines follow, each contains a pair of exit numbers, provided that the exits are numbered from 1 to N. It is guaranteed that the total round trip distance is no more than 10 7 .

输出

For each test case, print your results in M lines, each contains the shortest distance between the corresponding given pair of exits.

样例输入

5 1 2 4 14 9
3
1 3
2 5
4 1

样例输出

3
10
7
    这个题挺有意思的,开始我是计算从start到end每段链路的距离和,后来提示超时25%,我还想是不是start和end太远了应该判断一下是否超过n/2,后来不行,想明白了这不能解决根本问题,应该把数组中的每个元素存放到D1的距离,这样直接a[end]-a[start]就行,不用循环了,算是运用到了之前学过的运筹学的知识吧。
#include<iostream> 
#include<string.h>
using namespace std;
//运筹学知识吧。
int main(){
	int n,i,m,start,end,l1,l2,t,d;
	while(scanf("%d",&n)!=EOF)
	{
		int a[100002]={};
		a[1]=0;
		for(i=2;i<=n+1;i++)//a[n+1] is total
		{
			scanf("%d",&d);//to the next point distance
			a[i]=a[i-1]+d;
		}

		scanf("%d",&m);
		for(i=0;i<m;i++)
		{
			scanf("%d%d",&start,&end);
			if(start>end)
			{
				t=start;
				start=end;
				end=t;
			}
			l1=a[end]-a[start];
			l2=a[n+1]-l1;
			
			if(l1>=l2)
				printf("%d\n",l2);
			else
				printf("%d\n",l1);
		}
	}
	return 0;
}


6128 F A+B和C

注意long long输出是%lld就行


问题 G: 数字分类 (20)

时间限制: 1 Sec   内存限制: 32 MB
提交: 679   解决: 223
[ 提交][ 状态][ 讨论版][命题人: 外部导入]

题目描述

给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字:

A1 = 能被5整除的数字中所有偶数的和;A2 = 将被5除后余1的数字按给出顺序进行交错求和,即计算n1-n2+n3-n4...;A3 = 被5除后余2的数字的个数;A4 = 被5除后余3的数字的平均数,精确到小数点后1位;A5 = 被5除后余4的数字中最大数字。

输入

每个输入包含1个测试用例。每个测试用例先给出一个不超过1000的正整数N,随后给出N个不超过1000的待分类的正整数。数字间以空格分隔。

输出

对给定的N个正整数,按题目要求计算A1~A5并在一行中顺序输出。数字间以空格分隔,但行末不得有多余空格。

若其中某一类数字不存在,则在相应位置输出“N”。

样例输入

13 1 2 3 4 5 6 7 8 9 10 20 16 18
8 1 2 4 5 6 7 9 16

样例输出

30 11 2 9.7 9
N 11 2 N 9

注意除数不能为0,按照题目要求判断就ok,
#include<iostream> 
#include<string.h>
using namespace std;
//pay attention to ll is %lld and denominator cann't be 0; 别作死就行
int main(){
	int n,x,i,a1,a2,a3,a4,a5,flg,count2,count4;
	while(scanf("%d",&n)!=EOF)
	{
		a1=a2=a3=a4=a5=flg=count2=count4=0;
		for(i=0;i<n;i++)
		{
			scanf("%d",&x);
			if(x%5==0&&x%2==0)
				a1+=x;
			if(x%5==1)
			{
				count2++;
				flg=1-flg;
				if(flg==1)
					a2+=x;
				else
					a2-=x;
			}
			if(x%5==2)
			{
				a3++;
			}
			if(x%5==3)
			{
				a4+=x;
				count4++;
			}
			if(x%5==4&&x>a5)
			{
				a5=x;
			}
		}
		if(a1==0)
			printf("N ");
		else
			printf("%d ",a1);
		if(count2==0)
			printf("N ");
		else
			printf("%d ",a2);
		if(a3==0)
			printf("N ");
		else
			printf("%d ",a3);
		if(a4==0)
			printf("N ");
		else
			printf("%.1f ",(double)a4/count4);
		if(a5==0)
			printf("N");
		else
			printf("%d",a5);
		printf("\n");
	}
	return 0;
}


问题 H: 部分A+B (15)

时间限制: 1 Sec   内存限制: 32 MB
提交: 274   解决: 220
[ 提交][ 状态][ 讨论版][命题人: 外部导入]

题目描述

正整数A的“DA(为1位整数)部分”定义为由A中所有DA组成的新整数PA。例如:给定A = 3862767,DA = 6,则A的“6部分”PA是66,因为A中有2个6。

现给定A、DA、B、DB,请编写程序计算PA + PB

输入

输入在一行中依次给出A、D A 、B、D B ,中间以空格分隔,其中0 < A, B < 10 10

输出

在一行中输出P A  + P B 的值。

样例输入

3862767 6 13530293 3
3862767 1 13530293 8

样例输出

399
0
用%得余数判定用/迭代下一个数就行。
#include<iostream> 
#include<string.h>
using namespace std;
//%lld
int main(){
	long long a,da,b,db,pa,pb;
	while(scanf("%lld%lld%lld%lld",&a,&da,&b,&db)!=EOF)
	{
		pa=pb=0;
		while(a!=0)
		{
			if(a%10==da)
			{
				pa=pa*10+da;
			}
			a=a/10;
		}
		while(b!=0)
		{
			if(b%10==db)
			{
				pb=pb*10+db;
			}
			b=b/10;
		}
		printf("%d\n",pa+pb);
	}
	return 0;
}


问题 I: 锤子剪刀布 (20)

时间限制: 1 Sec   内存限制: 32 MB
提交: 378   解决: 183
[ 提交][ 状态][ 讨论版][命题人: 外部导入]

题目描述

大家应该都会玩“锤子剪刀布”的游戏:两人同时给出手势,胜负规则如图所示:


现给出两人的交锋记录,请统计双方的胜、平、负次数,并且给出双方分别出什么手势的胜算最大。


输入

输入第1行给出正整数N(<=10 5 ),即双方交锋的次数。随后N行,每行给出一次交锋的信息,即甲、乙双方同时给出的的手势。C代表“锤子”、J代表“剪刀”、B代表“布”,第1个字母代表甲方,第2个代表乙方,中间有1个空格。

输出

输出第1、2行分别给出甲、乙的胜、平、负次数,数字间以1个空格分隔。第3行给出两个字母,分别代表甲、乙获胜次数最多的手势,中间有1个空格。如果解不唯一,则输出按字母序最小的解。

样例输入

10
C J
J B
C B
B B
B C
C C
C B
J B
B C
J J

样例输出

5 3 2
2 3 5
B B

注意getchar()的用法,会把所有符号读入,注意case后面是常量表达式,石头剪刀布除去平局外是A32共6种结果,恰好值不同,因此可以简略成如下。
#include<iostream> 
#include<string.h>
using namespace std;
//the use of getchar() '\n' ' '
int main(){
	int a,b,n,wa,wb,d,cb,cc,cj,bb,bc,bj;
	while(scanf("%d",&n)!=EOF)
	{
		wa=wb=d=cb=cc=cj=bb=bc=bj=0;
		while(n--)
		{
			getchar();
			a=getchar();
			getchar();
			b=getchar();
			switch(a-b){//CONST expr

				case 'B'-'C':wa++;cb++;break;
				case 'C'-'J':wa++;cc++;break;
				case 'J'-'B':wa++;cj++;break;
				case 'C'-'B':wb++;bb++;break;
				case 'J'-'C':wb++;bc++;break;
				case 'B'-'J':wb++;bj++;break;
				case 0:d++;break;
				default:break;
			}
		}

		printf("%d %d %d\n",wa,d,wb);
		printf("%d %d %d\n",wb,d,wa);
			if(cb>=cc&&cb>=cj)//three meta get the max
			{
				printf("B ");
			}
			else if(cc>=cb&&cc>=cj)
				printf("C ");
			else if(cj>=cb&&cj>=cc)
				printf("J ");

			if(bb>=bc&&bb>=bj)//three meta get the max
			{
				printf("B\n");
			}
			else if(bc>=bb&&bc>=bj)
				printf("C\n");
			else if(bj>=bb&&bj>=bc)
				printf("J\n");
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值