[关闭]
@P2Oileen 2017-04-08T03:08:43.000000Z 字数 14007 阅读 2098

艾琳蒟蒻的OPENJUDGE奋斗史

代码 noi 艾琳


防止代码被吞掉- -
而且WORD文档这东西好辣鸡- =
啊就这样…


第一节 青铜膜法师


[1.1] 07:输出浮点数

描述
读入一个双精度浮点数,分别按输出格式“%f”,“%f”保留5位小数,“%e”和“%g”的形式输出这个整数,每次在单独一行上输出。
输入
一个双精度浮点数。
输出
输出有四行:
第一行是按“%f”输出的双精度浮点数;
第二行是按“%f”保留5位小数输出的双精度浮点数;
第三行是按“%e”输出的双精度浮点数;
第四行是按“%g”输出的双精度浮点数。
样例输入
12.3456789
样例输出
12.345679
12.34568
1.234568e+001
12.3457

  1. /*WA*/
  2. #include <cstdio>
  3. int main()
  4. {
  5. double a;
  6. scanf("%lf",&a);
  7. printf("%lf\n%lf.5\n%e\n%g\n",a,a,a,a);
  8. return 0;
  9. }
  1. /*AC*/
  2. #include <cstdio>
  3. int main()
  4. {
  5. double a;
  6. scanf("%lf",&a);
  7. printf("%lf\n%.5lf\n%e\n%g\n",a,a,a,a);
  8. return 0;
  9. }

所以说是%.5lf,不是%lf.5啦…………


[1.2] 10:Hello, World!的大小

描述
还记得在上一章里,我们曾经输出过的“Hello, World!”吗?
它虽然不是本章所涉及的基本数据类型的数据,但我们同样可以用sizeof函数获得它所占用的空间大小。
请编程求出它的大小,看看跟你设想的是否一样?
输入
无。
输出
一个整数,即“Hello, World!”的大小。
样例输入
(无)
样例输出
(不提供)

  1. /*CE*/
  2. #include <cstdio>
  3. int main(){
  4. printf("%d",sizeof("Hello,World!"));
  5. return 0;
  6. }
  1. /*AC*/
  2. #include <cstdio>
  3. int main(){
  4. printf("%d",14);
  5. return 0;
  6. }

完全就是耍赖了……
所以说空格符"\0"这种东西真的是不能忘……在后面的字符串问题中显得格外突出了。


[1.3] 10:计算并联电阻的阻值

描述
对于阻值为r1和r2的电阻,其并联电阻阻值公式计算如下:
R = 1/(1/r1 + 1/r2)
输入
两个电阻阻抗大小,浮点型,以一个空格分开。
输出
并联之后的阻抗大小,结果保留小数点后2位
样例输入
1 2
样例输出
0.67

  1. /*WA*/
  2. #include <cstdio>
  3. int main(){
  4. double r1,r2;
  5. scanf("%lf%lf",&r1,&r2);
  6. printf("%.2lf",1.0/(1.0/r1 + 1.0/r2));
  7. return 0;
  8. }
  1. /*AC*/
  2. #include <cstdio>
  3. int main(){
  4. float r1,r2;
  5. scanf("%f%f",&r1,&r2);
  6. printf("%.2f",1.0/(1.0/r1 + 1.0/r2));
  7. return 0;
  8. }

不知道为什么改成float类型就过了……果然是精度的问题= =哼唧不怪我qnq


[1.3] 11:计算浮点数相除的余数

描述
计算两个双精度浮点数a和b的相除的余数,a和b都是正数的。这里余数(r)的定义是:a = k * b + r,其中 k是整数, 0 <= r < b。
输入
输入仅一行,包括两个双精度浮点数a和b。
输出
输出也仅一行,a÷b的余数
样例输入
73.263 0.9973
样例输出
0.4601
提示
注意:输出时小数尾部没有多余的0,可以用下面这种格式:
double x;
x = 1.33;
printf("%g", x);

  1. /*WA*/
  2. #include <cstdio>
  3. int main(){
  4. double a,b,k=1.0;
  5. double miao=0;
  6. bool haha=1;
  7. scanf("%lf%lf",&a,&b);
  8. while(haha)
  9. {
  10. miao=a-k*b;
  11. if((miao<b && miao>=0)|| miao<0) break;
  12. k++;
  13. }
  14. printf("%g",miao);
  15. return 0;
  16. }
  1. /*AC*/
  2. #include <cstdio>
  3. int main(){
  4. double a,b;
  5. int k=1;
  6. double miao=0;
  7. bool haha=1;
  8. scanf("%lf%lf",&a,&b);
  9. while(haha)
  10. {
  11. miao=(double)a-k*b;
  12. if((miao<b && miao>=0)|| miao<0) break;
  13. k++;
  14. }
  15. if(miao<0) miao+=b;
  16. printf("%g",miao);
  17. return 0;
  18. }

