|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
各位大佬帮忙瞅瞅,才输入完程序就崩溃了!!!
- #include <stdio.h>
- #include <string.h>
- int isJYZ(int n); //判断校验是否正确
- void BJT(int UTC); //将UTC时间换成北京时间
- int main()
- {
- char s[127],s1[127],a[127],b[127];
- //scanf("%s\n",&s);
- //strcpy(s1,s);
- int i1,i2 = 0,sum = 0;
- int length1,length2;
- //length1 = sizeof(s);
- length1 = 0;
- do{
- scanf("%s",&s);
- strcat(s1,s);
- length1 += sizeof(s);
- }while(strcmp(s,'END') != 0);
-
- while(1){
- strrchr(a,strrchr(s1,'));
- length2 = sizeof(a);
- sum += length2;
- for( i1=0 ; i1< length2 ; i1++ ){
- if(i2 < 16){
- b[i2] = a[i1];
- }else{
- if(i1 >= length2-2) b[i2] = a[i1];
- }
-
- if(a[i1] = ','){
- i2++;
- }
- }
- if(b[2]=='A' && isJYZ(b[16])){
- BJT(b[1]);
- break;
- }
- for( i1=0 ; i1<length1-sum ; i1++ ){
- s1[i1] = s[i1];
- }
- }
-
- return 0;
- }
- int isJYZ(int n)
- {
- int i = 0; //校验不正确
- int a,b;
- a = b/10;
- int y = 1;
- while(a < b%10){
- y ^= a;
- a++;
- }
- if(y%65536 == n){
- i = 1; //校验正确
- }
- return i;
- }
- void BJT(int UTC){
- int a1,a2,b,c;
- a1 = UTC/10000;
- b = UTC%10000/100;
- c = UTC%10000%100;
- if(a1 >= 1 && a1 <= 16){
- a2 = a1 + 8;
- }else{
- a2 = a1 + 8 - 24;
- }
- if(a2 < 10){
- printf("0%d",a2);
- }else{
- printf("%d",a2);
- }
- if(b < 10){
- printf("0%d",b);
- }else{
- printf("%d",b);
- }
- if(c < 10){
- printf("0%d",c);
- }else{
- printf("%d",c);
- }
- }
复制代码
本帖最后由 超凡天赐 于 2017-5-8 17:15 编辑
好吧,这一题我已经帮你做出来了,你写的代码我看不懂,  所以我就自己写了一个。
其实这一题一点也不难,要放到竞赛,真是比送分还送分。就是题目有点长。这么长的题目,我们给它浓缩一下:
先验证‘$’和‘*’之间的字符累异或后的值(其实和累乘一个道理)是否等于‘*’后的两位数(这个数是16位进制的);
然后再将第一个逗号后的UCT时间转化为北京时间(其实就是将逗号后的两个数加上8,如果大于24就要剪去24;如果小于10,前面就要补零,比如02)。
当然了上面的只不过是我们的猜想,还要验证一下。写一个小代码
- #include <stdio.h>
- #include <string.h>
- int main()
- {
- char a[100];
- scanf("%s",a);
- long length;
- length=strlen(a);
- int i;
- int product=0;
- for(i=0;i<length;i++)
- product^=a[i];
- int result;
- result=product%65536;
- printf("%x",result);
- }//GPRMC,024813.640,A,3158.4608,N,11848.3737,E,10.05,324.27,150706,,,A
复制代码
我们输入一下题目中所给的数据
- GPRMC,024813.640,A,3158.4608,N,11848.3737,E,10.05,324.27,150706,,,A
复制代码
输出的结果为
 对了,好这下我们要用代码来描绘算法了。
- #include <stdio.h>
- #include <string.h>
- int verification(char str[])
- {
- int a,b;
- int location=0;
- while(str[location]!='*')
- {
- location++;
- }
- if(str[location+1]>='A'&&str[location+1]<='F')
- a=str[location+1]-'A'+10;
- else
- a=str[location+1]-'0';
- if(str[location+2]>='A'&&str[location+2]<='F')
- b=str[location+2]-'A'+10;
- else
- b=str[location+2]-'0';
- int proof_text_value;
- proof_text_value=a*16+b;//proof_text_value转化为10进制数
- int product=0,i;
- for(i=1;i<=location-1;i++)
- product^=str[i];
- product=product%65536;//product为10进制数
- if(product==proof_text_value)
- return 1;
- else
- return 0;
- }
- void transfer_time(char str[])//只需要对location+1到location+2位数字进行转换
- {
- int a,b;
- int location=0;
- while(str[location]!=',')
- {
- location++;
- }
- a=str[location+1]-'0';
- b=str[location+2]-'0';
- int UCT_time,Beijing_time;
- UCT_time=a*10+b;
- Beijing_time=UCT_time+8;
- if(Beijing_time>=24)
- printf("%d:%c%c:%c%c\n",Beijing_time-24,str[location+3],str[location+4],str[location+5],str[location+6]);
- else if(Beijing_time<10)
- printf("0%d:%c%c:%c%c\n",Beijing_time,str[location+3],str[location+4],str[location+5],str[location+6]);
- else
- printf("%d:%c%c:%c%c\n",Beijing_time,str[location+3],str[location+4],str[location+5],str[location+6]);
- }
- int main()
- {
- char a[100][100];
- char b[10]="END";
- int i=0,n;
- do
- {
- scanf("%s",a[i]);
- i++;
- }while(strcmp(a[i-1],b)!=0);
- for(n=0;n<i-1;n++)
- if(verification(a[n])==1)
- transfer_time(a[n]);
- return 0;
- }
复制代码
我们下面要输入数据,老天保佑不要错
- $GPRMC,024813.640,A,3158.4608,N,11848.3737,E,10.05,324.27,150706,,,A*50
- END
复制代码
对了对了
ok,但是我还要提醒你,  ,在竞赛中,那群变态会找一大些数据来测试,一个不通过抠分。超时也会扣分,甚至0分。所以这个代码有可能不会得100  。
不过这个题目有一点我不明白,那就是我一开始认为这个字符数组长度是固定的。因为好像每一项都是固定的。不过后来我看到他的题目说长度是不固定的。所以一开始我的代码是
- #include <stdio.h>
- #include <string.h>
- int main()
- {
- char a[100];
- scanf("%s",a);
- long length;
- length=strlen(a);
- int i;
- int product=0;
- for(i=0;i<length;i++)
- product^=a[i];
- int result;
- result=product%65536;
- printf("%x",result);
- }//GPRMC,024813.640,A,3158.4608,N,11848.3737,E,10.05,324.27,150706,,,A
- //其实依据你这道题的意思,刚开始时,字符长度已经确定了,为71。
- #include <stdio.h>
- #include <string.h>
- int verification(char str[])
- {
- int a,b;
- if(str[69]>='A'&&str[69]<='F')
- a=str[69]-'A'+10;
- else
- a=str[69]-'0';
- if(str[69]>='A'&&str[69]<='F')
- b=str[70]-'A'+10;
- else
- b=str[70]-'0';
- int proof_text_value;
- proof_text_value=a*16+b;//proof_text_value转化为10进制数
- int product=0,i;
- for(i=1;i<=67;i++)
- product^=str[i];
- product=product%65536;//product为10进制数
- if(product==proof_text_value)
- return 1;
- else
- return 0;
- }
- void transfer_time(char str[])//只需要对7到8位数字进行转换
- {
- int a,b;
- a=str[7]-'0';
- b=str[8]-'0';
- int UCT_time,Beijing_time;
- UCT_time=a*10+b;
- Beijing_time=UCT_time+8;
- if(Beijing_time>=24)
- printf("%d:%c%c:%c%c\n",Beijing_time-24,str[9],str[10],str[11],str[12]);
- else if(Beijing_time<10)
- printf("0%d:%c%c:%c%c\n",Beijing_time,str[9],str[10],str[11],str[12]);
- else
- printf("%d:%c%c:%c%c\n",Beijing_time,str[9],str[10],str[11],str[12]);
- }
- int main()
- {
- char a[100][100];
- char b[10]="END";
- int i=0,n;
- do
- {
- scanf("%s",a[i]);
- i++;
- }while(strcmp(a[i-1],b)!=0);
- for(n=0;n<i;n++)
- if(verification(a[n])==1)
- transfer_time(a[n]);
- return 0;
- }
复制代码
有点啰嗦,但我希望你能看完。
|
|