算法-程序设计课week1---2-剩余

I - Water1

输入一个百分制的成绩t,将其转换成对应的等级,具体转换规则如下:
90~100为A;
80~89为B;
70~79为C;
60~69为D;
0~59为E;

Input

输入数据有多组,每组占一行,由一个整数组成。

Output

对于每组输入数据,输出一行。如果输入数据不在0~100范围内,请输出一行:“Score is error!”。

Sample Input

56
67
100
123

Sample Output

E
D
A
Score is error!
Sponsor
#include <cstdio>
int main() {
	int a,n;
	//没什么好说的,不要忘记最后的 `printf("Score is error!\n");`
	while(~scanf("%d",&a)) {
		if(a>=90&&a<=100) printf("A\n");
		else if(a>=80&&a<=89) printf("B\n");
		else if(a>=70&&a<=79) printf("C\n");
		else if(a>=60&&a<=69) printf("D\n");
		else if(a>=0&&a<=59) printf("E\n");
		else  printf("Score is error!\n");
	}
	return 0;
}

J - Water2

小赵今年毕业,终于找到了工作。 他赚了很多钱,但似乎从来没有足够的钱。 小赵决定,他需要抓住他的金融投资组合并解决他的融资问题。 第一步是弄清楚他的钱花到哪里去了。 小赵有他的银行账户账单,并希望看到他有多少钱。 通过编写程序帮助小赵从过去12个月的每个月结算余额并计算其平均账户余额。

Input

输入将是十二行。 每一行将包含他的银行账户在特定月份的期末余额。 每个数字都是正数。 不包括美元符号。

Output

扫描二维码关注公众号,回复: 9777076 查看本文章

输出将是一个单一数字,即十二个月期末余额的平均值。 它将四舍五入到最接近的一分钱,立即以美元符号开头,然后是行尾。 输出中不会有其他空格或字符。

Sample Input

100.00
489.12
12454.12
1234.10
823.05
109.20
5.27
1542.25
839.18
83.99
1295.01
1.75

Sample Output

$1581.42
//本程序中输入输出是小关键点
#include<cstdio>
int main() {
	double sum=0;
	double a=0;
	for(int i=0; i<12; i++) {
		scanf("%lf",&a);//输入常浮点用%lf,注意用double,float可能会溢出
		sum+=a;
	}
	printf("$%.2lf",sum/12);//输出格式化,对于%f, “.2”代表四舍五入保留两位小数

	return 0;
}

K - Water3

求实数的绝对值。

Input

输入数据有多组,每组占一行,每行包含一个实数。

Output

对于每组输入数据,输出它的绝对值,要求每组数据输出一行,结果保留两位小数。

Sample Input

123
-234.00

Sample Output

123.00
234.00
#include<cstdio>
int main() {
	double a=0;
	while(~scanf("%lf",&a)){//获取文件末尾的小技巧不要忘记
		if(a>0)printf("%.2lf\n",a);
		else printf("%.2lf\n",-a);
	}
	return 0;
}

对于给定的一个字符串,统计其中数字字符出现的次数。

Input

输入数据有多行,第一行是一个整数n,表示测试实例的个数,后面跟着n行,每行包括一个由字母和数字组成的字符串。

Output

对于每个测试实例,输出该串中数值的个数,每个输出占一行。

Sample Input

2
asdfasdf123123asdfasdf
asdf111111111asdfasdfasdf

Sample Output

6
9
#include<cstdio>
int main() {
	int n;
	char a[1000];

	scanf("%d",&n);//不吃回车
	getchar();//吃掉回车
	while(n--) {
		gets(a);//读入整行,自动吃回车
		int k=0;
		int j=-1;
		while(a[++j]!='\0') {//遍历字符串
			if(a[j]>='0'&&'9'>=a[j])k++;
		}
		printf("%d\n",k);
		k=0;
	}
	return 0;
}

M - Water10

输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符。

Input

输入数据有多组,每组占一行,有三个字符组成,之间无空格。

Output

对于每组输入数据,输出一行,字符中间用一个空格分开。

Sample Input

qwe
asd
zxc

Sample Output