都说了K是整数了!就用int呗!实在不行类型转换就好啊……//果然我是个辣鸡


[1.3] 20:计算2的幂

描述
给定非负整数n,求2n。
输入
一个整数n。0 <= n < 31。
输出
一个整数,即2的n次方。
样例输入
3
样例输出
8

  1. /*WA1*/
  2. #include <cstdio>
  3. int main(){
  4. int n;
  5. scanf("%d",&n);
  6. int miao= 2 <<(n-1);
  7. printf("%d",miao);
  8. return 0;
  9. }
  1. /*WA2*/
  2. #include <cstdio>
  3. int main(){
  4. long long n;
  5. scanf("%d",&n);
  6. long long miao= 2 <<(n-1);
  7. printf("%d",miao);
  8. return 0;
  9. }
  1. /*WA3*/
  2. #include <cstdio>
  3. int main(){
  4. long long n;
  5. scanf("%lld",&n);
  6. long long miao= 2 <<(n-1);
  7. printf("%lld",miao);
  8. return 0;
  9. }
  1. /*WA4*/
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <algorithm>
  5. int main(){
  6. long long n;
  7. scanf("%lld",&n);
  8. long long miao= 2 <<(n-1);
  9. printf("%lld",miao);
  10. return 0;
  11. }
  1. /*WA5*/
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <algorithm>
  5. int main(){
  6. long long n;
  7. scanf("%lld",&n);
  8. long long miao= 2 <<(n-1);
  9. printf("%lld",miao);
  10. return 0;
  11. }
  1. /*AC*/
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <algorithm>
  5. int main(){
  6. long long n;
  7. scanf("%lld",&n);
  8. long long hhhhh= 1 << n;
  9. printf("%lld",hhhhh);
  10. return 0;
  11. }

求你看看n到底要取什么…… 到底是多少啊………………………………
longlong这种东西求你用一用吧………………


[1.4] 04:奇偶ASCII值判断

描述
任意输入一个字符,判断其ASCII是否是奇数,若是,输出YES,否则,输出NO
例如,字符A的ASCII值是65,则输出YES,若输入字符B(ASCII值是66),则输出NO
输入
输入一个字符
输出
如果其ASCII值为奇数,则输出YES,否则,输出NO
样例输入
A
样例输出
YES

  1. /*WA*/
  2. #include <cstdio>
  3. int main(){
  4. char a;
  5. scanf("%c",&a);
  6. if(a % 2==0) printf("even");
  7. else printf("odd");
  8. return 0;
  9. }
  1. /*AC*/
  2. #include <cstdio>
  3. int main(){
  4. char a;
  5. scanf("%c",&a);
  6. if(a % 2==0) printf("NO");
  7. else printf("YES");
  8. return 0;
  9. }

复制黏贴都能错 真是别活了……


[1.4] 05:整数大小比较

描述
输入两个整数,比较它们的大小。
输入
一行,包含两个整数x和y,中间用单个空格隔开。
0 <= x < 2^32, -2^31 <= y < 2^31。
输出
一个字符。
若x > y,输出 > ;
若x = y,输出 = ;
若x < y,输出 < ;
样例输入
1000 100
样例输出
>

  1. /*WA*/
  2. #include <cstdio>
  3. int main(){
  4. int a,b;
  5. scanf("%d%d",&a,&b);
  6. if(a>b) printf(">");
  7. else if(a==b) printf("=");
  8. else if(a<b) printf("<");
  9. return 0;
  10. }
  1. /*AC*/
  2. #include <cstdio>
  3. int main(){
  4. long long a,b;
  5. scanf("%lld%lld",&a,&b);
  6. if(a>b) printf(">");
  7. else if(a==b) printf("=");
  8. else if(a<b) printf("<");
  9. return 0;
  10. }

