一路走下去….
2012年01月17日软件技术

评论关闭

POJ1019

#include "stdio.h"
#include <math.h>
#define SUM_MAX1 45
#define SUM_MAX2 9045
#define SUM_MAX3 1395495
#define SUM_MAX4 189414495
char pre45bits [46]={"112123123412345123456123456712345678123456789"};
unsigned int i,num,position,pos,maxnumber,minnumber,tempvalue1,tempvalue2,current,gap,minnumber2;
unsigned int calc1,calc2,calc3,x,y,z;
unsigned int total=0;
double p=0;
double bits;

void getValue2(unsigned int pos){
     minnumber=9;
     while(minnumber<100){
          tempvalue1=minnumber*minnumber - 8*minnumber+36;
          tempvalue2=(minnumber+1)*(minnumber+1)-8*(minnumber+1)+36;
          if((pos>=tempvalue1) && (pos<tempvalue2)){
              current = tempvalue1;
              gap=pos-tempvalue1;
              break;
          }
          minnumber++;
    }
    if(gap==0){
          printf("%d\n",minnumber%10);
       }
    else if(gap <10){
          printf("%d\n",gap);
       }
    else{
           calc1 = (gap-9)/2;
           if(((gap-9)%2)==0){
             printf("%d\n",(calc1+9)%10);
           }else{
             printf("%d\n",(calc1+10)/10);
          }
       }
}

void getValue3(unsigned int pos){
     minnumber=99;
     while(minnumber<1000){
          tempvalue1=minnumber*189-9666+3*(minnumber-98)*(minnumber-99)/2;
          minnumber2=minnumber+1;
          tempvalue2=minnumber2*189-9666+3*(minnumber2-98)*(minnumber2-99)/2;
          if((pos>=tempvalue1) && (pos<tempvalue2)){
              current = tempvalue1;
              gap=pos-tempvalue1;
              break;
          }
          minnumber++;
    }
        if(gap==0){
          printf("%d\n",minnumber%10);
       }
       else if(gap <10){
             printf("%d\n",gap);
       }else if(gap-9 <=180){
          calc1 = (gap-9)/2;
          if(((gap-9)%2)==0){
             printf("%d\n",(calc1+9)%10);
           }else{
             printf("%d\n",(calc1+10)/10);
          }
       }else{
          calc2 = (gap-189)/3;
          if(((gap-189)%3)==1){
             printf("%d\n",(calc2+100)/100);
          }
          if(((gap-189)%3)==2){
             printf("%d\n",((calc2+100)%100)/10);
          }
          if(((gap-189)%3)==0){
             printf("%d\n",(calc2+99)%10%10 );
          }
       }
}

void getValue4(unsigned int pos){
     minnumber=999;
     while(minnumber){
          tempvalue1=minnumber*2889-1490616+2*(minnumber-999)*(minnumber-998);
          minnumber2=minnumber+1;
          tempvalue2=minnumber2*2889-1490616+2*(minnumber2-999)*(minnumber2-998);
          if((pos>=tempvalue1) && (pos<tempvalue2)){
              current = tempvalue1;
              gap=pos-tempvalue1;
              break;
          }
          minnumber++;
    }
       if(gap==0){
          printf("%d\n",minnumber%10);
       }
       else if(gap <10){
             printf("%d\n",gap);
       }else if(gap-9 <=180){
          calc1 = (gap-9)/2;
          if(((gap-9)%2)==0){
             printf("%d\n",(calc1+9)%10);
           }else{
             printf("%d\n",(calc1+10)/10);
          }
       }else if(gap-189<=2700){
          calc2 = (gap-189)/3;
          if(((gap-189)%3)==1){
             printf("%d\n",(calc2+100)/100);
          }
          if(((gap-189)%3)==2){
             printf("%d\n",((calc2+100)%100)/10);
          }
          if(((gap-189)%3)==0){
             printf("%d\n",(calc2+99)%10 );
          }
       }else{
          calc3= (gap-2889)/4;
          if(((gap-2889)%4)==1){
             printf("%d\n",(calc3+1000)/1000);
          }
          if(((gap-2889)%4)==2){
             printf("%d\n",(calc3+1000)%1000/100);
          }
          if(((gap-2889)%4)==3){
             printf("%d\n",(calc3+1000)%100/10);
          }
          if(((gap-2889)%4)==0){
             printf("%d\n",(calc3+99)%10);
          }
       }
}

