【codeup 1934】找x
当然也可利用 i == n 判断是否有x存在
#include<cstdio> int main(){ const int max = 210; int a[max] = {0}; int n, j = -1; scanf("%d", &n); for(int i=0; i < n; i++){ scanf("%d", &a[i]); } int x; scanf("%d", &x); for(int i = 0; i < n; i++){ if(a[i] == x){ printf("%d", i); j = 0; break; } } if(j == -1){printf("-1");} return 0; }
【PAT B1036】跟奥巴马一起编程
#include<cstdio> int main(){ int row; int col; char character; do{ scanf("%d %c", &col, &character); }while(col > 20 || col < 3); if(col % 2 == 0){ row = col / 2; }else{ row = col / 2 + 1; } for(int i=0; i < col; i++){ printf("%c", character); } printf(" "); for(int i=0; i < row; i++){ printf("%c", character); for(int j=0; j < col - 2; j++){ printf(" "); } printf("%c ", character); } for(int i=0; i < col; i++){ printf("%c", character); } return 0; }
【codeup 1928】 日期差值
#include<cstdio> int month[13][2] = { {0, 0}, {31, 31}, {28, 29}, {31, 31}, {30, 30}, {31, 31}, {30, 30}, {31, 31}, {31, 31}, {30, 30}, {31, 31}, {30, 30}, {31, 31}}; bool isLeap(int year){ return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } void change(int* y1, int* y2){ int temp; if(*y1 > *y2){ temp = *y1; *y1 = *y2; *y2 = temp; } return; } int main(){ int counts = 1; int data1, year1, month1, day1; int data2, year2, month2, day2; while(scanf("%d%d", &data1, &data2) != EOF){ change(&data1, &data2); } year1 = data1 / 10000; month1 = data1 % 10000 / 100; day1 = data1 % 100; year2 = data2 / 10000; month2 = data2 % 10000 / 100; day2 = data2 % 100; while(year1 < year2 || month1 < month2 || day1 < day2){ day1 ++; if(day1 == month[month1][isLeap(year1)] + 1){ month1 ++; day1 = 1; } if(month1 == 13){ year1 ++; month1 = 1; } counts ++; } printf("%d ", counts); return 0; }
十进制内 进位制之间相互转换
#include<cstdio> int main(){ int P, Q; int x; printf("输入数字x 原来进位制P 被转换进位制Q "); scanf("%d %d %d", &x, &P, &Q); //先把P进制的x转换为10进制 int y = 0, product = 1; while(x != 0){ y = y + (x % 10) * product; x = x / 10; product = product * P; } //将十进制数转换为Q进制数 int z[40], num = 0; do{ z[num ++] = y % Q; y = y / Q; }while(y != 0); for(int i = num - 1; i >= 0; i --){ printf("%d", z[i]); } return 0; }
【codeup 5901】回文串
#include<cstdio> #include<string.h> int main(){ char str[256]; gets(str); int len,tag = -1; len = strlen(str); int i = 0; for(i ; i < (len / 2); i ++){ if(str[i] == str[len - 1 - i]){ tag = 1; }else{ tag = 0; break; } } if(tag == 0){ printf("不是回文串 "); }else{ printf("回文串 "); } return 0; }
【PAT B1009】说反话
#include<cstdio> #include<string.h> int main(){ char str[256]; gets(str); int len = strlen(str); char ans[90][90]; int row = 0, col = 0; //定义行、列 for(int i = 0; i < len; i ++){ if(str[i] != ' '){ ans[row][col] = str[i]; col ++; }else{ ans[row][col] = ' '; col = 0; row ++; } } for(int i = row; i >= 0; i --){ printf("%s", ans[i]); if(i > 0)printf(" "); } return 0; }