不开long long会死人 不开long long会死人 不开long long会死人


[1.4] 14:计算邮资

描述
根据邮件的重量和用户是否选择加急计算邮费。计算规则:重量在1000克以内(包括1000克), 基本费8元。超过1000克的部分,每500克加收超重费4元,不足500克部分按500克计算;如果用户选择加急,多收5元。
输入
输入一行,包含整数和一个字符,以一个空格分开,分别表示重量(单位为克)和是否加急。如果字符是y,说明选择加急;如果字符是n,说明不加急。
输出
输出一行,包含一个整数,表示邮费。
样例输入
1200 y
样例输出
17

  1. /*WA*/
  2. #include <cstdio>
  3. int main(){
  4. int x;
  5. double tot=0.0;
  6. char y;
  7. scanf("%d %c",&x,&y);
  8. if(x<=1000) tot+=8.0;
  9. else tot+=((((x-500)/500)*4)+8.0);
  10. if(y=='y') tot+=5.0;
  11. printf("%.0lf",tot);
  12. return 0;
  13. }
  1. /*AC*/
  2. #include <cstdio>
  3. int main(){
  4. int x,tot=8;
  5. char y;
  6. scanf("%d %c",&x,&y);
  7. int k=x-1000;
  8. if(k % 500 >0) k+=(500-k % 500);
  9. if(x>1000) tot+=k/500*4;
  10. if(y=='y') tot+=5;
  11. printf("%d",tot);
  12. return 0;
  13. }

因为是涉及到除法的运算,最好开个double类型。


[1.4] 17:判断闰年

描述
判断某年是否是闰年。
输入
输入只有一行,包含一个整数a(0 < a < 3000)
输出
一行,如果公元a年是闰年输出Y,否则输出N
样例输入
2006
样例输出
N
提示
公历纪年法中,能被4整除的大多是闰年,但能被100整除而不能被400整除的年份不是闰年, 能被3200整除的也不是闰年,如1900年是平年,2000年是闰年,3200年不是闰年。

  1. /*WA*/
  2. #include <cstdio>
  3. int main(){
  4. int a;
  5. bool b;
  6. scanf("%d",&a);
  7. if(a % 4==0) b=1;
  8. if(a % 100 == 0 && a % 400 != 0) b=0;
  9. if(a % 3200 == 0) b=0;
  10. if(b) printf("Y");
  11. else printf("N");
  12. return 0;
  13. }
  1. /*AC*/
  2. #include <cstdio>
  3. int main(){
  4. int a;
  5. bool b;
  6. scanf("%d",&a);
  7. if (a % 4==0 && a % 100 !=0|| a % 4==0 && a % 400 ==0 && a % 3200 !=0) printf("Y");
  8. else printf("N");
  9. return 0;
  10. }

早就应该画个树状图来表示这种筛选了,不然逻辑关系很难理清吧。


[1.4] 20:求一元二次方程的根

