C语言 之 PTA乙级错误集锦

1,很大很大的数输入,并各位加和  PTA-1001

#include <stdio.h>
#include <math.h> 
int main(){
    int sum=0,count=0,item,i=0,num,j;
    int Sum;
    char S=getchar();
	while(S!='\n'){
		sum+=S-'0';
		S=getchar();
	}
    printf("%d",sum);
return 0;
}

输入:1234567890987654321123456789
输入:135

2,输出不一致  PTA-1004

//输入n个学生信息,输出分数最高和最低的两个人
#include <stdio.h>
struct student{
    char name[10];
    char count[10];
    int score;
};
int main(){
    int i,n;
    struct student stu;
    struct student max;
    struct student min;
    scanf("%d",&n);
    for(i=0;i<n;i++){
        scanf("%s %s %d",stu.name,stu.count,&stu.score);
        if(i==0){
            max=stu;
            min=stu;
        }
        if(max.score<stu.score){
            max=stu;
        }
        if(min.score>stu.score){
            min=stu;
        }
    }
    printf("%s %s\n",max.name,max.count);
    printf("%s %s",min.name,min.count);
    return 0;
}

输入:
3
Joe Math990112 89
Mike CS991301 100
Mary EE990830 95

输出:
Mike CS991301
Joe Math990112Y      多了一个 Y      溢出导致;改下数组大小即可解决


#include <stdio.h>
struct student{
    char name[11];      注意大小改为11
    char count[11];
    int score;
};
int main(){
    int i,n;
    struct student stu;
    struct student max;
    struct student min;
    scanf("%d",&n);
    for(i=0;i<n;i++){
        scanf("%s %s %d",stu.name,stu.count,&stu.score);
        if(i==0){
            max=stu;
            min=stu;
        }
        if(max.score<stu.score){
            max=stu;
        }
        if(min.score>stu.score){
            min=stu;
        }
    }
    printf("%s %s\n",max.name,max.count);
    printf("%s %s",min.name,min.count);
    return 0;
}

3,PTA-1005

%取余,int和int之间才可以

pow( )求次幂的函数结果是浮点型

#include <stdio.h>
#include <math.h>
int num=10;
num/pow(10,2)  //报错


//想不报错的话
int num=10;
int num_1;
num_1=pow(10,2);
num/num_1;

4,多次输入字符串 PTA-1003

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(){
    int n,i;
    int item;
    scanf("%d",&n);
    getchar();
    char* str[10];
    char Str[100];
    for(i=0;i<n;i++){
        gets(Str);
        
        str[i]=(char*)malloc(strlen(Str)*sizeof(char));
        strcpy(str[i],Str);
    }
return 0;
}

5, PTA-1009  更新字符串

1-> Hello World Here I Come
2-> Hello World Here I
3-> Hello World Here
4-> Hello World
.......
    for(i=len;i>=0;i--){
        if(str[i]==' '){
            str[i]='\0';  //重点!!!
        }
    }

6,PTA 1010输入多个未知数字

#include <stdio.h>
int main(){
    int num[1000];
    int i=0,j;
    char c; 
    do{
    	scanf("%d",&num[i++]);
	}while((c=getchar())!='\n');
return 0;
}

太废了,这都不会写了,得多练练了

7,PTA-1011 比较大小  A+B>C

#include <stdio.h>
struct num{
    double A;
    double B;
    double C;
};
int main(){
    int i,T;
    scanf("%d",&T);
    struct num number[T];
    for(i=0;i<T;i++){
        scanf("%lf %lf %lf",&number[i].A,&number[i].B,&number[i].C);
    }
    for(i=0;i<T;i++){
        if(number[i].A+number[i].B>number[i].C){
            printf("Case #%d: true\n",i+1);
        }else{
            printf("Case #%d: false\n",i+1);
        }
    }
    return 0;
}

这个代码,刚开始的时候A,B,C定义的都是整型,然而答案都错误。改为浮点型才成功。浮点型更加准确应该是一方面

8,字符型多次输入

int main(){
    int N;
    char x,y;
    int i;
    scanf("%d",&N);
    for(i=0;i<N;i++){
    	getchar();
        scanf("%c %c",&x,&y);
    }

我就截取了一部分。刚开始我没有加 getchar()这个语句,所以每次输入的时候,再次输出总是不对,或许以后使用字符型输入时,遇到此问题可以试试getchar()

9,字符串输入不用加 &

这个总是一直忘

#include <stdio.h>
int main(){
    char name[80];
    scanf("%s",name);
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值