void getValue5(unsigned int pos){
     minnumber=9999;
     while(minnumber<33000){
           x=minnumber-9999;
           tempvalue1 = 189414495+38889*x + 5*(x+1)*x/2;
           minnumber2 = x+1;
          tempvalue2 = 38889*minnumber2 + 5*(minnumber2+1)*minnumber2/2+189414495;
          if((pos>=tempvalue1) && (pos<tempvalue2)){
              current = tempvalue1;
              gap=pos-tempvalue1;
              break;
          }
          minnumber++;
    }
       if(gap==0){
          printf("%d\n",minnumber%10);
       }
       else if(gap <10){
             printf("%d\n",gap);
       }else if(gap-9 <=180){
          calc1 = (gap-9)/2;
          if(((gap-9)%2)==0){
             printf("%d\n",(calc1+9)%10);
           }else{
             printf("%d\n",(calc1+10)/10);
          }
       }else if(gap-189<=2700){
          calc2 = (gap-189)/3;
          if(((gap-189)%3)==1){
             printf("%d\n",(calc2+100)/100);
          }
          if(((gap-189)%3)==2){
             printf("%d\n",((calc2+100)%100)/10);
          }
          if(((gap-189)%3)==0){
             printf("%d\n",(calc2+99)%10 );
          }
       }else if(gap-2889<36000){
          calc3= (gap-2889)/4;
          if(((gap-2889)%4)==1){
             printf("%d\n",(calc3+1000)/1000);
          }
          if(((gap-2889)%4)==2){
             printf("%d\n",(calc3+1000)%1000/100);
          }
          if(((gap-2889)%4)==3){
             printf("%d\n",(calc3+1000)%100/10);
          }
          if(((gap-2889)%4)==0){
             printf("%d\n",(calc3+999)%10);
          }
       }
       else{
           calc3= (gap-38889)/5;
          if(((gap-38889)%5)==1){
             printf("%d\n",(calc3+10000)/10000);
          }
          if(((gap-38889)%5)==2){
             printf("%d\n",(calc3+10000)%10000/1000);
          }
          if(((gap-38889)%5)==3){
             printf("%d\n",(calc3+10000)%1000/100);
          }
          if(((gap-38889)%5)==4){
             printf("%d\n",(calc3+10000)%100/10);
          }

          if(((gap-38889)%5)==0){
             printf("%d\n",(calc3+9999)%10);
          }
       }
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#else
#endif
scanf("%d",&num);
for(i=0;i<num;i++){
    scanf("%u",&position);
    if(position<=45){
       printf("%c\n",pre45bits[position-1]);
    }else if(position<=9045){
        getValue2(position);
    }else if(position<=1395495){
        getValue3(position);
    }else if(position<=189414495){
        getValue4(position);
    }else{
        getValue5(position);
    }

}

#ifndef ONLINE_JUDGE
fclose(stdin);
#else
#endif
return 0;
}

2012年01月16日软件技术

评论关闭

POJ1035

#include "stdio.h"

struct thedic{
     char wordname[17];
     int thelen;
    }thedic_p[10010];
int i =0;
int p =0;
int x=0;
int correct =0;
int j,count,len,mywordlen,totalnumber;
char myword[17];
char * tempword;
char* tempmyword;
char wordtopass[17];

//Replace one character
void replace(const char * wordpro,const char * dicword){
         int count =0;
         tempword = dicword;
         tempmyword = wordpro;
         for(j=0;j<strlen(tempword);j++){
             if(tempmyword[j] != tempword[j])count++;
             if(count > 1)return;
         }
         printf(" %s",tempword);
    }

//Add one character to myword
void add(const char * wordpro,const char *dicword){
         tempword = dicword;
         tempmyword = wordpro;
         for(j=0;j<=strlen(tempword);j++){
             if(tempmyword[j] != tempword[j]){
                len = strlen(tempmyword);
                while(len-- > j){
                      tempmyword[len+1]=tempmyword[len];
                }
                tempmyword[j] = tempword[j];
                if(!strcmp(tempmyword,tempword)){
                  printf(" %s",tempword);
                }
                return;
             }
        }
        return;
    }