描述
利用公式x1 = (-b + sqrt(b*b-4*a*c))/(2*a), x2 = (-b - sqrt(b*b-4*a*c))/(2*a)求一元二次方程ax2+ bx + c =0的根,其中a不等于0。
输入
输入一行,包含三个浮点数a, b, c(它们之间以一个空格分开),分别表示方程ax2 + bx + c =0的系数。
输出
输出一行,表示方程的解。
若b2 = 4 * a * c,则两个实根相等,则输出形式为:x1=x2=...。
若b2 > 4 * a * c,则两个实根不等,则输出形式为:x1=...;x2 = ...,其中x1>x2。
若b2 < 4 * a * c,则有两个虚根,则输出:x1=实部+虚部i; x2=实部-虚部i,即x1的虚部系数大于等于x2的虚部系数,实部为0时不可省略。实部 = -b / (2*a), 虚部 = sqrt(4*a*c-b*b) / (2*a)
所有实数部分要求精确到小数点后5位,数字、符号之间没有空格。
样例输入
样例输入1
1.0 2.0 8.0
样例输入2
1 0 1
样例输出
样例输出1
x1=-1.00000+2.64575i;x2=-1.00000-2.64575i
样例输出2
x1=0.00000+1.00000i;x2=0.00000-1.00000i

  1. /*WA1*/
  2. #include <cstdio>
  3. #include <cmath>
  4. int main()
  5. {
  6. double a,b,c;
  7. scanf("%lf%lf%lf",&a,&b,&c);
  8. if(b*b == 4 * a * c)
  9. {
  10. double m=(-b + sqrt(b*b-4*a*c))/(2*a);
  11. printf("x1=x2=%.5lf",m);
  12. }
  13. else if(b*b > 4 * a * c)
  14. {
  15. double m=(-b + sqrt(b*b-4*a*c))/(2*a);
  16. double n=(-b - sqrt(b*b-4*a*c))/(2*a);
  17. printf("x1=%.5lf x2=%.5lf",m,n);
  18. }
  19. else if(b*b < 4 * a * c)
  20. {
  21. double shi=-b / (2*a);
  22. double xv=sqrt(4*a*c-b*b) / (2*a);
  23. printf("x1=%.5lf+%.5lfi x2=%.5lf-%.5lfi",shi,xv,shi,xv);
  24. }
  25. return 0;
  26. }
  1. /*WA2*/
  2. #include <cstdio>
  3. #include <cmath>
  4. int main()
  5. {
  6. double a,b,c;
  7. scanf("%lf%lf%lf",&a,&b,&c);
  8. double d=b*b-4*a*c;
  9. if(d==0)
  10. {
  11. double m=(-b + sqrt(d))/(2*a);
  12. printf("x1=x2=%.5lf\n",m);
  13. }
  14. else if(d>0)
  15. {
  16. double m=(-b + sqrt(d))/(2*a);
  17. double n=(-b - sqrt(d))/(2*a);
  18. printf("x1=%.5lf;x2=%.5lf\n",m,n);
  19. }
  20. else if(d<0)
  21. {
  22. double shi=-b / (2*a);
  23. double xv=sqrt(-d) / (2*a);
  24. if(shi=0.00000) shi=-shi;
  25. printf("x1=%.5lf+%.5lfi;x2=%.5lf-%.5lfi\n",shi,xv,shi,xv);
  26. }
  27. return 0;
  28. }
  1. /*AC*/
  2. #include <cstdio>
  3. #include <cmath>
  4. int main()
  5. {
  6. double a,b,c;
  7. scanf("%lf%lf%lf",&a,&b,&c);
  8. double d=b*b-4*a*c;
  9. if(d==0)
  10. {
  11. double m=(-b + sqrt(d))/(2*a);
  12. printf("x1=x2=%.5lf\n",m);
  13. }
  14. else if(d>0)
  15. {
  16. double m=(-b + sqrt(d))/(2*a);
  17. double n=(-b - sqrt(d))/(2*a);
  18. printf("x1=%.5lf;x2=%.5lf\n",m,n);
  19. }
  20. else if(d<0)
  21. {
  22. double shi=-b / (2*a);
  23. double xv=sqrt(-d) / (2*a);
  24. if(shi==0.000000) shi=-shi;
  25. printf("x1=%.5lf+%.5lfi;x2=%.5lf-%.5lfi\n",shi,xv,shi,xv);
  26. }
  27. return 0;
  28. }

首先看懂题目是很关键的。其次c++里面对于0的定义是非常微妙的……要知道0和-0在二进制里是不一样的……谁知道这玩意儿是什么啊orz 反正不管怎么说……精度问题就是烦死了- =


[1.4] 21:苹果和虫子2

描述
你买了一箱n个苹果,很不幸的是买完时箱子里混进了一条虫子。虫子每x小时能吃掉一个苹果,假设虫子在吃完一个苹果之前不会吃另一个,那么经过y小时你还有多少个完整的苹果?
输入
输入仅一行,包括n,x和y(均为整数)。
输出
输出也仅一行,剩下的苹果个数
样例输入
10 4 9
样例输出
7
提示
注意:是要求完整的苹果数。

  1. /*WA*/
  2. #include <cstdio>
  3. #include <cmath>
  4. int main()
  5. {
  6. int n,x,y,miao;
  7. scanf("%d%d%d",&n,&x,&y);
  8. miao=y/x;
  9. if (y%x!=0) miao++;
  10. printf("%d",n-miao);
  11. return 0;
  12. }
  1. /*AC*/
  2. #include <cstdio>
  3. #include <cmath>
  4. int main()
  5. {
  6. int n,x,y,miao;
  7. scanf("%d%d%d",&n,&x,&y);
  8. miao=y/x;
  9. if (y%x>0) miao++;
  10. int wang=n-miao;
  11. if(wang<0) ## 标题 ##wang=0;
  12. printf("%d",wang);
  13. return 0;
  14. }