e q w
a d s
c x z
#include <cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
i
nt main() {
	char s[100];
	while(~scanf("%s",&s)) {//无穷输入判断末尾技巧
		sort(&s[0],&s[3]);//排序,这里不需要传入自定义的排序函数
		//尤其注意,传入的开始指针是最开头的元素,但结束指针是最后一个元素的后一个元素。
		printf("%c %c %c\n",s[0],s[1],s[2]);
	}
	return 0;
}

N - 十六进制


给定两个十六进制整数a和b,输出他们的和

Input

输入多组数据,以EOF作为数据的结束
每组数据在一行中包含了两个十六进制整数a和b。

Output

对于每一组数据,以十进制输出a+b的和。
每组输出占用一行。

Sample Input

1 9
A B
a b

Sample Output

10
21
21
#include<cstdio>

int main() {
	int a,b;
	while(~scanf("%x %x",&a,&b)){//%x代表输入一个16进制,%o代表8进制
		printf("%d\n",a+b);
	}
	return 0;
}

O - 快速幂板子题

给出3个正整数A B C,求A^B Mod C。
例如,3 5 8,3^5 Mod 8 = 3。

Input

3个正整数A B C,中间用空格分隔。(1 <= A,B,C <= 10^9)

Output

输出计算结果

Sample Input

3 5 8

Sample Output

3
#include<cstdio>
//这是带模快速幂,注意输入输出的长度。
int qPowerMod(long long a,long long b,long long c) {
	int ans=1;
	a=a%c;
	while(b>0) {
		if(b%2==1)
			ans=ans*a%c;
		a=a*a%c;
		b=b/2;
	}
	return ans;
}
int main() {
	long long a,b,c;
	scanf("%lld%lld%lld",&a,&b,&c);
	a=qPowerMod(a,b,c);
	printf("%lld\n",a);
	return 0;
}

P - 多关键字排序

一个长方形有长和宽,分别设为 a 和 b
现在想对一些长方形进行排序
有一种新的排序方法。如下
我们按照两个长方形的a-b值来对他们按降序排序,如果有重复,按照b值升序排序,如果还有重复,按照输入的顺序排序。
也就是说,是多关键字排序:
第1关键字,a-b,降序;
第2关键字,b,升序;
第3关键字,输入的顺序,升序;

Input

多组测试数据(大概100组),每一组测试数据第一行先给出一个整数n,代表有n个长方形需要被排序。
长方形被从0到n−1标号。
接下来n行,每一含有两个整数代表每一个长方形a和b 第i行描述长方形i−1的信息。
请处理到文件末尾。
所有整数都在[1,100]的范围内

Output

对于每一个数据,在一行中输出排好序之后的长方形ID,注意每两个 ID 【之间!】有一个空格,其他地方不要有多余空格

Sample Input

2
100 1
1 2
3
100 50
3 4
1 2

Sample Output

0 1
0 2 1
#include<iostream>
#include<cstdio>
#include<algorithm>
#define maxn 1000
char ch[maxn];
using namespace std;
struct K {
	int x;
	int y;
	int z;
} a[maxn];
//多关键词排序中自定义排序函数很管用,基本上是唯一解决方案
//我最开始做题时纯手动写排序,太可笑了!
//所以必须有人领进门,不然很多“理所当然”技巧你根本不知道! 
int cmp(K x,K y) {
	return ((x.x>y.x) ||((x.x==y.x && (x.y<y.y))) || (x.x==y.x && x.y==y.y && (x.z<y.z)));
}
int main() {
	int n,x,y;
	while(scanf("%d",&n)!=EOF) {
		for(int i=1; i<=n; i++) {
			scanf("%d%d",&x,&y);
			a[i].x=x-y;
			a[i].y=y;
			a[i].z=i-1;
		}
		sort(a+1,a+1+n,cmp);
		for(int i=1; i<n; i++) {
			printf("%d ",a[i].z);
		}
		printf("%d\n",a[n].z);
	}
	return 0;
}

多项式的描述如下:
1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 + …
现在请你求出该多项式的前n项的和。

Input