//Delete one character from myword
void deleteone(const char * wordpro,const char * dicword){
        tempword = dicword;
        tempmyword = wordpro;

         for(j=0;j<strlen(tempmyword);j++){
             if(tempmyword[j] != tempword[j]){
                len = strlen(tempmyword);
                if(j <(len-1)){
                while(j++ < (len-1)){
                      tempmyword[j-1]=tempmyword[j];
                      tempmyword[j] ='\0';
                }
                }
                else if(j== (len-1)){
                      tempmyword[j] ='\0';
                }

                if(!strcmp(tempmyword,tempword)){
                  printf(" %s",tempword);
                }
                return;
             }
        }
        return;
    }

int main(void) {
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #else
    #endif
    while(scanf("%s",thedic_p[i++].wordname)){
          if(!strcmp(thedic_p[i-1].wordname,"#")){
          totalnumber = i-1;
          break;
          }
          thedic_p[i-1].thelen= strlen(thedic_p[i-1].wordname);
    }

     for(;;){

        for(i=0;i<17;i++){
            myword[i]='\0';
            wordtopass[i]='\0';
        }

        correct=0;
        scanf("%s",myword);
        if(!strcmp(myword,"#"))break;
        mywordlen=strlen(myword);

        for(p=0;p<totalnumber;p++){
            //printf("")
            if(!strcmp(myword,thedic_p[p].wordname)){
            printf("%s is correct\n",myword);
            correct=1;
            break;
            }
        }
        if(correct != 1){
        printf("%s:",myword);
        for(p=0;p<totalnumber;p++){
            strcpy(wordtopass,myword);
            if(abs(mywordlen-thedic_p[p].thelen)>1)continue;
            if(mywordlen==thedic_p[p].thelen)
               replace(wordtopass,thedic_p[p].wordname);
            if(mywordlen > thedic_p[p].thelen)
               deleteone(wordtopass,thedic_p[p].wordname);
            else
               add(wordtopass,thedic_p[p].wordname);
        }
        printf("\n");
     }
     }

}

2012年01月5日软件技术

评论关闭

POJ1007