>


[1.5] 13:乘方计算

描述
给出一个整数a和一个正整数n,求乘方an。
输入
一行,包含两个整数a和n。-1000000 <= a <= 1000000,1 <= n <= 10000。
输出
一个整数,即乘方结果。题目保证最终结果的绝对值不超过1000000。
样例输入
2 3
样例输出
8

  1. /*WA*/
  2. #include <cstdio>
  3. int main(){
  4. int a,n;
  5. scanf("%d%d",&a,&n);
  6. for(int i=1;i<n;i++) a*=a;
  7. printf("%d",a);
  8. return 0;
  9. }
  1. /*AC*/
  2. #include <cstdio>
  3. int main(){
  4. int a,n,tot=1;
  5. scanf("%d%d",&a,&n);
  6. for(int i=1;i<=n;i++) tot=tot*a;
  7. printf("%d",tot);
  8. return 0;
  9. }

太粗心了……a*a的话每次都会多乘一大堆。类乘器为什么不用啊……


[1.6] 06:校门外的树

描述
某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米。我们可以把马路看成一个数轴,马路的一端在数轴0的位置,另一端在L的位置;数轴上的每个整数点,即0,1,2,……,L,都种有一棵树。
由于马路上有一些区域要用来建地铁。这些区域用它们在数轴上的起始点和终止点表示。已知任一区域的起始点和终止点的坐标都是整数,区域之间可能有重合的部分。现在要把这些区域中的树(包括区域端点处的两棵树)移走。你的任务是计算将这些树都移走后,马路上还有多少棵树。
输入
第一行有两个整数L(1 <= L <= 10000)和 M(1 <= M <= 100),L代表马路的长度,M代表区域的数目,L和M之间用一个空格隔开。接下来的M行每行包含两个不同的整数,用一个空格隔开,表示一个区域的起始点和终止点的坐标。
对于20%的数据,区域之间没有重合的部分;
对于其它的数据,区域之间有重合的情况。
输出
包括一行,这一行只包含一个整数,表示马路上剩余的树的数目。
样例输入
500 3
150 300
100 200
470 471
样例输出
298

  1. /*WA*/
  2. #include <cstdio>
  3. #include <cmath>
  4. int main(){
  5. int tree[10005];
  6. int n,m,a,b,tot=0;
  7. scanf("%d%d",&n,&m);
  8. for(int i=1;i<=m;i++)
  9. {
  10. scanf("%d%d",&a,&b);
  11. for(int j=a;j<=b;j++) tree[j]=1;
  12. }
  13. for(int i=0;i<=n;i++)
  14. {
  15. if(tree[i]==0) tot++;
  16. }
  17. printf("%d",tot);
  18. return 0;
  19. }
  1. /*AC*/
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <iostream>
  5. #include <cstring>
  6. using namespace std;
  7. int main(){
  8. int tree[10005];
  9. int n,m,a,b,tot=0;
  10. scanf("%d%d",&n,&m);
  11. for(int i=0;i<=n;i++) tree[i]=1;
  12. for(int i=1;i<=m;i++)
  13. {
  14. scanf("%d%d",&a,&b);
  15. for(int j=a;j<=b;j++) if(tree[j]==1) tree[j]=0;
  16. }
  17. for(int i=0;i<=n;i++)
  18. {
  19. if(tree[i]==1) tot++;
  20. }
  21. printf("%d\n",tot);
  22. return 0;
  23. }

所以说校门外的树,第一个位置tree[0]上面也是有树的啊- =


[1.6] 07:有趣的跳跃