输入数据由2行组成,首先是一个正整数m(m<100),表示测试实例的个数,第二行包含m个正整数,对于每一个整数(不妨设为n,n<1000),求该多项式的前n项的和。

Output

对于每个测试实例n,要求输出多项式前n项的和。每个测试实例的输出占一行,结果保留2位小数。

Sample Input

2
1 2

Sample Output

1.00
0.50
#include<cstdio> 
#include <cmath>
using namespace std;
int main() {
	int m,n,i;
	float s;
	while(~scanf("%d",&m)) {
		while(m--) {
			s=0;
			scanf("%d",&n);
			for(i=1; i<=n; i++)//手动算多项式
				s=s+(1/((pow((-1),(i+1)))*i));
			printf("%.2f\n",s);//格式化输出
		}
	}
	return 0;
}

R - Water12

给定一段连续的整数,求出他们中所有偶数的平方和以及所有奇数的立方和。

Input

输入数据包含多组测试实例,每组测试实例包含一行,由两个整数m和n组成。

Output

对于每组输入数据,输出一行,应包括两个整数x和y,分别表示该段连续的整数中所有偶数的平方和以及所有奇数的立方和。
你可以认为32位整数足以保存结果。

Sample Input

1 3
2 5

Sample Output

4 28
20 152
//注意审题,是从n到m的一系列整数
#include <iostream>
using namespace std;
int main() {
	int m, n, i, sum1, sum2, temp;
	while (~scanf("%d %d",&m,&n)) {
		sum1 = 0;
		sum2 = 0;
		if (m > n) {//注意 
			temp = m;
			m = n;
			n = temp;
		}
		for (i = m; i <= n; i++) {
			if (i % 2 == 0) {
				sum1 = sum1 + i*i;
			} else {
				sum2 = sum2 + i*i*i;
			}
		}
		printf("%d %d\n",sum1,sum2);
	}
	return 0;
}

S - Water13

数列的定义如下:
数列的第一项为n,以后各项为前一项的平方根,求数列的前m项的和。

Input

输入数据有多组,每组占一行,由两个整数n(n<10000)和m(m<1000)组成,n和m的含义如前所述。

Output

对于每组输入数据,输出该数列的和,每个测试实例占一行,要求精度保留2位小数。

Sample Input

81 4
2 2

Sample Output

94.73
3.41
//循环求和即可
#include<cstdio>
#include<cmath>
int main(){
	int m,i,n;
	double r,s;
	while(scanf("%d %d",&n,&m)!=EOF){
		s=n;
		r=n;
		for(i=1; i<m; i++){
			r=sqrt(r);
			s=s+r;
		}
		printf("%.2lf\n",s);
	}
	return 0;

}

“水仙花数”是指一个三位数,它的各位数字的立方和等于其本身,比如:153=1^ 3 +5^ 3 +3^ 3。
现在要求输出在[m,n]内的水仙花数。

Input

输入数据有多组,每组占一行,包括两个整数m和n(100<=m<=n<=999)。

Output

对于每个测试实例,要求输出所有在给定范围内的水仙花数,就是说,输出的水仙花数必须大于等于m,并且小于等于n,如果有多个,则要求从小到大排列在一行内输出,之间用一个空格隔开;
如果给定的范围内不存在水仙花数,则输出no;
每个测试实例的输出占一行。

Sample Input

100 120
300 380

Sample Output

no
370 371
//可以遍历计算,也可以先把水仙花数穷举出来,下一个题也要用到整除法。
#include <cstdio>

int main(void) {
	//freopen("input.txt","r",stdin);
    int m, n;
    while (scanf("%d%d", &m, &n) == 2) {
        int cnt = 0;
        for (int i = m; i < n + 1; ++i) {
        	//水仙花数
            if ( (i % 10) * (i % 10) * (i % 10) +
                 (i / 10 %10) * (i / 10 %10) * (i / 10 %10) +
                 (i / 100) * (i / 100) * (i / 100) == i 
				) {
					
				//´¦ÀíÐÐÄ©¿Õ¸ñ 
                if (cnt == 0) {
                    printf("%d", i);
                }else if (cnt > 0) {
                    printf(" %d", i);
                }
                ++cnt;
            }
        }
        if (cnt == 0) {
            printf("no");
        }
        cnt = 0;
        printf("\n");
    }

    return 0;
}

