资源描述
1005货币兑换
Description
给出人民币对美元、欧元、日元的当日汇率,求给定金额的人民币能兑换成外币的金额,求给定金额的外币能兑换成人民币的金额。
要计算的外币有三种:美元、欧元、日元。
Input
输入有三行。
第一行依次为美元、欧元、日元外币汇率,用空格分开。汇率用100外币为单位,精确到小数点后4位,如668.5200表示“100美元=668.5200人民币”。汇率浮动范围为(0,10000)。
第二行为外币金额x,第三行为人民币金额y。x,y均为整数,且0<x,y<10000。
Output
输出为两行。
第一行是金额为x的美元、欧元、日元兑换成人民币的金额,用空格分开。
第二行是金额为y的人民币兑换成美元、欧元、日元的金额,用空格分开。
所有金额精确到小数点后两位。
Sample Input
668.5200 908.0685 7.9852 1500 1500
Sample Output
10027.80 13621.03 119.78 224.38 165.19 18784.75
HINT
了解浮点数据类型的精确度和输出控制。
#include <stdio.h>
int main()
{
double a,b,c;
double x;
double y;
scanf ("%lf%lf%lf",&a,&b,&c);
scanf ("%lf",&x);
scanf ("%lf",&y);
printf ("%.2lf %.2lf %.2lf\n",x*0.01*a,x*0.01*b,x*0.01*c);
printf ("%.2lf %.2lf %.2lf\n",y/a*100,y/b*100,y/c*100);
return 0;
}
1006求字符的值
Description
从键盘输入3个字符(不含双字节字符),分别输出每个字符的十进制值(ASCII码)、八进制值和十六进制值。
Input
输入为3个字符。
Output
输出为3行。
每一行为每个字符(对应输入顺序)的十进制、八进制和十六进制值,用空格分隔开。每个输出的值占3个字符,不足3个字符前面补0。
Sample Input
0 A
Sample Output
048 060 030
032 040 020
065 101 041
HINT
了解字符值的存储和整型的关系。
#include <stdio.h>
int main()
{
char a,b,c;
scanf ("%c%c%c",&a,&b,&c);
printf("%.3d %.3o %.3x\n",a,a,a);
printf("%.3d %.3o %.3x\n",b,b,b);
printf("%.3d %.3o %.3x\n",c,c,c);
return 0;
}
1007奇数还是偶数?
Description
输入一个整数,判读它是奇数还是偶数。
Input
输入只有一行,为一个100以内的正整数。
Output
输出为一行。
若输入为偶数则输出“even”,奇数输出“odd”。
Sample Input
30
Sample Output
even
HINT
用整数运算可以解决,练习“?:”表达式。
法1:
#include <stdio.h>
int main()
{
int a;
(0<a)&&(a<100);
scanf ("%d",&a);
if(a%2==0)
printf ("even\n");
else
printf ("odd\n");
return 0;
}
法2:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
scanf("%d",&a);
if(a<100&&a>0)
return a%2==0?printf("even\n"):printf("odd\n");
}
1008绝对值
Description
求整型数据和浮点型数据的绝对值。
Input
输入两个数,第一个是整数,第二个是浮点数。
Output
输出为两行,第一行为整数的绝对值,第二行为浮点数的绝对值,注意浮点数的绝对值不输出无意义的0。
Sample Input
-1 1
Sample Output
1 1
HINT
求绝对值可以用标准库函数来完成,也可以自己判断。注意浮点数的输出格式。求绝对值的函数在哪个头文件?貌似很多人会搞错,包括很多编书的人!
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int a,b;
double c,d;
scanf("%d",&a);
b=abs(a);
scanf("%lf",&c);
d=fabs(c);
printf("%d\n",b);
printf("%g\n",d); //%g 按%f,%e,两者中较短的输出,不输出无意义的0
return 0;
}
//fabs---math.h
//abs---stdlib.h
1009简单的打折计算
Description
商店规定:消费满n元,可以打八八折。设某件商品标价m元,输入购买的件数x,计算出需要支付的金额(单位:元),精确到分。
Input
输入只有一行,三个整数m、n和x,且0<x<m<n<1000。
Output
输出金额,精确到分。
Sample Input
95 300 4
Sample Output
334.40
HINT
了解浮点型的输出控制,注意整型和浮点型混合运算过程中的数据类型转换。
法1:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m,n,x;
(0<x)&&(x<m)&&(m<n)&&(n<1000);
float s;
scanf("%d %d %d",&m,&n,&x);
if(m*x>=n)
s=m*x*0.88;
else
s=m*x;
printf ("%.2f",s);
return 0;
}
法2:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m,n,x;
float s;
scanf("%d %d %d",&m,&n,&x);
while((0<x)&&(x<m)&&(m<n)&&(n<1000))
{
if(m*x>=n)
s=m*x*0.88;
else
s=m*x;
printf ("%.2f",s);
break;
}
return 0;
}
法3:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x;
float m,n,z;
scanf("%f%f%d",&m,&n,&x);
while((0<x)&&(x<m)&&(m<n)&&(n<1000))
{
z=m*x;
if(z<=n)
printf("%.2f",z);
else
if(z>n)
z=0.88*z;
printf("%.2f",z);
break; //breeak 可换为return 0;代表结束
}
}
1010判断闰年
Description
输入一个正整数的年份,判断是否为闰年。
Input
输入只有一行,为一个10000以内的正整数。
Output
输出为一行。
若输入为闰年则输出“Yes”,否则输出“No”。
Sample Input
2010
Sample Output
No
HINT
了解逻辑运算符和关系运算符。
闰年的定义:能被4整除,但不能被100整除;或能被400整除
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x;
scanf("%d",&x);
(0<x&&x<10000);
{
if(x%4==0&&x%100!=0)
printf("Yes\n");
else if (x%400==0)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
法2:
#include <stdio.h>
#include <stdlib.h>
int main ()
{ int y;
scanf("%u",&y);
while(y>0&&y<10000)
{
if(((y%4==0)&&(y%100!=0))||(y%400==0))
printf("yes");
else
printf("no");
return 0;
}
}
1011GHacker的解谜过关游戏
Description
GHacker最近痴迷于一个新上市的解谜游戏,其中一关的过关是破解一个字符串S。经过3天的冥思苦想,GHacker成功的搞明白了这其中的奥秘,把串S中的整数取出来求和,就可以过关了。但是GHacker的数学实在糟糕。他无法在短暂的时间内算出来,只好求助Jackie。Jackie观察到虽然每次出现的数字不同,但是其它的符号并不会变化。于是Jackie编写了一个非常短的程序,帮助GHacker把这一关过了。
Input
输入为串S,只有一行。
Output
串S中用非数字(0~9)分隔开的非负整数之和,不会超出int类型的数据范围。
Sample Input
`13?:[7514],54.487=="(438922x159??392)%032\n111cdef120$95;
Sample Output
447899
HINT
scanf()可以解决这个问题,注意转义字符和格式控制字符。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b,c,d,e,f,g,h,i,j,k,l;
scanf("`%d\?:[%d],%d.%d==\"(%dx%d\?\?%d)%%%d\\n%dcdef%d\$%d\;",&a,&b,&c,&d,&e,&f,&g,&h,&i,&j,&k);
l=a+b+c+d+e+f+g+h+i+j+k;
printf("%d",l);
return 0;
}
1012水仙花数
Description
如果一个三位十进制数等于其各位数字的立方和,则称这个数为水仙花数。如:13+53+33=153。
Input
一个整数x,100<=x<=999。
Output
x是水仙花数,则输出“YES”,否则为“NO”。
Sample Input
153
Sample Output
YES
HINT
Append Code
法1:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{ int c,a,b,x,m,sum;
scanf ("%d",&x);
c=x/100;
m=x%100;
a=m/10;
b=m%10;
if(c*c*c+a*a*a+b*b*b==x)
printf("YES\n");
else
printf ("NO\n");
return 0;
}
法2:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b,c,z;
scanf("%d",&z);
while (z>=100&&z<=999)
{
a=z/100;
b=z%100/10;
c=z%100%10;
if(z==a*a*a+b*b*b+c*c*c)
printf("YES");
else printf("NO");
return 0;
}
}
1021 A+B Problem (II) : Input/Output Practice
Description
计算a+b,0<=a,b<1000。
Input
输入的第一行是一个整数N,后面有N对整数a和b,每对a和b占一行,a,b用空格分开。
Output
每行输出一个a+b的和,顺序与输入对应。
Sample Input
2
1 2
10 20
Sample Output
3 30
HINT
N给出了测试样例数,用for循环处理方便。
Append Code
法1:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,i,j;
scanf("%d",&n);
int a[n][2];
for(i=0;i<n;i++)
{
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
}
for(i=0;i<n;i++)
printf("%d\n",a[i][0]+a[i][1]);
return 0;
}
法2:
#include <stdio.h>
#include <stdlib.h>
int main()
{ int a,b,N;
scanf ("%d",&N);
int c[N][2];
for (a=0;a<N;a++)
{for(b=0;b<2;b++)
scanf ("%d",&c[a][b]);
}
for (a=0;a<N;a++)
printf("%d\n",c[a][0]+c[a][1]);
return 0;
}
1022 A+B Problem (III) : Input/Output Practice
Description
计算a+b,0<=a,b<1000。
Input
输入有多对整数a和b组成,每对a和b占一行,a,b用空格分开。当测试样为0 0时表示输入结束,0 0不参与运算。
Output
每行输出一个a+b的值,顺序与输入对应。
Sample Input
1 2
10 20
0 0
Sample Output
3
30
HINT
练习break的使用。
Append Code
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b;
while (scanf("%d %d",&a,&b))
//while(scanf("%d %d",&a,&b)&&(a!=0&&b!=0))不正确,当输入a=0,b=1时也会终止
{
if(a==0&&b==0)
break;
printf("%d\n",a+b);
}
return 0;
}
(未看)1054 Matrix Problem (II) : Array Practice
Description
求两个矩阵A、B的和。根据矩阵加法的定义,只有同阶的矩阵才能相加。可以确保所有运算结果都在int类型的范围之内。
Input
输入数据为多个矩阵,每个矩阵以两个正整数m和n开始,满足0<m,n<=100,接下来为一个m行n列的矩阵A。当输入的m和n均为0时,表示输入数据结束
Output
对输入的矩阵两两相加:第1个和第2个相加、第3个和第4个相加……按顺序输出矩阵相加的结果:每行两个元素之间用一个空格分开,每行最后一个元素之后为一个换行,在下一行开始输出矩阵的下一行。
若输入的矩阵不为偶数个,最后剩余的矩阵不产生任何输出。
不满足矩阵加法定义的数据输出“Not satisfied the definition of matrix addition!”
每两组输出之间用一个空行分隔开。
Sample Input
3 3
1 2 3
4 5 6
7 8 9
3 3
9 8 7
6 5 4
3 2 1
3 3
1 1 1
1 1 1
1 1 1
2 2 2
2 2 2
1 1
0
0 0
Sample Output
10 10 10
10 10 10
10 10 10
Not satisfied the definition of matrix addition!
HINT
矩阵的加法就是对应位置上的元素相加。
Append Code
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m,n,i,j,t,s,k;
int a[100][100],b[100][100],c[100][100];
for(k=0;;k++)
{
scanf("%d%d",&m,&n);
if(m==0||n==0)
return 0;
if(k!=0)
printf("\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
scanf("%d%d",&s,&t);
if(s==0||t==0)
return 0;
for(i=0;i<s;i++)
for(j=0;j<t;j++)
scanf("%d",&b[i][j]);
if(m==s&&n==t)
{
for(i=0;i<m;i++)
for(j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
if(j==0)
printf("%d",c[i][j]);
else
printf(" %d",c[i][j]);
printf("\n");
}
}
else
printf("Not satisfied the definition of matrix addition!\n");
}
return 0;
}
(未看)1055 Matrix Problem (III) : Array Practice
Description
求两个矩阵A、B的乘积C=AB。根据矩阵乘法的定义,只有A的列数和B的行数相同才能相乘。可以确保所有运算结果都在int类型的范围之内。
Input
输入数据为多个矩阵(最少2个),每个矩阵以两个正整数m和n开始,满足0<m,n<=100,接下来为一个m行n列的矩阵A。当输入的m和n均为0时,表示输入数据结束。
Output
对输入的矩阵两两相乘:第1个和第2个相乘、第1个和第2个相乘的结果和第3个相乘……按顺序输出矩阵相乘的结果:每行两个元素之间用一个空格分开,每行最后一个元素之后为一个换行,在下一行开始输出矩阵的下一行。
若前k个矩阵相乘的结果和第k+1个矩阵无法相乘(即不满足矩阵乘法定义),则输出“Not satisfied the definition of matrix multiplication!”。然后用第k+1个矩阵去和第k+2个矩阵相乘。最后一个矩阵只做乘数。
每两组输出之间用一个空行分隔开。
Sample Input
2 3
1 1 1
1 1 1
3 3
1 2 3
4 5 6
7 8 9
3 1
0
0
0
0 0
Sample Output
12 15 18
12 15 18
0
0
HINT
矩阵的乘法就是一行乘以一列加起来做一个元素。
Append Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int m,n,m1,n1,i,j,k,l=0;
int a[102][102],b[102][102],c[102][102];
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
scanf("%d %d",&m,&n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
m1=m;
n1=n;
while(scanf("%d%d",&m,&n)!=EOF)
{
if(m==0&&n==0) break;
i=0;
while(i<m)
{
for(j=0;j<n;j++)
scanf("%d",&b[i][j]);
i++;
}
if(n1==m)
{
for(i=0;i<m1;i++)
{
for(j=0;j<n;j++)
{
for(k=0;k<m;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}//矩阵的乘法就是一行乘以一列加起来做一个元素。
}
m1=m1;
n1=n;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
if(l!=0)
printf("\n");
l=1;
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
if(j==0)
printf("%d",c[i][j]);
else
printf(" %d",c[i][j]);
a[i][j]=c[i][j];
}
printf("\n");
}
memset(c,0,sizeof(c));
}
else
{
if(l!=0)
printf("\n");//
l=1;
printf("Not satisfied the definition of matrix multiplication!\n");
memset(a,0,sizeof(a));//
//每两组输出之间用一个空行分隔开。
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=b[i][j];
}
}
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
m1=m;
n1=n;
}
}
return 0;
}
1099简单的整数排序
Description
对给出的若干整数按从小到大排序。
Input
输入的第一个数为n(n<=1000),后接n个整数。
Output
按从小到大的顺序输出这些整数,每两个整数之间用一个空格分隔开,最后一个整数后面没有空格。
Sample Input
10 3 9 1 5 2 8 5 6 7 3
Sample Output
1 2 3 3 5 5 6 7 8 9
HINT
排序前必须把所有的整数都存储下来。因为只有最多1000个数,1秒的时间足够任何排序算法运行处结果来。
Append Code
法1:
#include<stdio.h>
#include <stdlib.h>
#define N 1000
int main()
{
int n,i,temp,a[N],j;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
int first=0;
for(i=0;i<n;i++)
{
if(first==1)
printf(" ");
printf("%d",a[i]);
first=1;//先输出空格,再输出数字
}
return 0;
}
法2:
#include <stdio.h>
#include <stdlib.h>
#include<stdio.h>
#include <stdlib.h>
#define N 1000
int main()
{
int first=0,n,i,temp,a[N],j;//定义整形
scanf("%d",&n);//输入n,后面跟n个数字
for(i=0;i<n;i++)//for循环
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
for(j=0;j<n-i-1;j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];//i与j互换
a[j+1]=temp;
}
for(i=0;i<n;i++)
{
if(first==1)//使输出的格式符合提议
printf(" ");
printf("%d",a[i]);
first=1;
}
return 0;
}
1201编写函数:你交换了吗?之一 (Append Code)
Description
从标准输入读取两个整数a、b,按先小后大的次序输出。那么a、b的输出顺序与输入顺序是否一致呢?即,输出相对于输入是否交换过位置。
注意:a和b相等时不产生交换。
-----------------------------------------------------------------------------
用C语言编写函数实现,append.c中函数原型为:
int is_swapped(int * a, int * b);
功能:返回值是0或者1。其它的就不告诉你了,猜猜看^-^。
用C++编写函数实现,append.cc中函数原型为:
bool isSwapped(int &a, int &b);
功能:返回一个布尔值(true或false)。
函数的调用格式见“Append Code”。
Input
两个较小的整数a,b,用空格分开。
Output
输出有两种情况:
1) “a b NO”, 当a,b没有交换过
2) “b a YES”, 当a,b交换过
Sample Input
5 3
Sample Output
3 5 YES
HINT
参看系统首页上的“Append Code”使用说明,讨论版(Web Board)上也有。
Append Code
append.c, append.cc,
int main()
{
int a, b;
scanf("%d%d", &a, &b);
if(is_swapped(&a, &b))
printf("%d %d YES", a, b);
else
printf("%d %d NO", a, b);
}
#include <stdio.h>
#include <stdlib.h>
int is_swapped(int * a, int * b)
{ int temp;
if (*a<=*b)
return 0;
else
{
temp =*a;
*a=*b;
*b=temp;
}
return 1;
}
int main()
{
int a, b;
scanf("%d%d", &a, &b);
if(is_swapped(&a, &b))
printf("%d %d YES", a, b);
else
printf("%d %d NO", a, b);
}
1202编写函数:你交换了吗?之二 (Append Code)
Description
从标准输入读取两个整数a、b,按先小后大的次序输出。那么a、b的输出顺序与输入顺序是否一致呢?即,输出相对于输入是否交换过位置。
注意:a和b相等时不产生交换。
-----------------------------------------------------------------------------
用C语言编写函数实现,append.c中函数原型为:
int is_swapped(int * a, int * b);
功能:返回值是0或者1。其它的就不告诉你了,猜猜看^-^。
用C++编写函数实现,append.cc中函数原型为:
bool isSwapped(int &a, int &b);
功能:返回一个布尔值(true或false)。
函数的调用格式见“Append Code”。
Input
两个较小的整数a,b,用空格分开。
Output
输出有两种情况:
1) “a b NO”, 当a,b没有交换过
2) “b a YES”, 当a,b交换过
Sample Input
5 3
Sample Output
3 5 YES
HINT
参看系统首页上的“Append Code”使用说明,讨论版(Web Board)上也有。
Append Code
append.c, append.cc,
int main()
{
int a, b;
scanf("%d%d", &a, &b);
if(is_swapped(&a, &b))
printf("%d %d YES", b, a);
else
printf("%d
展开阅读全文