描述
一个长度为n(n>0)的序列中存在“有趣的跳跃”当前仅当相邻元素的差的绝对值经过排序后正好是从1到(n-1)。例如,1 4 2 3存在“有趣的跳跃”,因为差的绝对值分别为3,2,1。当然,任何只包含单个元素的序列一定存在“有趣的跳跃”。你需要写一个程序判定给定序列是否存在“有趣的跳跃”。
输入
一行,第一个数是n(0 < n < 3000),为序列长度,接下来有n个整数,依次为序列中各元素,各元素的绝对值均不超过1,000,000,000。
输出
一行,若该序列存在“有趣的跳跃”,输出"Jolly",否则输出"Not jolly"。
样例输入
4 1 4 2 3
样例输出
Jolly

  1. /*WA*/
  2. #include <cstdio>
  3. #include <iostream>
  4. #include <cmath>
  5. #include <cstring>
  6. #include <algorithm>
  7. using namespace std;
  8. int main()
  9. {
  10. int n,a[3005];
  11. scanf("%d",&n);
  12. if(n==0)
  13. {
  14. printf("Jolly");
  15. return 0;
  16. }
  17. for(int i=1;i<=n;i++)
  18. {
  19. scanf("%d",&a[i]);
  20. if(i!=1) a[i-1]=abs(a[i]-a[i-1]);
  21. }
  22. sort(a+1,a+n+1);
  23. for(int i=1;i<=n-1;i++)
  24. {
  25. if(a[i]!=i)
  26. {
  27. printf("Not jolly");
  28. return 0;
  29. }
  30. }
  31. printf("Jolly");
  32. return 0;
  33. }
  1. /*AC*/
  2. #include <cstdio>
  3. #include <iostream>
  4. #include <cmath>
  5. #include <cstring>
  6. #include <algorithm>
  7. using namespace std;
  8. int main()
  9. {
  10. int n,a[3005];
  11. scanf("%d",&n);
  12. if(n==0)
  13. {
  14. printf("Jolly");
  15. return 0;
  16. }
  17. memset(a,0,sizeof(a));
  18. for(int i=1;i<=n;i++)
  19. {
  20. scanf("%d",&a[i]);
  21. if(i!=1) a[i-1]=abs(a[i]-a[i-1]);
  22. }
  23. sort(a+1,a+n);
  24. for(int j=1;j<=n-1;j++)
  25. {
  26. if(a[j]!=j)
  27. {
  28. printf("Not jolly");
  29. return 0;
  30. }
  31. }
  32. printf("Jolly");
  33. return 0;
  34. }

sort(a+1,a+n)的原因是,对于n的元素的差序列只有n-1个元素。
后面的差值也是一样的w
后来补上一个memset,不管怎么样以后还是记得清零一下吧- -


[1.13] 01:数制转换

描述
求任意两个不同进制非负整数的转换(2进制~16进制),所给整数在long所能表达的范围之内。
不同进制的表示符号为(0,1,...,9,a,b,...,f)或者(0,1,...,9,A,B,...,F)。
输入
输入只有一行,包含三个整数a,n,b。a表示其后的n 是a进制整数,b表示欲将a进制整数n转换成b进制整数。
a,b是十进制整数,2 =< a,b <= 16。
输出
输出包含一行,该行有一个整数为转换后的b进制数。输出时字母符号全部用大写表示,即(0,1,...,9,A,B,...,F)。
样例输入
15 Aab3 7
样例输出
210306

  1. //WA
  2. #include <cstdio>
  3. #include <cstring>
  4. int a,b,db[255],len,j;
  5. char n[105],bd[255],ans[105];
  6. long long qwq;
  7. void csh()
  8. {
  9. for(int i=0;i<=9;i++)
  10. {
  11. db['0'+i]=i;
  12. bd[i]='0'+i;
  13. }
  14. for(int i=10;i<=15;i++)
  15. {
  16. db['A'+i-10]=i;
  17. db['a'+i-10]=i;
  18. bd[i]='A'+i-10;
  19. }
  20. }
  21. void toshi()
  22. {
  23. long long now=1;
  24. for(int i=len;i>=1;i--)
  25. {
  26. qwq+=db[n[i]]*now;
  27. now*=a;
  28. }
  29. }
  30. void ton(long long k)
  31. {
  32. j=0;
  33. while(k!=0)
  34. {
  35. ans[j++]=bd[k % b];
  36. k=k/b;
  37. }
  38. }
  39. int main()
  40. {
  41. scanf("%d %s %d",&a,&n[1],&b);
  42. len=strlen(n+1);
  43. csh();
  44. toshi();
  45. ton(qwq);
  46. if(n == 0)
  47. printf("0\n");
  48. else
  49. {
  50. for(int i=j-1;i>=0;i--) printf("%c",ans[i]);
  51. }
  52. return 0;
  53. }
  1. //AC
  2. #include <cstdio>
  3. #include <cstring>
  4. int a,b,db[255],len,j;
  5. char n[105],bd[255],ans[105];
  6. long long qwq;
  7. void csh()
  8. {
  9. for(int i=0;i<=9;i++)
  10. {
  11. db['0'+i]=i;
  12. bd[i]='0'+i;
  13. }
  14. for(int i=10;i<=15;i++)
  15. {
  16. db['A'+i-10]=i;
  17. db['a'+i-10]=i;
  18. bd[i]='A'+i-10;
  19. }
  20. }
  21. void toshi()
  22. {
  23. long long now=1;
  24. for(int i=len;i>=1;i--)
  25. {
  26. qwq+=db[n[i]]*now;
  27. now*=a;
  28. }
  29. }
  30. void ton(long long k)
  31. {
  32. j=0;
  33. while(k!=0)
  34. {
  35. ans[j++]=bd[k % b];
  36. k=k/b;
  37. }
  38. }
  39. int main()
  40. {
  41. scanf("%d %s %d",&a,&n[1],&b);
  42. len=strlen(n+1);
  43. csh();
  44. toshi();
  45. ton(qwq);
  46. if(n == 0)
  47. printf("0\n");
  48. else
  49. {
  50. for(int i=j-1;i>=0;i--) printf("%c",ans[i]);
  51. }
  52. return 0;
  53. }