U - Water15

要发工资了!!!
财务处的小胡老师最近在考虑一个问题:如果每个老师的工资额都知道,最少需要准备多少张人民币,才能在给每位老师发工资的时候都不用老师找零呢?
这里假设老师的工资都是正整数,单位元,人民币一共有100元、50元、10元、5元、2元和1元六种。

Input

输入数据包含多个测试实例,每个测试实例的第一行是一个整数n(n<100),表示老师的人数,然后是n个老师的工资。
n=0表示输入的结束,不做处理。

Output

对于每个测试实例输出一个整数x,表示至少需要准备的人民币张数。每个输出占一行。

Sample Input

3
1 2 3
0

Sample Output

4
//可以不断整除,商是该币数量,余数继续向下整除;
#include<cstdio>
#include<cmath>
#include<cstdlib>
int main(void) {
	//freopen("input.txt","r",stdin);
	int n;
	while(~scanf("%d",&n)) {
		if(n==0)
			break;
		int c[100];
		int i;
		int sum=0;
		for(i=0; i<n; i++)
			scanf("%d",&c[i]);
		for(i=0; i<n; i++) {
			if(c[i]/100!=0)
				sum=sum+c[i]/100;
			if(c[i]%100/50!=0)
				sum=sum+1;
			if(c[i]%100%50/10!=0)
				sum=sum+c[i]%100%50/10;
			if(c[i]%100%50%10/5!=0)
				sum=sum+1;
			if(c[i]%100%50%10%5/2!=0)
				sum=sum+c[i]%100%50%10%5/2;
			sum=sum+c[i]%100%50%10%5%2;
		}
		printf("%d", sum);
		printf("\n");
	}
	return 0;

}

V - Water16

青年歌手大奖赛中,评委会给参赛选手打分。选手得分规则为去掉一个最高分和一个最低分,然后计算平均得分,请编程输出某选手的得分。

Input

输入数据有多组,每组占一行,每行的第一个数是n(2<n<=100),表示评委的人数,然后是n个评委的打分。

Output

对于每组输入数据,输出选手的得分,结果保留2位小数,每组输出占一行。

Sample Input

3 99 98 97
4 100 99 98 97

Sample Output

98.00
98.50
//排序就可以去掉最高和最低分了
#include<cstdio>
#include<cstdlib>
int cmp(const void*p1,const void*p2) {
	return *(int*)p1-*(int*)p2;
}
int main() {
	//freopen("input.txt","r",stdin);
	int n,score[105],i;
	double s,aver;
	while(scanf("%d",&n)!=EOF) {
		s=0;
		for(i=0; i<n; i++)
			scanf("%d",&score[i]);
		qsort(&score[0],n,sizeof(score[0]),cmp);
		for(i=1; i<n-1; i++)
			s=s+score[i];
		aver=(double)s/(double)(n-2.0);
		printf("%.2f\n",aver);
	}
	return 0;
}

读入N名学生的成绩,将获得某一给定分数的学生人数输出。

Input

测试输入包含若干测试用例,每个测试用例的格式为
第1行:N
第2行:N名学生的成绩,相邻两数字用一个空格间隔。
第3行:给定分数
当读到N=0时输入结束。其中N不超过1000,成绩分数为(包含)0到100之间的一个整数。

Output

对每个测试用例,将获得给定分数的学生人数输出。

Sample Input

3
80 60 90
60
2
85 66
0
5
60 75 90 55 75
75
0

Sample Output

1
0
2
//这个题用哈希表来做会比较好
#include <cstdio>
#include <cstdlib>

int rsc[1000+4];

int main() {
	int n,i,j,k;
	//freopen("input.txt","r",stdin);
	while(~scanf("%d",&n)) {
		if(n==0)
			break;
		for(i=0; i<n; i++) {
			scanf("%d",&rsc[i]);
		}
		int sc;
		int num=0;
		scanf("%d",&sc);
		for(i=0; i<n; i++) {
			if(sc==rsc[i])
				num++;
		}
		printf("%d\n",num);
	}
	return 0;
}