#include "stdio.h"
int main(void) {
    int num;
    int length;
    char str_out[100][51];
    int index;
    int index1;
    int result[100];
    int a=0;
    int b=0;
    int min=0;
    int num2=0;
    int temp;
    int min_index;
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #else
    #endif

    scanf("%d%d",&length,&num);
    num2=num;
    temp=num2;
    while(num>0){
        scanf("%s", str_out[a]);
        if(num2 == 1){
           printf("%sn",str_out[a]);
           return 0;
        }
        result[a] = 0;
        for(index=0;index<length-1;index++){
            if(str_out[a][index] == 'A')continue;
            else{
               for(index1=index+1;index1<=length-1;index1++){
                   if(str_out[a][index] > str_out[a][index1])result[a]++;
               }
            }

        }
    num--;
    a++;
    }
    while(num2>0){
    if(result[b]==5000)
    {
        b++;
        continue;
    }
    min = result[b];
    min_index = b;

    for(index=0;index<temp-1;index++){
        if(min > result[index+1]){
           min = result[index+1];
           min_index =index+1;
        }
    }
    result[min_index] = 5000;
    num2--;
    printf("%sn",str_out[min_index]);
    min_index = 0;
}
    #ifndef ONLINE_JUDGE
    fclose(stdin);
    #else
    #endif
    return 0;

软件技术

评论关闭

POJ1005

#include "stdio.h"
#include "math.h"
int main(void) {
    int num;
    float x_value=0.0;
    float y_value=0.0;
    int a =1;
    int result;
    float length;
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #else
    #endif

    scanf("%d",&num);
    //printf("%dn",num);
    while(num>0){
         scanf("%G%G",&x_value,&y_value);
         //printf("%G  %Gn",x_value,y_value);
         length = sqrt(x_value*x_value + y_value*y_value);
         result =(3.1415926*length*length/100)+1;
        // printf("%dn",result);
printf("Property %d: This property will begin eroding in year %d.n", a,result);
         num--;
         a++;
    }
     printf("END OF OUTPUT.");
    //printf(Property 1: This property will begin eroding in year 1.);
    #ifndef ONLINE_JUDGE
    fclose(stdin);
    #else
    #endif
    return 0;
}

2012年01月4日软件技术

评论关闭

ONLINE_JUDGE

POJ上已经定义了ONLINE_JUDGE.
所以要用”ifndef”, 本地IDE中的宏定义删了.
scanf:
%f小数点后6位
%G和%g 小数点后2位,但是自动会去除末尾的0

#include "stdio.h"

int main(void) {
    float c;
    float result=0.0;
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #else
    #endif
    while((scanf("%G",&c))!=EOF){
          //printf("%Gn",c);
         result = result + c;
         //printf("%Gn",result);
    }

    printf("$%G",result/12);
    #ifndef ONLINE_JUDGE
    fclose(stdin);
    #else
    #endif
    return 0;
}

2011年12月26日软件技术

评论关闭

Linux shell定时器

网上找了个例子,简单实用如下.
可以有一个后台进程,在timer超时采取一些操作.

#!/bin/ksh
#Delaytime=0
AlarmHandler()
{
echo "Get SIGALAM"
KillSubProcs
exit 14
}
KillSubProcs()
{
#kill cbm
echo "it's time out,kill cbm here"
if [ $? -eq 0 ];then
echo "Sub-processes killed."
fi
}

SetTimer()
{
Delaytime=$1
echo $Delaytime
if [ $Delaytime  -ne 0 ];then
sleep $Delaytime  && kill -s 14 $$ &
#CHPROCIDS="$CHPROCIDS $!"
TIMERPROC=$!
fi
}
UnsetTimer()
{
echo "Start to unset timer"
kill $TIMERPROC
}

trap AlarmHandler 14
SetTimer 30
#CHPROCIDS="$CHPROCIDS $!"
#wait $!
sleep 40
UnsetTimer
echo "ALL Done."
exit 0

2011年12月20日软件技术

评论关闭

怪异的grep结果

原意是发现文件中IP地址为1.1.1.1的行(不需显示1.1.1.191这样的).但是写错了,忘记”.”是用于匹配任意字符的.但却发现有几条grep结果很奇怪.

根据GNU Grep的manual
“\<" Match the empty string at the beginning of word
"\>” Match the empty string at the end of word

所以当时我就写成了grep “\<1.1.1.1\>” 结果如下:
ksim@MxGms3:~> echo “1.1.1.1 games1″|grep “\<1.1.1.1\>”
1.1.1.1 games1
ksim@MxGms3:~> echo “1.1.1.191 games1″|grep “\<1.1.1.1\>”
1.1.1.191 games1

第二句执行结果,出乎我的意料.经人提醒,这里”.”匹配了任意字符.

于是我改成 grep “<1\.1\.1\.1>”
执行结果如下:
ksim@MxGms3:~> echo “1.1.1.191 games1″|grep “<1\.1\.1\.1>”
ksim@MxGms3:~> echo “1.1.1.1 games1″|grep “<1\.1\.1\.1>”
1.1.1.1 games1

后来,改成-w也可以: grep -w “1\.1\.1\.1″
执行结果如下:
ksim@MxGms3:~> echo “1.1.1.191 games1″|grep -w “1\.1\.1\.1″
ksim@MxGms3:~> echo “1.1.1.1 games1″|grep -w “1\.1\.1\.1″
1.1.1.1 games1

但是我依然不明白最早使用命令的输出结果,就是这一句:
ksim@MxGms3:~> echo “1.1.1.191 games1″|grep “\<1.1.1.1\>”
1.1.1.191 games1
为什么会把1.1.1.191显示出来., “.”是用于代表一个任意字符的,但只是代表一个任意字符,不能代表多个,这个怎么能匹配出来1.1.1.191. 同样的,我改成-w option,也是不理解:
ksim@MxGms3:~> echo “1.1.1.191 games1″|grep -w “1.1.1.1″
1.1.1.191 games1

但这又应该和”.” 操作符有关的,因为我又试了以下命令:
ksim@MxGms3:~> echo “121212121 games1″|grep “\<1212121\>”
ksim@MxGms3:~> echo “1212121 games1″|grep “\<1212121\>”
1212121 games1
把”.”换成2,就不会把121212121显示出来.
所以所有的问题归结到一个问题, 那就是”\<1.1.1.1\>” 和 -w “1.1.1.1″
这2个难道不是精确匹配” 4个1以及4个1中间夹带3个任意字符” ??
问题出在哪里?

查看GNU manul,可以看到下面一段:
-w, –word-regexp
Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word con-stituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-constituent characters are letters, digits, and the underscore.

也就是说-w只关心constituent characters, 像其他符号不影响匹配结果.比如:
ksim@MxGms3:~> echo “+abc++” |grep -w “abc”
+abc++
ksim@MxGms3:~> echo “+abc+abc+” |grep -w “abc”
+abc+abc+
ksim@MxGms3:~> echo “+ac+abc+” |grep -w “abc”
+ac+abc+
ksim@MxGms3:~> echo “+abc+ac+” |grep -w “abc”
+abc+ac+

而且发现”\<" "\>“的效果和-w一样:
ksim@MxGms3:~> echo “+abc+ac+” |grep “\
+abc+ac+

姑且认为两者是等价的吧.

2011年12月8日软件技术

评论关闭

URL2FILE

URL2File is a free Java application able to retrieve and save the content of a given URL to a local file.

URL2File Java console-mode application can be used by itself or within a shell script to retrieve the content of an HTML document or other file from a remote Internet server to a local file.A typical example would be to write a shell script, such as a Windows command prompt BATch file, to download multiple files.

Once the Java VM is successfully installed, copy URL2File.class to a directory of your choice and run it using the following syntax:
java URL2File

伤不起,这个不是开源的?找不到源代码.
具体JAVA code的实现可以如下:

import java.net.*;
import java.io.*;
class url2file{
public static void main(String args[])throws IOException
{
int num1;
num1=1;
File fi3=new File(args[1]);
RandomAccessFile fi4=new RandomAccessFile(fi3,"rw");
URL urll=null;
try{
        urll=new URL(args[0]);
        DataInputStream dis;
        dis = new DataInputStream(urll.openStream());
        while((num1=dis.read())!=-1)
        {
                fi4.write((char)num1);
        }
        fi4.close();
        dis.close();
}
catch(MalformedURLException e)
{
        System.out.println(e);
        fi4.close();
}
catch(IOException e)
{
        System.out.println(e);
        fi4.close();
}

}
}

2011年12月7日软件技术

评论关闭

C primer笔记

1. 对指针加1,等于对指针的值加上它指向的对象的字节的大小


2. date+2 =&date[2]
*(date+2) = date[2]

3. 函数原型或者函数定义头,可以用int * ar 代替int ar[ ]
int sum(int ar[],int n)
int sum(int * ar,int n)

4. 原型是允许省略名词的:
int sum(int * ,int );
int sum(int [],int);

5. *和++ 具有相同的优先级,但是它的结合是从右向左进行的

6. total += *start++;
先把指针start指向的数据加到total上,然后指针自增1

total += *++start;
指针先自增1,然后再使用指向的值加到total上

注意和(*start)++的区别

7. 函数值传递,copy一份.如果需要改变传入的参数,用指针. 传入数组必须用指针

8. 指向常量的指针:
double array [5]={1,2,3,4,5,};
const double * pd=array;
*pd =2;      /NOK
pd[2]=3;    /NOK
array[0]=2 /OK
pd ++;      /OK
通常把指向常量的指针用作函数参数,以表明函数不会用这个指针来修改数据, 同时使得函数可以使用声明为常量的数组

9. double * const pc =array;
指针不会指向别处,但是指针指向的数据可以更改

10.结构不仅可以作为参数传递给函数,也可以作为函数返回值返回

11. 应该用结构指针作为参数还是结构作为参数?
结构指针作为参数: 1) 执行比较快 2) 可以在较早的C上运行 3)但是缺少对数据的保护,不过可以用const来解决这个问题
结构作为参数: 1)函数处理的是原始结构的副本,比较安全 2)浪费时间和空间
12 结构需要存储字符串,尽量使用字符数组,少用字符指针

软件技术

评论关闭

Vxworks增加system call

Vxworks中增加system call的伪代码.
比如用户层可以获取kernel中的time tick.
#include <syscall.h>
#include <syscallLib.h>

unsigned short gettimdsp(void);

SYSCALL_RTN_TBL_ENTRY msGetTbl[] =
{
{(FUNCPTR) gettimdsp, 1, “gettimdsp”, 0}
};

unsigned short gettimdsp(void)
{
return timetck;
}

void usrAppInit (void)
{
syscallGroupRegister (2, “msgetforrtp”, 2, (SYSCALL_RTN_TBL_ENTRY*)&msGetTbl, 0);

}

返回顶部
Դ