[1.13] 09:大整数乘法

总时间限制: 1000ms 内存限制: 65536kB
描述
求两个不超过200位的非负整数的积。
输入
有两行,每行是一个不超过200位的非负整数,没有多余的前导0。
输出
一行,即相乘后的结果。结果里不能有多余的前导0,即如果结果是342,那么就不能输出为0342。
样例输入
12345678900
98765432100
样例输出
1219326311126352690000

  1. //WA
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <algorithm>
  6. #include <iostream>
  7. #include <cstdlib>
  8. using namespace std;
  9. int main()
  10. {
  11. char a[205];
  12. gets(a);
  13. int b[205];
  14. memset(b,0,sizeof(b));
  15. for(int i=0;i<strlen(a);i++)
  16. {
  17. b[strlen(a)-i]=a[i]-'0';
  18. }
  19. char c[205];
  20. gets(c);
  21. int d[205];
  22. memset(d,0,sizeof(d));
  23. for(int i=0;i<strlen(c);i++)
  24. {
  25. d[strlen(c)-i]=c[i]-'0';
  26. }
  27. if(strlen(a)==1 && a[0]=='0'||strlen(c)==1 && c[0]=='0')
  28. {
  29. printf("0");
  30. return 0;
  31. }//如果有一个字符串是0,输出0
  32. int miao[205];
  33. memset(miao,0,sizeof(miao));
  34. for(int i=1;i<=strlen(a);i++)
  35. {
  36. for(int j=1;j<=strlen(c);j++)
  37. {
  38. miao[i+j-1]+=b[i]*d[j];
  39. }
  40. }
  41. int ha=max(strlen(a),strlen(c));
  42. for(int i=1;i<=ha;i++)//进位
  43. {
  44. if(miao[i]>=10)
  45. {
  46. miao[i+1]+=miao[i]/10;
  47. miao[i]=miao[i] % 10;
  48. }
  49. }
  50. if(miao[strlen(a)+strlen(c)]!=0) ha=strlen(a)+strlen(c);
  51. else ha=strlen(a)+strlen(c)-1;
  52. for(int i=ha;i>0;i--) printf("%d",miao[i]);
  53. return 0;
  54. }
  1. //AC

消除前导零!


[1.13] 10:判决素数个数

