zoukankan      html  css  js  c++  java
  • 习题3-1 Score UVa1585

    头一次在OJ上做题,捣鼓了好久终于AC了....QAQ~

    ▲注意的地方:

    • 首先想好自己写的语言,如果是用C写的话就选择ANSI C(C89不支持C99和C11,所以for(int i...)的语法就会报错),C++就选C++……
    • 看了其他人的答案才知道要写T次,所以要补scanf("%d",&T);

    UVa使用邮箱反馈是否正确的做法,正是让我有点操蛋....哎,纪念下第一次提交。

    以下是本人做法:

    #include <stdio.h>
    #include <string.h>
    
    #define maxn 85
    
    char s[maxn];
    
    int main()
    {
    	int T;
    	scanf("%d",&T);	//T次输出
    	while(T--)
    	{	int i;
    		int sum = 0;
    		int nlen = strlen(s);
    		scanf("%s",s);	
    		for ( i=0; i<nlen; i++){	//依次判别每个字符
    			if (s[i] == 'O') {
                    sum ++;
    				if(i>0){			//如果i=0,则j--就成负数了
                    int j = i;
    				while(j--) {if(s[j]=='O') sum++; else break;}
     //解决的关键:可由观察得出,第3个O得3分可以理解为本身的1分加上前两个O的每个一分
     //所以做法为:当前O前面有n个O,sum就多加n分,碰到X就退出(避免跟X之前的O重复计算)
                    }
                }
    		}
    		printf("%d
    ",sum );}
    
    	return 0;
    }
    

    mrcrack的做法也是挺好的。

    #include <stdio.h>
    #include <string.h>
    
    int main(){
        int T;
        char s[100];
        int len;
        int i;
        int value;
        int sum;
        scanf("%d",&T);
        while(T--){
            scanf("%s",s);
            len=strlen(s);
            value=0;
            sum=0;
            for(i=0;i<len;i++){//扫描字符串,每次必加value的值 
                if(s[i]=='O')
                    value++;
                else//遇到'X' 
                    value=0;
                sum+=value;
            }
            printf("%d
    ",sum);
        }
        return 0;
    }
    

    //环境Dev-cpp4.9.9.2
    
    #include <stdio.h>
    #include <string.h>
    
    char s[10000];
    int main(){
        int T;
        int len;
        int i;
        int k;
        int sum;
        scanf("%d",&T);
        while(T--){
            sum=0;
            scanf("%s",s);
            len=strlen(s);
            for(i=0;i<len;i++){
                if(s[i]=='O'){//找到'O',对子串进行处理 
                    k=0;
                    while(s[i+k]=='O'){//连续'O'处理 
                        sum+=k+1;
                        k++;//移动到子串下一个字母 
                    }
                    i=i+k;//将i值移到连续'O' 子串之外处理 
                }
            }
            printf("%d
    ",sum);
        }
        return 0;
    }
  • 相关阅读:
    LeetCode Find Duplicate File in System
    LeetCode 681. Next Closest Time
    LeetCode 678. Valid Parenthesis String
    LeetCode 616. Add Bold Tag in String
    LeetCode 639. Decode Ways II
    LeetCode 536. Construct Binary Tree from String
    LeetCode 539. Minimum Time Difference
    LeetCode 635. Design Log Storage System
    LeetCode Split Concatenated Strings
    LeetCode 696. Count Binary Substrings
  • 原文地址:https://www.cnblogs.com/nymrli/p/9543575.html
Copyright © 2011-2022 走看看