跳至正文

Noip 2007 提高组 字符串的展开 解题报告

  • OI路程

  麻烦的题目,第一次只拿了30分,代码如下:

#include <stdio.h>
char str[101];
char ans[1500];
int i, j;
int a, b, c;
int spell = 0;

void init(void)
{
        scanf("%d%d%d\n", &a, &b, &c);
        if(a == 2){
                spell = 0x20;
        }
}

void put(char c1, char c2)
{
        int k;
        if(c1 > c2){
                return;
        }
        if(c == 1){
                if(a != 3){
                        for(k = 0; k < b; k++){
                                ans[j++] = c1 – spell;
                        }
                }else{
                        for(k = 0; k < b; k++){
                                ans[j++] = ;
                        }
                }
                put(c1 + 1, c2);
        }else{
                if(a != 3){
                        for(k = 0; k < b; k++){
                                ans[j++] = c2 – spell;
                        }
                }else{
                        for(k = 0; k < b; k++){
                                ans[j++] = 
;
                        }
                }
                put(c1, c2 – 1);
        }
}

void change(void)
{
        int k;
        if((str[i – 1] >= str[i + 1]) || 
                (isalpha(str[i – 1]) && !isalpha(str[i +1])) ||
                (isdigit(str[i – 1]) && !isdigit(str[i +1]))){
                                //忘记写!了 
                ans[j++] = str[i];
                return ;
        }
        put(str[i – 1] + 1, str[i + 1] – 1);
}

int main(void)
{
        int len;
//      freopen("abc.txt", "r", stdin);
        init();
        scanf("%s", str);
        len = strlen(str);
        //刚刚忘记给len赋值了. 
        for(i = j = 0; i < len; i++){
                if(str[i] == ‘-‘){
                        change();
                }else{
                        ans[j++] = str[i];
                }
        }
        ans[j] = ‘\0’;
        printf("%s\n", ans);
//      getch();
        return 0;
}

  后来认为是要处理整数,就是说比如:10-12展开就是101112,写了好久~!~!~!,还是错了,再一看数据,根本就不用。
  代码如下:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
char str[101];
int i, j;
int a, b, c;
int spell;

void init(void)
{
        scanf("%d%d%d\n", &a, &b, &c);
        if(a == 2){
                spell = 0x20;
        }
}

void putcha(char c1, char c2)
{
        int k;
        if(c1 > c2){
                return;
        }
        if(a == 3){
                for(k = 0; k < b; k++){
                        putchar();
                }
                putcha(c1 + 1, c2);
                return;
        }
        if(c == 1){
                for(k = 0; k < b; k++){
                        putchar(c1 – spell);
                }
                putcha(c1 + 1, c2);
        }else{
                for(k = 0; k < b; k++){
                        putchar(c2 – spell);
                }
                putcha(c1, c2 – 1);
        }
}

int getcount(int n)
{
        int m = 0;
        while(n){
                n /= 10;
                m++;
        }
        return m;
}

void putnum(int c1, int c2)
{
        int k, r;
        if(a == 3){
                while(c1 <= c2){
                        r = getcount(c1);
                        for(k = 0; k < r
b; k++){
                                putchar();
                        }
                        c1++;
                }
                return;
        }
        if(c == 1){
                while(c1 <= c2){
                        r = getcount(c1);
                        for(k = 0; k < b; k++){
                                printf("%d", c1);
                                j += r;
                        }
                        c1++;
                }
        }else{
                while(c1 <= c2){
                        r = getcount(c2);
                        for(k = 0; k < b; k++){
                                printf("%d", c2);
                                j += r;
                        }
                        c2–;
                }
        }
}

void change(void)
{
        int k;
        if((isalpha(str[i – 1]) && !isalpha(str[i + 1])) && (isdigit(str[i – 1]) && !isdigit(str[i + 1]))){
                                //忘记写!了 
                putchar(str[i]);
                return ;
        }
        if(isalpha(str[i – 1]) && (str[i – 1] < str[i + 1])){
                putcha(str[i – 1] + 1, str[i + 1] – 1);
        }else if(isdigit(str[i – 1])){
                int k, r, d;
                for(d = i – 1; d >= 0 && isdigit(str[d]); d–){
                        ;
                }
                sscanf(&str[d + 1], "%d%d", &k, &r);
                if(k < r){
                        putnum(k + 1, r – 1);
                }else{
                        putchar(‘-‘);
                }
        }else{
                        putchar(‘-‘);
        }
}

int main(void)
{
        int len;
        init();
        scanf("%s", str);
        len = strlen(str);
        //刚刚忘记给len赋值了. 
        for(i = j = 0; i < len; i++){
                if(str[i] == ‘-‘){
                        change();
                }else{
                        putchar(str[i]);
                }
        }
        printf("\n");
        return 0;
}
  正确代码还在撰写中。。。
  辛辛苦苦修改了一次,还只有70分,这个代码就不发上来了吧。
  根据数据修改了好几次,第一次没考虑到–的情况,第二次忘记考虑当a=2而转换数字时的情况。
  代码如下:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
char str[101];
int i;
int a, b, c;
int spell;

void init(void)
{
        scanf("%d%d%d\n", &a, &b, &c);
        if(a == 2){
                spell = 0x20;
        }
}

void output(char c1, char c2)
{
        int k;
        if(a == 3){
                while(c1 <= c2){
                        for(k = 0; k < b; k++){
                                putchar(
);
                        }
                        c1++;
                }
                return ;
        }
        if(isdigit(c1)){
                //要考虑数字但是a=2的情况 
                while(c1 <= c2){
                        for(k = 0; k < b; k++){
                                putchar(c1);
                        }
                        c1++;
                }
        }else if(c == 1){
                while(c1 <= c2){
                        for(k = 0; k < b; k++){
                                putchar(c1 – spell);
                        }
                        c1++;
                }
        }else{
                while(c1 <= c2){
                        for(k = 0; k < b; k++){
                                putchar(c2 – spell);
                        }
                        c2–;
                }
        }
}

void change(void)
{
        int k;
        if((i == 0) || (str[i – 1] >= str[i + 1])){
                putchar(‘-‘);
                return;
        }
        if((isdigit(str[i – 1]) && isdigit(str[i + 1])) || 
                (isalpha(str[i – 1]) && isalpha(str[i + 1]))){
                output(str[i – 1] + 1, str[i + 1] – 1);
        }else{
                putchar(‘-‘);
        }
}

int main(void)
{
        int len;
        init();
        scanf("%s", str);
        len = strlen(str);
        //刚刚忘记给len赋值了. 
        for(i = 0; i < len; i++){
                if(str[i] == ‘-‘){
                        change();
                }else{
                        putchar(str[i]);
                }
        }
        printf("\n");
        return 0;
}

发表回复

您的电子邮箱地址不会被公开。