今天说点啥呢,没啥说的哈哈哈哈哈,看帖子的你今天要开心啊!!!PEACE & LOVE
1026 程序运行时间
思路: 结束时间减去开始时间除以CLK_TCK得到运行时间(以秒为单位)注意四舍五入,之后输出时分秒,注意数值部分是两位所以要%.2d;
#include<stdio.h>
#define CLK_TCK 100
int main() {
int c1, c2;
scanf("%d%d", &c1, &c2);
c1 = c2 - c1;
if(c1%100 >= 50)
c1 = c1/100+1;
else
c1 = c1/100;
printf("%.2d:%.2d:%.2d", c1/60/60, (c1%3600)/60, c1-(c1/3600)*3600-(c1%3600)/60*60);
return 0;
}
1027 打印沙漏
思路: 首先需要确定第一行符号的数量,之后将沙漏分为上下两部分输出即可。
#include<stdio.h>
int GetFirstLine(int i, int cnt, int sum) {
if(cnt > sum)
return 2*(i-1)-1;
if(cnt == sum)
return 2*i-1;
return GetFirstLine(i+1, cnt+(2*i+1)*2, sum);
}
int main() {
int n, cnt, _cnt;
char c;
scanf("%d %c", &n, &c);
cnt = _cnt = GetFirstLine(1, 1, n);
for(; _cnt>0; _cnt-=2, printf("\n")) {
for(int i=0; i<(cnt-_cnt)/2; i++)
putchar(' ');
for(int i=0; i<_cnt; i++, n--)
putchar(c);
}
_cnt = 3;
for(; _cnt<=cnt; _cnt+=2, printf("\n")) {
for(int i=0; i<(cnt-_cnt)/2; i++)
putchar(' ');
for(int i=0; i<_cnt; i++, n--)
putchar(c);
}
printf("%d", n);
return 0;
}
1028 人口普查
思路: 生日包括年月日共八位数字,所以直接将将年月日组成一个八位数字比大小即可。坑点有效数据为0时,直接输出0 。
#include<stdio.h>
#include<string.h>
#define LEN 6
#define OLD 18140906
#define YOUNG 20140906
int main() {
char yName[LEN], oName[LEN];
int cnt, _cnt, youngest, oldest;
_cnt = 0;
youngest = 18140906;
oldest = 20140906;
scanf("%d", &cnt);
while(cnt-- > 0) {
char name[LEN];
int year, mon, day, age;
scanf("\n%s %d/%d/%d", &name, &year, &mon, &day);
age = year*10000 + mon*100 + day;
if(age <= YOUNG && age >= OLD) {
_cnt++;
if(age < oldest) {
oldest = age;
strcpy(oName, name);
}
if(age > youngest) {
youngest = age;
strcpy(yName, name);
}
}
}
if(_cnt == 0)
putchar('0');
else
printf("%d %s %s", _cnt, oName, yName);
return 0;
}
1029 旧键盘
思路: 双指针遍历两个字符串,若相同则两指针均自增一比较下一位置的字符,不同将此字符与坏键比较,若数组不存在该字符则存入,然后主串指针自增一。因为字母只输出大写形式,所以英文字母在存入数组时直接存入大写字母。
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define LEN 100
int main() {
int i, j, k;
i = j = k = 0;
char s1[LEN], s2[LEN], keys[LEN] = "";
scanf("%s\n%s", &s1, &s2);
for(; s1[i] !='\0'; i++) {
if(s1[i] == s2[j] && j<strlen(s2))
j++;
else {
while(keys[k]!=toupper(s1[i]) && keys[k]!='\0')
k++;
if(keys[k] == '\0')//游标到了末尾说明数组中不存在该字符
keys[k] = toupper(s1[i]);
k = 0;
}
}
keys[strlen(keys)] == '\0';
printf("%s",keys);
return 0;
}
1030 完美数列
思路: 大体思路很简单:对数组排序,从小到大依次排序,之后对数组中每个数a在数组中找小于a * p的数的个数。数字个数多,所以排序采用快排的方式(最开始的处理方法是读数据时插入排序,测试点超时。)查找采用折半查找。另外虽然给的数均可用int表示,但乘积不一定,所以p和乘积用long long int。测试点3:只有一个数字。
#include<stdio.h>
#define LEN 100010
int Partition(int arr[], int low, int high) {
int pivot = arr[low];
while(low < high) {
while(high>low && arr[high]>=pivot)
high--;
arr[low] = arr[high];
while(low<high && arr[low]<=pivot)
low++;
arr[high] = arr[low];
}
arr[low] = pivot;
return low;
}
int QuickSort(int arr[], int low, int high) {
if(low < high) {
int pivotpos = Partition(arr, low, high);
QuickSort(arr, low, pivotpos-1);
QuickSort(arr, pivotpos+1, high);
}
}
int BiSearch(int arr[], int start, int len, long long int target) {
int low, high, mid;
low = start;
high = len;
while(low<=high) {
mid = (low+high)/2;
if(arr[mid] > target)
high = mid-1;
else
low = mid+1;
}
return high;
}
int main() {
int n, arr[LEN]= {0};
long long int product, p;
scanf("%d%d", &n, &p);
for(int i=1; i<=n; i++)
scanf("%d", &arr[i]);
QuickSort(arr, 1, n);
for(int i=1; i<=n; i++) {
if(n-i+1 < arr[0])
break;
product = p*arr[i];
if(arr[0] < BiSearch(arr, i, n, product)-i+1)
arr[0] = BiSearch(arr, i, n, product)-i+1;
}
printf("%d",arr[0]);
return 0;
}