Wall for Warriops3826

大小寫轉換
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main()
{
  char S[5000];
  while(scanf("%s",S)==1)
      {
         int a,N=strlen(S);
         for(a=0;a<=N;a++)
             if(S[a]>='a')
               printf("%c",S[a]-'a'+'A');
             else
               printf("%c",S[a]-'A'+'a');
          puts("");
      }
 return 0;   
}
Jan 19, 2018 by Warriops3826
凱亦

#include <stdio.h>

int calculus(int a);

int main()
{
    int a,b;
        
    scanf("%d",&a);
    
    printf("%d",calculus(a));
    return 0;
}
int calculus(int a)
{
    int b,c,d;
    b=a/12;
    c=a%12;
    d=b*50+c*5;
    
    return d;
    }
Jan 19, 2018 by Warriops3826
1.子字串出現的次數
2.魔方!!!!!!!!!
3.稀疏矩陣
4.11的倍數
5.最大的數 最小的數 出現最多次的數
6.共用了多少種英文字母
7.質數 非質數 emirp !!!!!!!!!!!!!!!!!!!!!!!!
8.置換車廂
9.骰子遊戲
10.唐.喬凡尼
Jan 19, 2018 by Warriops3826
1 #include <stdio.h>

#include <string.h>

int count(const char str[], const char key[]);

int main()

{

   char str[1000], key[1000];

   gets(str);

   gets(key);

   printf(“子字串共出現 %d 次”, count(str, key));

   return 0;

}

int count(const char str[], const char key[])

{

int len=strlen(key),i=0;

char *a=strstr(str,key);

while(a!=NULL)

{

i++;

a++;

a=strstr(a,key);

}

return i;

}



2 #include<stdio.h>

int main()
{
    int x ,y , n ,m[100][100]={0},k=1;
    
    scanf("%d",&n);
    while(k<=n*n)
    {
        if(k==1)
        {
            x=n/2;
            y=0;
        }
        else{
           if(m[(y-1+n)%n][(x+1)%n]==0)
           {
                y=(y-1+n)%n;
                x=(x+1)%n;
               }
               else{y=(y+1)%n;
               }    
        }  
        m[y][x]=k;
        k++;
    }
    for(y=0;y<n;y++){
    
      for(x=0;x<n;x++)
      {
          printf("%d ",m[y][x]);
          
      }
    printf("\n");}
 }


3 #include<stdio.h>
int main(){
    int a[100][100]={0};
    int b[100][3]={0};
    int i,j,k,x,y,c=0;
    
    scanf("%d%d",&y,&x);
    
    for(i=0;i<y;i++){
        for(j=0;j<x;j++){
            scanf("%d",&a[y][x]);
            if(a[y][x]!=0){
                c++;
                b[c][0]=i;
                b[c][1]=j;
                b[c][2]=a[y][x];
            }
        }
    }
    b[0][0]=y;
    b[0][1]=x;
    b[0][2]=c;
    for(k=0;k<c+1;k++){
        printf("%d %d %d\n",b[k][0],b[k][1],b[k][2]);
    }
}


4 #include<stdio.h>

int main()
{
    int ans,i,b=0,c=0;
    char a[1000]={0};
    scanf("%s",a);
    for(i=0;a[i]!='\0';i++)
    {
        if(i%2==0)
        b+=a[i]-'0';
        else
        c+= a[i]-'0';
    }
    if(b>c)
        ans=b-c;
    else
        ans=c-b;
    if(ans%11==0)
        printf("YES");
    else
        printf("NO");
    
    
    return 0;
}


5 #include<stdio.h>