X - 与课堂讲过的快读粗略原理异曲同工

读入两个小于100的正整数A和B,计算A+B.
需要注意的是:A和B的每一位数字由对应的英文单词给出.

Input

测试输入包含若干测试用例,每个测试用例占一行,格式为"A + B =",相邻两字符串有一个空格间隔.当A和B同时为0时输入结束,相应的结果不要输出.

Output

对每个测试用例输出1行,即A+B的值.

Sample Input

one + two =
three four + five six =
zero seven + eight nine =
zero + zero =

Sample Output

3
90
96
//还好所有字符串中间都加了空格,要不然处理起来会很麻烦
#include<cstdio>
#include<cstring>
char a[10][10]= {"zero","one","two","three","four","five","six","seven","eight","nine"};
int strToNum(char *b) {
	int i,digit=-1;
	for(i=0; i<10; i++) {
		if(strcmp(a[i],b)==0) {
			digit=i;
			break;
		}
	}
	return digit;
}
int main() {
	//freopen("input.txt","r",stdin);
	while(1) {
		int sum1=0;
		int sum2=0;
		int sum=0;
		char tmp[100];
		while(scanf("%s",tmp)&&strcmp(tmp,"+")!=0)
			sum1=sum1*10+strToNum(tmp);
		while(scanf("%s",tmp)&&strcmp(tmp,"=")!=0)
			sum2=sum2*10+strToNum(tmp);
		if(sum1==0&&sum2==0)//Á½ÊýΪ0£¬Í˳ö
			break;
		printf("%d\n",sum1+sum2);
	}
	return 0;
}

“回文串”是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串。请写一个程序判断读入的字符串是否是“回文”。

Input

输入包含多个测试实例,输入数据的第一行是一个正整数n,表示测试实例的个数,后面紧跟着是n个字符串。

Output

如果一个字符串是回文串,则输出"yes",否则输出"no".

Sample Input

4
level
abcde
noon
haha

Sample Output

yes
no
yes
no
/基本思路是非常清晰的,但是有些边界条件的处理需要注意
#include<cstdio>
#include<cstring>

int main() {
	//freopen ( "input.txt", "r", stdin );
	int n, i, j, index;
	char a[1000];

	scanf("%d",&n);
	getchar();//获取回车



	for(i=0; i<n; i++) {
//进行判断
		gets(a);
		index=0;
		for(j=0; j<strlen(a); j++) {
			if(a[j]==a[strlen(a)-1-j]) {
				index++;
			}
		}
//输出结果,相等的程度不等于字符串长度则no
		if(index==j) {
			printf("yes\n");
		} else {
			printf("no\n");
		}

	}
	return 0;
}

Z - 假装考察插入排序

有n(n<=100)个整数,已经按照从小到大顺序排列好,现在另外给一个整数m,请将该数插入到序列中,并使新的序列仍然有序。

Input

输入数据包含多个测试实例,每组数据由两行组成,第一行是n和m,第二行是已经有序的n个数的数列。n和m同时为0标示输入数据的结束,本行不做处理。

Output

对于每个测试实例,输出插入新的元素后的数列。

Sample Input

3 3
1 2 4
0 0

Sample Output

1 2 3 4
//这道题没什么好说的
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
bool cmp(int a,int b) {
	return a<b;
}
int main() {
	//freopen ( "input.txt", "r", stdin );

	int n=0;
	int m=0;
	while(~scanf("%d %d",&n,&m)&&(n||m)) {
		int* s=new int[n+1];
		for(int i=0; i<n; i++) {
			int temp=0;
			scanf("%d",&temp);
			s[i]=temp;
		}
		s[n]=m;
		sort(&s[0],s+n+1,cmp);
		for(int i=0; i<n+1; i++) {
			printf("%d",s[i]);
			if(i<n)printf(" ");//防止最后输出空格
		}
		printf("\n");
	}

	return 0;
}
发布了166 篇原创文章 · 获赞 21 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/lgfx21/article/details/104454703