簡單寫一個程式來算看看吧!
也提供大家參考~
🍼 泡奶溫度計算器
計算中...
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n, k, charge, actual = 0;
scanf("%d %d\n", &n, &k);
for(int i = 0; i < n; i++){
int c;
scanf("%d ", &c);
if(i != k){
actual += c;
}
}
actual /= 2;
scanf("%d", &charge);
if(charge > actual)
printf("%d", charge - actual);
else
printf("Bon Appetit");
return 0;
}
6 7 3
.......
...O...
....O..
.......
OO.....
OO.....
範例輸出:OOO.OOO
OO...OO
OOO...O
..OO.OO
...OOOO
...OOOO
/*
Solve by Desolve Lin, 2016/09/15
Please help yourself take it for reference,
but kindly have a link to my blog if you use it at other website,
thanks a lot!
*/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
void printAns(int r, int c, char grid[][201]){
for(int i = 0; i < r; i++){
for(int j = 0; j < c; j++){
printf("%c", grid[i][j]);
}
printf("\n");
}
}
void printFull(int row, int column){
for(int i = 0; i < row; i++){
for(int j = 0; j < column; j++){
printf("O");
}
printf("\n");
}
}
// t3 result
void expAndRev(int row, int column, char grid[][201]){
// Expand to find detonate area
for(int i = 0; i < row; i++){
for(int j = 0; j < column; j++){
if(grid[i][j] == 'O'){
// Those who are detonated areas are not real bomb and can't be expanded again.
if(i - 1 >= 0)
if(grid[i-1][j] == '.')
grid[i-1][j] = 'E';
if(j - 1 >= 0)
if(grid[i][j-1] == '.')
grid[i][j-1] = 'E';
if(i + 1 < row)
if(grid[i+1][j] == '.')
grid[i+1][j] = 'E';
if(j + 1 < column)
if(grid[i][j+1] == '.')
grid[i][j+1] = 'E';
}
}
}
// Reverse to find t3 bomb state
for(int i = 0; i < row; i++){
for(int j = 0; j < column; j++){
if(grid[i][j] != '.'){
grid[i][j] = '.';
}else{
grid[i][j] = 'O';
}
}
}
}
int main() {
int row, column, n;
scanf("%d %d %d\n", &row, &column, &n);
char grid[201][201];
for(int i = 0; i < row; i++){
char tmp;
for(int j = 0; j < column; j++){
scanf("%c", &grid[i][j]);
}
scanf("%c", &tmp); // remove linefeed
}
if(n == 1)
printAns(row, column, grid);
else if(n % 2 == 0)
printFull(row, column);
else{
// t3 state => expand and reverse once : t3, 7, 11, 15
// t5 state => expand and reverse twice: t5, 9, 13, 17
expAndRev(row, column, grid);
if(n % 4 == 1)
expAndRev(row, column, grid);
printAns(row, column, grid);
}
return 0;
}
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
int n;
scanf("%d",&n);
int *A = malloc(sizeof(int) * n);
for(int i = 0; i < n; i++){
scanf("%d",&A[i]);
}
int d = -1;
for(int i = 0; i < n-1; i++)
for(int j = i + 1; j < n; j++){
if(A[i] == A[j])
if(d < 0) d = j - i;
else if(j - i < d) d = j - i;
}
printf("%d", d);
return 0;
}
3
2 1
3 0
3 2
範例輸出:2 1
1 2 3
-1
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
// swap: make element i, j of the array arr[] swap with each other.
void swap(int *arr, int i, int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
/* permute: calculate absolute permutation of k, if NOT possible then return -1.
/ case: k==0
Array itself is the answer, no need to swap anything.
case: other
Suppose there's an absolute permutation of the array, then element i must swap with element i+k,
then the difference will be -k, +k.
Since then, a section will be 2*k.
Ex: 0, 1, ..., k-1 -> k+0, k+1, ... k+k-1, and next round should start from 0+2k.
We could start from i=0, each time adding 2*k to find if there's a pair to swap,
that is, from 0, k -> 2k, 3k -> 4k, 5k ...(if still < n)
(Notice that if we find a single num that can't be paired, we have to return -1.)
Then i=1, i=2, i=3 ... ...
And we could get the array done with absolute permutation of k.
*/
int permute(int *arr, int k, int n){
for(int i = 0; i < k; i++){
for(int j = i; j < n; j += k*2){
if(j + k >= n)
return -1;
else
swap(arr, j, j+k);
}
}
return 0;
}
int main(){
int t;
scanf("%d",&t);
for(int a0 = 0; a0 < t; a0++){
int n, k;
scanf("%d %d",&n,&k);
int arr[n];
for(int i = 0; i < n; i++)
arr[i] = i + 1;
if(k == 0){
for(int i = 0; i < n; i++)
printf("%d ", arr[i]);
}else{
if(permute(arr, k, n) == -1)
printf("-1");
else
for(int i = 0; i < n; i++)
printf("%d ", arr[i]);
}
printf("\n");
}
return 0;
}
5 3
4 2 6 1 10
輸出4
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n, k;
scanf("%d %d\n", &n, &k);
int t[n];
for(int i=0; i<n; i++)
scanf("%d ", &t[i]);
int special_num = 0, page = 1;
for(int chap=1; chap<=n; chap++)
{
for(int pb_cnt=1; pb_cnt<=t[chap-1]; pb_cnt++)
{
if(pb_cnt == page) special_num++;
if(pb_cnt % k == 0 && pb_cnt < t[chap-1]) page++;
}
page++;
}
printf("%d", special_num);
return 0;
}
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
char* s = (char *)malloc(512000 * sizeof(char));
scanf("%s",s);
long n;
scanf("%ld",&n);
if(strlen(s) == 1)
{
if(s[0] == 'a')
printf("%ld", n);
else
printf("0");
}else {
int leng = strlen(s);
long quotient = n / leng, remainder = n % leng;
int a_total = 0, a_remain = 0, i = 0;
while(i < remainder){
if(s[i] == 'a'){
a_total++;
a_remain++;
}
i++;
}
while(i < leng){
if(s[i] == 'a'){
a_total++;
}
i++;
}
printf("%ld", quotient * a_total + a_remain);
}
return 0;
}
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
long long int t;
scanf("%lli",&t);
long long int n = 1;
long long int cycle = 3;
while(t > cycle){
t -= cycle;
n++;
cycle *= (long long int) 2;
}
printf("%lli", (long long int)cycle - t + 1);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int t;
scanf("%d\n", &t);
for(int i=0; i<t; i++)
{
int n, m, s, result;
scanf("%d %d %d\n", &n, &m, &s);
result = (m % n) + (s-1) % n;
if (result > n) result %= n;
if (result == 0) result = n;
printf("%d\n", result);
}
return 0;
}
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int compare(const void *arg1, const void *arg2){
return (*(int *)arg1 - *(int *)arg2);
}
int main(){
int n;
int m;
scanf("%d %d",&n,&m);
if(n == m)
printf("0");
else{
int *c = malloc(sizeof(int) * m);
int max = 0, temp = 0;
for(int i = 0; i < m; i++){
scanf("%d",&c[i]);
}
qsort((void *)c, m, sizeof(int), compare);
max = c[0];
for(int i = 1; i < m; i++){
temp = (c[i]-c[i-1])/2;
max = (max > temp)? max : temp;
}
temp = n-1 - c[m-1];
max = (max > temp)? max : temp;
printf("%d", max);
}
return 0;
}
2
5
2 1 5 3 4
5
2 5 1 3 4
輸出3
Too chaotic
1 2 3 -> 3 1 2
編號3的人賄賂了2次,#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
int T;
scanf("%d",&T);
for(int a0 = 0; a0 < T; a0++){
int n, bribe_total = 0, chao = 0;
scanf("%d",&n);
int *q = malloc(sizeof(int) * n);
int q_now[n], pos_now[n];
for(int q_i = 0; q_i < n; q_i++){
scanf("%d",&q[q_i]);
q_now[q_i] = q_i + 1;
pos_now[q_i] = q_i;
}
for(int i = 0; i < n; i++){
if(pos_now[q[i]-1] - i > 2)
{
chao = 1;
break;
}
}
if(chao){
printf("Too chaotic\n");
}else{
for(int i = 0; i < n-1; i++)
{
int temp = q[i], diff = pos_now[q[i]-1] - i;
if( diff == 2 )
{
bribe_total +=2;
pos_now[q_now[i+1]-1]++;
pos_now[q_now[i]-1]++;
pos_now[q_now[i+2]-1] -= 2;
q_now[i+2] = q_now[i+1];
q_now[i+1] = q_now[i];
q_now[i] = temp;
} else if( diff == 1 ){
bribe_total++;
pos_now[q_now[i]-1]++;
pos_now[q_now[i+1]-1]--;
q_now[i+1] = q_now[i];
q_now[i] = temp;
}
}
printf("%d\n", bribe_total);
}
}
return 0;
}
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
int n;
int k;
scanf("%d %d",&n,&k);
int *c = malloc(sizeof(int) * n);
for(int c_i = 0; c_i < n; c_i++){
scanf("%d",&c[c_i]);
}
int energy = 100, jumptime = n / k;
energy -= jumptime;
energy -= c[0] * 2; // line a
for(int i = k; i < n; i += k) // line b
energy -= c[i]*2;
printf("%d", energy);
return 0; }
0 1 2 5 3 3 0那我們當然直覺可以講出其最大辭典排法為:
5 3 3 2 1 0 0也就是由左至右為遞減的排列。
0 1 2 5 3 3 0紅字標起來的地方是完整的遞減排序,
0 1 2 5 3 3 0 0 1 3 5 3 2 0換完以後就是所有可能中的辭典順序最小的解嗎?
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
void swap(char *x, char *y){
char temp;
temp = *x;
*x = *y;
*y = temp;
}
int next_permutation(char *a, size_t length){
size_t i, j;
if (length == 0) return 0;
i = length - 1;
while (i > 0 && a[i-1] >= a[i])
i--;
if (i == 0)
return 0;
// 現在保證存在一個i使得 a[i-1] < a[i],
// 在此之前其他的數字由左到右都是越來越小
// 也就是本來就是排序較大的permutation,這部分換了字串不會比較大
// 所以如果全部都是降序排列,就直接回傳0
// 接下來再找一個j,滿足a[j]比a[i-1]大,swap過來,可以保證現在的陣列比較大
// 且由右往回找,可以確保換的值是最小的。
// 剩下的部分就是把原本降序排列改成升序,這樣才是比較小的排列方式。
j = length - 1;
while (a[j] <= a[i-1])
j--;
swap(&a[i-1], &a[j]);
j = length - 1;
while(i < j){
swap(&a[i], &a[j]);
i++;
j--;
}
return 1;
}
int main() {
int n;
scanf("%d\n", &n);
for(int i=0; i<n; i++){
char word[100];
scanf("%s", word);
if (next_permutation(word, strlen(word))){
printf("%s\n", word);
} else {
printf("no answer\n");
}
}
return 0;
}
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
int n;
scanf("%d",&n);
int *c = malloc(sizeof(int) * n);
for(int c_i = 0; c_i < n; c_i++){
scanf("%d",&c[c_i]);
}
int cnt = 0;
for(int i = 0; i+1 < n; i++){
cnt++;
if((i+2) < n)
if(c[i+2] == 0)
i++;
}
printf("%d", cnt);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>
/*
* Complete the function below.
*/
int maxXor(int l, int r) {
int max = 0;
for(int a=l; a<=r; a++)
for(int b=a; b<=r; b++)
if((a^b) > max)
max = (a^b);
return max;
}
int main() {
int res;
int _l;
scanf("%d", &_l);
int _r;
scanf("%d", &_r);
res = maxXor(_l, _r);
printf("%d", res);
return 0;
}
留意%0.1lf可以保證輸出的小數點後面顯示1位。
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int compare(const void *arg1, const void *arg2){
return (*(int *)arg1 - *(int *)arg2);
}
int main() {
int n;
scanf("%d\n", &n);
int x[n];
double mean = 0.0;
for(int i = 0; i < n; i++)
{
scanf("%d\n", &x[i]);
mean += (double) x[i];
}
mean /= (double) n;
printf("%0.1lf\n", mean);
qsort((void *)x, n, sizeof(int), compare);
if(n % 2 == 1)
printf("%d\n", x[(n+1)/2 - 1]);
else
printf("%0.1lf\n", (double)(x[n/2 - 1] + x[n/2+1-1])/2.0);
int every_cnt[100000] = {0};
int max_n = x[0];
for(int i = 0; i < n; i++){
every_cnt[x[i]]++;
}
for(int j = x[0]; j < 100000; j++)
{
if(every_cnt[j] == 0)
continue;
if(every_cnt[j] > every_cnt[max_n])
max_n = j;
}
printf("%d\n", max_n);
return 0;
}
4 3
1 7 2 4
輸出則為3
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n, k, result=0;
scanf("%d %d",&n, &k);
// Special case: 1. integer num only 1 -> result = 1;
// 2. divisor k = 1 -> result = 1
if(n == 1 || k == 1){
printf("%d", 1);
}else{
int s_i, num[k];
for(int i = 0; i < k; i++)
num[i] = 0;
// Store num of those whose remainder is k' at num[k'].
for(int arr_i = 0; arr_i < n; arr_i++){
scanf("%d",&s_i);
s_i %= k;
num[s_i] += 1;
}
for(int i = 1; i < k/2; i++){
if(num[i] > num[k-i])
result += num[i];
else
result += num[k-i];
}
if(num[0] > 0) result++;
if(k % 2 == 0){
if(num[k/2] > 0)
result++;
}else{
if(num[k/2] > num[k/2 + 1])
result += num[k/2];
else
result += num[k/2 +1];
}
printf("%d", result);
}
return 0;
}
5 4 4 2 2 8
在一次cut的操作中,我們將6根樹枝都減去2的長度,3 2 2 6
重複上述的操作直到沒有樹枝留下。#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int compare(const void *arg1, const void *arg2){
return (*(int *)arg1 - *(int *)arg2);
}
int main(){
int n;
scanf("%d",&n);
int arr[n];
for(int arr_i = 0; arr_i < n; arr_i++){
scanf("%d",&arr[arr_i]);
}
qsort((void *)arr, n, sizeof(int), compare);
int remain_num = n, cut_num = 1;
printf("%d\n", remain_num);
for(int i = 1; i < n ; i++){
if (arr[i] > arr[i-1]){
remain_num -= cut_num;
printf("%d\n", remain_num);
cut_num = 1;
}else {
cut_num++;
}
}
return 0;
}
2
4 3
-1 -3 4 2
4 2
0 -1 2 1
表示有兩個test case,YES
NO
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
int t;
scanf("%d",&t);
bool result[t];
for(int a0 = 0; a0 < t; a0++){
int n;
int k;
scanf("%d %d",&n,&k);
int a[n], cnt = 0;
for(int a_i = 0; a_i < n; a_i++){
scanf("%d",&a[a_i]);
if(a[a_i] <= 0) cnt++;
}
if (k > cnt)
result[a0] = true;
else
result[a0] = false;
}
for(int a0 = 0; a0 < t; a0++){
if(result[a0])
printf("YES\n");
else
printf("NO\n");
}
return 0;
}