int main() {
   int arr[1000];
   int n, i, j, max_length=0, max_length_num, current_num, current_num_length=0, temp;

   scanf("%d",&n);
   for (i=0;i<n;i++)
    {
       scanf("%d",&arr[i]);
       }

   for (i=0; i<n-1; i++)
   {
      for (j =i+1; j<n; j++)
      {
         if (arr[i] < arr[j])
         {
             temp =arr[i];
             arr[i] = arr[j];
             arr[j] = temp;
         }
      }
   }  

   current_num = arr[0];
   current_num_length = 1;
   for (i=1; i<n-1; i++)
   {
      if (current_num == arr[i])
      {
          current_num_length ++;
          if (current_num_length > max_length)
          {
              max_length = current_num_length;
              max_length_num = current_num;
          }
      }
      else
      {
          current_num_length = 1;    
      }
      current_num = arr[i];
  }
   printf("%d %d %d", arr[0], arr[n-1], max_length_num);
    return 0;
}

6  #include<stdio.h>
int count(const char str[]);
int main()
{
    char str[1000];
    gets(str);
    printf("共%d個字母",count(str));
    return 0;
}
int count(const char str[])
{
    char c[26]={0};
    int i,sum=0;
    for(i=0;str[i]!=0;i++)
    {
    if(str[i]>='A'&&str[i]<='Z')
    c[str[i]-'A']=1;
    else if(str[i]>='a'&&str[i]<='z')
    c[str[i]-'a']=1;
    }
    for(i=0;i<26;i++)
    sum+=c[i];
    return sum;
}


7  #include<stdio.h>

int main()
{
    int n, np;
    int j, k;
    scanf("%d", &n);
    for(j = 1, k = 0;j <= n;j ++)
    {
        if(n % j == 0)
            k ++;
    }
    if(k == 2)
    {
        printf("%d is ", n);
        np = 0;
        while(n > 0)
        {
            k = n % 10;
            n = n / 10;
            np = np * 10 + k;
        }
        for(j = 1, k = 0;j <= np;j ++)
        {
            if(np % j == 0)
                k ++;
        }
        if(k == 2)
        {
            printf("emirp.");
        }
        else
        {
            printf("prime.");
        }
    }
    else
    {
        printf("%d is not prime.", n);
    }
}


8 #include<stdio.h>

int main()
{
    int n;
    int i, j;
    int count;
    int c[100],store[100];
    int flag, a, temp;
    scanf("%d", &n);
    for(i=0;i<n;i++) {
        a=0;
        scanf("%d",&count);
        for(j=0;j<count;j++) {
            scanf("%d",&c[j]);
        }
        flag = 1;
        while(flag==1) {
            flag = 0;
            for(j=0;j<count-1;j++) {
                if(c[j]>c[j+1]) {
                    temp = c[j];
                    c[j] =c[j+1];
                    c[j+1] = temp;
                    flag =1;
                    a++;
                }
            }
        }
        store[i] = a;
    }
    printf("Optimal train swapping takes %d swaps.",store[0]);
    for(i=1;i<n;i++) {
        printf("\nOptimal train swapping takes %d swaps.", store[i]);
    }
    return 0;
}


9  #include <stdio.h>
    
    int dice[6]={1,6,2,5,3,4};
    
    void N(){
        int t=dice[0];
        dice[0]=dice[3];
        dice[3]=dice[1];
        dice[1]=dice[2];
        dice[2]=t;
    }
    void S(){
        int t=dice[0];
        dice[0]=dice[2];
        dice[2]=dice[1];
        dice[1]=dice[3];
        dice[3]=t;
    }
    void W(){
        int t=dice[0];
        dice[0]=dice[5];
        dice[5]=dice[1];
        dice[1]=dice[4];
        dice[4]=t;
    }
    void E(){
        int t=dice[0];
        dice[0]=dice[4];
        dice[4]=dice[1];
        dice[1]=dice[5];
        dice[5]=t;
    }
    int main(){
        int n,i,j=0,str[100]={0};
        char ch[10];
        scanf("%d",&n);
        do{
        dice[0]=1;
        dice[1]=6;
        dice[2]=2;
        dice[3]=5;
        dice[4]=3;
        dice[5]=4;
        for(i=0;i<n;i++){
            scanf("%s",ch);
        switch(ch[0]){
            case 'n':N();
            break;
            case 's':S();
            break;
            case 'w':W();
            break;
            case 'e':E();
            break;
            }
        }
        str[j]=dice[0];
        j++;
        scanf("%d",&n);
        }while(n!=0);
        for(i=0;i<j;i++){
            printf("%d",str[i]);
        if(i!=j-1){
            printf("\n");
        }
    }
}