总时间限制: 1000ms 内存限制: 65536kB
描述
输入两个整数X和Y,输出两者之间的素数个数(包括X和Y)。
输入
两个整数X和Y(1 <= X,Y <= 105)。
输出
输出一个整数,表示X,Y之间的素数个数(包括X和Y)。
样例输入
1 100
样例输出
25

  1. //WA
  2. #include <cstdio>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <cmath>
  6. using namespace std;
  7. bool pd(int x)
  8. {
  9. if(x==1) return false;
  10. if(x==2) return true;
  11. for(int i=2;i*i<=x;i++)
  12. {
  13. if(x % i==0) return false;
  14. }
  15. return true;
  16. }
  17. int main()
  18. {
  19. int a,b,tot=0;
  20. scanf("%d%d",&a,&b);
  21. for(int i=a;i<=b;i++)
  22. {
  23. if(pd(i)) tot++;
  24. }
  25. printf("%d\n",tot);
  26. return 0;
  27. }
  1. //AC
  2. #include <cstdio>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <cmath>
  6. using namespace std;
  7. bool pd(int x)
  8. {
  9. if(x==1) return false;
  10. if(x==2) return true;
  11. for(int i=2;i*i<=x;i++)
  12. {
  13. if(x % i==0) return false;
  14. }
  15. return true;
  16. }
  17. int main()
  18. {
  19. int a,b,tot=0;
  20. scanf("%d%d",&a,&b);
  21. if(a>b) swap(a,b);
  22. for(int i=a;i<=b;i++)
  23. {
  24. if(pd(i)) tot++;
  25. }
  26. printf("%d\n",tot);
  27. return 0;
  28. }

我是真的要吐槽OJ的数据……这道破题我调了20多分钟,然而看了评论区好心人才知道区间可能有l>r的情况……好气


[1.13]39:多项式输出

描述
一元 n 次多项式可用如下的表达式表示:

f(x)=anxn+an-1xn-1+...+a1x+a0,an≠0

其中,aixi称为i次项,ai称为i次项的系数。给出一个一元多项式各项的次数和系数,请按照如下规定的格式要求输出该多项式:

  1. 多项式中自变量为x,从左到右按照次数递减顺序给出多项式。

  2. 多项式中只包含系数不为0的项。

  3. 如果多项式n次项系数为正,则多项式开头不出现“+”号,如果多项式n次项系数为负,则多项式以“-”号开头。

  4. 对于不是最高次的项,以“+”号或者“-”号连接此项与前一项,分别表示此项系数为正或者系数为负。紧跟一个正整数,表示此项系数的绝对值(如果一个高于0次的项,其系数的绝对值为1,则无需输出1)。如果x的指数大于1,则接下来紧跟的指数部分的形式为“x^b”,其中b为x的指数;如果x的指数为1,则接下来紧跟的指数部分形式为“x”; 如果x的指数为0,则仅需输出系数即可。

  5. 多项式中,多项式的开头、结尾不含多余的空格。

输入
共有2 行:
第一行 1 个整数 n,表示一元多项式的次数。
第二行有 n+1 个整数,其中第 i 个整数表示第 n-i+1 次项的系数,每两个整数之间用空格隔开。

1 ≤ n ≤ 100,多项式各次项系数的绝对值均不超过100。
输出
共1行,按题目所述格式输出多项式。
样例输入
样例 #1:
5
100 -1 1 -3 0 10

样例 #2:
3
-50 0 0 1
样例输出
样例 #1:
100x^5-x^4+x^3-3x^2+10

样例 #2:
-50x^3+1

  1. //WA
  2. #include <cstdio>
  3. int n;
  4. int xs;
  5. int main()
  6. {
  7. scanf("%d",&n);
  8. for(int i=n;i>=0;i--)
  9. {
  10. scanf("%d",&xs);
  11. if(i==0 && xs!=0)
  12. {
  13. if(xs>0) printf("+");
  14. if(xs==0) return 0;
  15. printf("%d",xs);
  16. }
  17. else if(xs==-1) printf("-x^%d",i);
  18. else if(xs<0 || i==n) printf("%dx^%d",xs,i);
  19. else if(xs>0 && xs!=1) printf("+%dx^%d",xs,i);
  20. else if(xs==1) printf("+x^%d",i);
  21. else if(xs==0) continue;
  22. }
  23. return 0;
  24. }
  1. //AC
  2. #include<iostream>
  3. using namespace std;
  4. int main(){
  5. int i,n,a,b=0;
  6. cin>>n;
  7. for(i=n;i>0;i--)
  8. {
  9. cin>>a;
  10. if(a==0)continue;
  11. if(b&&a>0)cout<<"+";
  12. if(a==-1)cout<<"-";
  13. else if(a!=1)cout<<a;
  14. if(i!=1)cout<<"x^"<<i;
  15. else cout<<"x";
  16. b=1;
  17. }
  18. cin>>a;
  19. if(a>0)cout<<"+"<<a;
  20. if(a<0)cout<<a;
  21. }

参考了一下右边的大佬的代码……主要是运算符的逻辑关系出了问题,画了个树状图以后明白了- =

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注