博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LRJ
阅读量:4687 次
发布时间:2019-06-09

本文共 9753 字,大约阅读时间需要 32 分钟。

//3-1

1 #define _CRT_SECURE_NO_WARNINGS  2  3 #include 
4 5 int main() 6 { 7 int T; 8 char score[85]; 9 scanf("%d", &T);10 while (T-- > 0) {11 scanf("%s", score);12 int sum = 0;13 int num = 1;14 char prev = 'X';15 for (int i = 0; score[i] != '\0'; i++) {16 if (score[i] == 'O'){17 if (prev == 'O'){18 num++;19 }20 else {21 num = 1;22 }23 sum += num;24 prev = 'O';25 }26 else {27 prev = 'X';28 }29 }30 printf("%d\n", sum);31 }32 33 return 0;34 }

3-2

#define _CRT_SECURE_NO_WARNINGS #include 
bool isNum(char c){ return c >= '1' && c <= '9';}double mass(char m){ switch (m){ case 'C': return 12.01; case 'H': return 1.008; case 'O': return 16.00; case 'N': return 14.01; default: return 0.0; }}int charToInt(char c) { if (c >= '0' && c <= '9') return c - '0'; else return 0;}int main(){ int T; char molar[85]; scanf("%d", &T); while (T-- > 0) { scanf("%s", molar); double sum = 0; bool prevIsNum = false; char m = 'X'; // m is the previous molecular (C, H, O, N), 'X' is the previous for the first molecular, mass('X') == 0 for (int i = 0; molar[i] != '\0'; i++) { if (isNum(molar[i])){ if (prevIsNum){ char hiDigit = molar[i - 1]; char loDigit = molar[i]; sum += mass(m) * (charToInt(hiDigit) * 10 + charToInt(loDigit)); } else { } prevIsNum = true; } else { // C, H, O, N if (prevIsNum){ // if previous two letters are numbers, the mass is caculated elsewhere if (!isNum(molar[i - 2])){ char loDigit = molar[i - 1]; sum += mass(m) * charToInt(loDigit); } } else{ // add previous m sum += mass(m); } m = molar[i]; prevIsNum = false; } } // last letter is C/H/O/N if (!prevIsNum) sum += mass(m); printf("%.3f\n", sum); } return 0;}

 

3-3

#define _CRT_SECURE_NO_WARNINGS #include 
int main(){ int T; int n; scanf("%d", &T); while (T-- > 0) { scanf("%d", &n); int counts[10] = { 0 }; for (int i = 1; i <= n; i++){ int j = i; while (j > 0) { counts[j % 10]++; j /= 10; } } for (int i = 0; i < 10; i++){ printf("%d", counts[i]); if (i == 9) printf("\n"); else printf(" "); } } return 0;}

 

3-4

#define _CRT_SECURE_NO_WARNINGS #include 
bool sameStr(char *s1, char *s2, int len){ for (int i = 0; i < len; i++) if (s1[i] != s2[i]) return false; return true;}int main(){ int T; char s[85]; scanf("%d", &T); while (T-- > 0) { scanf("%s", s); if (s[0] != '\0'){ char first = s[0]; // find a equal char int start = 1; // start point for searching while (true) { int idx = start; for (; s[idx] != '\0'; idx++){ if (first == s[idx]) break; } if (s[idx] != '\0'){ // compare s[0..idx-1] with s[idx..] of same length = idx if (sameStr(s, s + idx, idx)){ printf("%d\n\n", idx); break; } start = idx + 1; } else{ // not found break; } } } else { // empty string } } return 0;}

 

3-5

#define _CRT_SECURE_NO_WARNINGS #include 
int main(){ int T = 1; char puzzle[5][5]; char newline; int empty_row, empty_col; while (true) { for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { scanf("%c", &puzzle[i][j]); if (i == 0 && j == 0 && puzzle[0][0] == 'Z'){ // end of test cases return 0; } if (puzzle[i][j] == ' '){ empty_row = i; empty_col = j; } } scanf("%c", &newline); // swallow the new line } // process moves char m; bool error = false; while (true){ scanf("%c", &m); if (m == '0') break; switch (m){ case 'A': if (empty_row == 0){ error = true; break; } // swap with above puzzle[empty_row][empty_col] = puzzle[empty_row - 1][empty_col]; puzzle[empty_row - 1][empty_col] = ' '; empty_row--; break; case 'B': if (empty_row == 4){ error = true; break; } // swap with bottom puzzle[empty_row][empty_col] = puzzle[empty_row + 1][empty_col]; puzzle[empty_row + 1][empty_col] = ' '; empty_row++; break; case 'L': if (empty_col == 0){ error = true; break; } // swap with left puzzle[empty_row][empty_col] = puzzle[empty_row][empty_col - 1]; puzzle[empty_row][empty_col - 1] = ' '; empty_col--; break; case 'R': if (empty_col == 4){ error = true; break; } // swap with above puzzle[empty_row][empty_col] = puzzle[empty_row][empty_col + 1]; puzzle[empty_row][empty_col + 1] = ' '; empty_col++; break; } } scanf("%c", &newline); // swallow the new line printf("Puzzle #%d:\n", T++); if (error){ printf("This puzzle has no final configuration.\n"); } else { for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { printf("%c", puzzle[i][j]); if (j != 4) printf(" "); } printf("\n"); } } printf("\n"); }}

 

3-6

#define _CRT_SECURE_NO_WARNINGS #include 
struct grid{ char c; int n;};struct grid puzzle[10][10];int main(){ int r, c; char newline; int T = 1; while (true) { scanf("%d", &r); if (r == 0) break; scanf("%d", &c); scanf("%c", &newline); // swallow the new line int num = 1; for (int row = 0; row < r; row++){ for (int col = 0; col < c; col++) { scanf("%c", &puzzle[row][col].c); if ((puzzle[row][col].c != '*') && // white (row == 0 || col == 0 || puzzle[row - 1][col].c == '*' || puzzle[row][col - 1].c == '*')){ // eligible puzzle[row][col].n = num++; } else if (puzzle[row][col].c == '*') { // black puzzle[row][col].n = -1; } else { // illegible white puzzle[row][col].n = 0; } } scanf("%c", &newline); // swallow the new line } printf("puzzle #%d:\n", T++); // Across words printf("Across\n"); for (int row = 0; row < r; row++){ int col = 0; while (col < c) { while (col < c && puzzle[row][col].n < 0) { // skip black col++; } if (col < c) { printf("%d.", puzzle[row][col].n); } while (col < c && puzzle[row][col].n >= 0) { // eligible and illegible white printf("%c", puzzle[row][col].c); col++; } printf("\n"); while (col < c && puzzle[row][col].n < 0) { // skip black col++; } } } // Down words printf("Down\n"); for (int row = 0; row < r; row++){ for (int col = 0; col < c; col++) { if ((puzzle[row][col].c != '*') && (row == 0 || puzzle[row - 1][col].c == '*')) { printf("%d.", puzzle[row][col].n); for (int dr = row; dr < r && puzzle[dr][col].c != '*'; dr++){ printf("%c", puzzle[dr][col].c); } printf("\n"); } } } printf("\n"); // Separate output for successive input puzzles by a blank line. /* for (int row = 0; row < r; row++) { for (int col = 0; col < c; col++) { printf("%c ", puzzle[row][col].c); } printf("\n"); } for (int row = 0; row < r; row++) { for (int col = 0; col < c; col++) { printf("%d ", puzzle[row][col].n); } printf("\n"); } */ } return 0;}

 

转载于:https://www.cnblogs.com/patrickzhou/p/5659758.html

你可能感兴趣的文章
2017~回顾分享
查看>>
let const var的区别与作用
查看>>
计算出线在屏幕内的最长坐标
查看>>
使用svn——项目的目录布局
查看>>
Linux学习之CentOS(二十五)--Linux磁盘管理:LVM逻辑卷基本概念及LVM的工作原理
查看>>
【bzoj4310/hdu5030-跳蚤】后缀数组
查看>>
深度信任网络的快速学习算法(Hinton的论文)
查看>>
RSA System.Security.Cryptography.CryptographicException
查看>>
s的封装和信息隐蔽
查看>>
excelhttp://www.cnblogs.com/caoyuanzhanlang/p/3591904.html
查看>>
webservice整合spring cxf
查看>>
[解题报告] 100 - The 3n + 1 problem
查看>>
Entity Framework 学习高级篇1—改善EF代码的方法(上)
查看>>
Mybatis逆向工程配置文件详细介绍(转)
查看>>
String类的深入学习与理解
查看>>
不把DB放进容器的理由
查看>>
OnePage收集
查看>>
Java parseInt()方法
查看>>
yahoo的30条优化规则
查看>>
[CCF2015.09]题解
查看>>