10  #include<stdio.h>
#include<string.h>
int main(){
    char country[100][100];
    int count[100]={0},n,i,j,k,now=0;
    scanf("%d\n",&n);
    for(i=0;i<n;i++){
        char input[100];
        fgets(input,100,stdin);
        char *p;
        int find=0;
        p=strtok(input," ");
        if(now!=0){
            for(j=0;j<now;j++){
                if(strcmp(p,country[j])==0){
                    count[j]++;
                    find=1;
                }
            }
            if(!find){
                strcpy(country[now],p);
                count[now]++;
                now++;
                find=0;
            }
        }
        else{
            strcpy(country[now],p);
            count[now]++;
            now++;
        }
        for(j=0;j<now-1;j++){
            for(k=j+1;k<now;k++){
                if(strcmp(country[j],country[k])>0){
                char t[100];
                strcpy(t,country[j]);
                strcpy(country[j],country[k]);
                strcpy(country[k],t);
                int temp=count[j];
                count[j]=count[k];
                count[k]=temp;
                }
            }
        }
    }
    for(i=0;i<now;i++){
        printf("%s %d",country[i],count[i]);
        if(i!=now-1){
            printf("\n");
        }
    }
}
Jan 19, 2018 by Warriops3826
1.當我需要使用者輸入資料時,我可以用那個函數。
Scanline
Scan
Scanf
Scanz


2.如果我想輸出一行文字在螢幕上,我可以用那個函數。
Printf
Print
Printline
Prinfz


3.if(a == 10)
if(b == 10)
if(c == 10)
d = a;
else
d = b;
請問第5行的else是對應到哪一行的if
1
2
3


4.if(a >=0)
{
    if(a <= 100)
    {
    }
}
請問上面的兩個if判斷可以用下列哪一個取代
if(0<=a<=100)
if(a >=0 & a <= 100)
if(a >= 0 && a <= 100)
if(a >=0 and a <= 100)

5.下列哪一個不是C語言的迴圈
for
do......while
foreach
while


6.哪一個指令可以跳離最近的迴圈
break
jump
goto
continue


7.下列哪些是合法的陣列宣告
int array[100];
int array[];
int array[] = {1,2,3};
int array[5] = {1,2};


8.下列哪一個是陣列的第一個元素
array[-1]
array[0]
array[1]


9.下列何者是正確的指標宣告,代表指向一個正整數的指標。
int *num;
int &num
* int num;
& int num;






10.num = &a;
如果a的型態是float
那num的型態可能是什麼
float &
float *
float
float []


11.'0' 跟 "0" 是一樣的
yes

12.'1' + '2' == '3'
no

13.請問指標不能做下列哪種運算
++
-=
&
/


14.程式片段如下
char str[]="1234";
printf("%d",strlen(str));
請問printf 會輸出什麼呢
3
4
5
6


15.哪個是可以用在struct變數間的運算子
+
==
=
/
16.請問以下struct的宣告是不是合法的

struct {
   int a, b;
  char c, d;
} s1, s2;


17.當我要開啟一個檔案用來做寫入的時候
怎樣的語法是對的
out = fopen(\"text.txt\", \"b\");
out = fopen(\"text.txt\", \"a\");
out = fopen(\"text.txt\", \"w\");
out = fopen(\"text.txt\", \"r\");


18.檔案只有一種類型
就是文字檔案
yes
Jan 19, 2018 by Warriops3826
12,783 questions
183,443 answers
172,219 comments
4,824 users