ETH官方钱包

前往
大廳
主題

[OJ練習] 374、10346、10340、488 我發現我數學不好

テリ君(桃夫模式) | 2022-12-23 11:00:53 | 巴幣 2 | 人氣 189

374(2/5)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// #include <math.h> not suitable for such large number.

long long R(long long B, long long P, long long M){
    long long result = 1;
    
    if(P == 0) return 1;
    else if(P == 1) return B % M;
    else{
        result = result * R(B, P / 2, M);
        if(P % 2) return result * result * B % M;
        else return result * result % M;
    }
}

int main(){
    
    long long B, P, M;
    while(scanf("%lld%lld%lld", &B, &P, &M) != EOF){
        printf("%lld\n", R(B, P, M));
    }
    
    
    return 0;
}

10346(2/5)

#include <stdio.h>

int main(){
    
    int n, k;
    int r; // r for result;
    
    while(scanf("%d %d", &n, &k) != EOF){
        r = n;
        while(n / k){
            r += n / k;
            n = n / k + n % k;
        }
        
        printf("%d\n", r);
    }
    
    return 0;
}

10340(2/5)

#include <stdio.h>
#include <string.h>

#define MAX_LEN 256

int main() {
  char s[MAX_LEN], t[MAX_LEN];

  while (scanf("%s %s", s, t) != EOF) {
    int s_len = strlen(s), t_len = strlen(t); // set strlen

    int s_idx = 0, t_idx = 0; // refresh

    while (s_idx < s_len && t_idx < t_len) {
      
      if (s[s_idx] == t[t_idx]) s_idx++;
      
      t_idx++;
    }

    if (s_idx == s_len) printf("Yes\n");
    else printf("No\n");
  }

  return 0;
}

488(1/5)

#include <stdio.h>

int main(){
    int w, h_c, n; // waves, height, numbers
    
    scanf("%d", &w);
    
    for(int CASE = 0; CASE < w; CASE++){
        scanf("\n%d", &h_c);
        scanf("%d", &n);
        
        for(int i = 0; i < n; i++){
            if(i == n - 1){ // if last wave
                int h = 1;
                while(h <= h_c){
                    for(int k = 0; k < h; k++){
                        printf("%d", h);
                    }
                    printf("\n");
                    h++;
                }
                h--; // same height
                while(h--){
                    for(int k = 0; k < h; k++){
                        printf("%d",h);
                    }
                    printf("\n");
                }
            }
            else{
                int h = 1;
                while(h <= h_c){
                    for(int k = 0; k < h; k++){
                        printf("%d", h);
                    }
                    printf("\n");
                    h++;
                }
                h--; // same height
                while(h--){
                    for(int k = 0; k < h; k++){
                        printf("%d",h);
                    }
                    printf("\n");
                }
                printf("\n");
            }
        }
    }
    
    return 0;
}


374本身不難懂,主要是要宣告long long是第一次,畢竟那個測資有夠大,另外就是要自己刻mod,因為math.h承受不住= =

10346就是個菸猴說服自己多抽菸的運算模式,一樣是卡在運算,想很久才通= =

10340就是第一個詞或句要完整包含於第二個詞或句,簡寫也是一樣,大小寫也要分

488就相對簡單,只是迴圈的應用和概念釐清而已。


創作回應

更多創作