LoginSignup
0
0

More than 5 years have passed since last update.

C言語の典型問題

Last updated at Posted at 2018-10-12

C言語の競プロ典型問題3選

素数

/#include
/#include
/#include
//素数nまでを求め、それらの合計と掛け算を求めるプログラム
int main(void){
int x[50];
printf("input number:");
scanf("%d",&x[0]);
int yakusu,sum,mul=1;

    for(int i=0;i<x[0];i++){
            yakusu=0;
            for(int j=1;j<=i;j++){
                    if(i%j==0) yakusu++;
            }
            if(yakusu==2){
                    printf("%d",i);
                    sum+=i;
                    mul*=i;
            }

    }
      printf("\n");
      printf("sum:%d\n",sum);
      printf("mul:%d\n",mul);
      return 0;

三角形

/#include
/#include

int main(void){
int x[6];
float a, b, c;
for(int i=0;i<6;++i) scanf("%d", &x[i]);

a = (x[0]+x[2]+x[4])/3.;
b = (x[1]+x[3]+x[5])/3.;

x[2]-=x[0];
x[3]-=x[1];
x[4]-=x[0];
x[5]-=x[1];

c = abs(x[2]*x[5]-x[3]*x[4]) / 2.;
printf("%f, %f\n", a, b);
printf("%f\n", c);

}

文字並び替え

'''c:str.c
#include
#include

int main(void)
{
char str[256];
printf("input:");
fgets(str, 256, stdin);
int alphabet[26]={0};
int len = strlen(str);
char abc[26];
int words=0;
for(char i = 0;i<26;++i) abc[i]='a'+i;

    for(int i=0;i<len;++i){
        if(str[i]=='.'||str[i]==',') continue;
        if(str[i]==' '){
            ++words;
            continue;
        }
        int x = tolower(str[i])-'a';
        alphabet[x]++;
    }


    for (int i = 0; i < 25; i++) {
        for (int j = 25; j > i; j--) {
            if (alphabet[j-1] < alphabet[j]) {
                int temp = alphabet[j-1];
                alphabet[j-1] = alphabet[j];
                alphabet[j] = temp;
                int temp_ = abc[j-1];
                abc[j-1]=abc[j];
                abc[j]=temp_;
            }
        }
    }

    for (int i=0;i<26;++i){
        if(alphabet[i]==0){
            break;
        }
        printf("%c:%d\n", abc[i], alphabet[i]);
    }
    printf("Word count:%d" ,words+1);
    return 0;

}
'''

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0