33,009
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <string.h>
#define MAXN 10010
int a[MAXN], stack[MAXN];
int n, top;
int main(){
int i, next, k;
for (scanf("%d", &n); n; scanf("%d", &n)){
for (i=1; i<=n; ++i)
scanf("%d", a+i);
top = 0;
next = 1;
for (i=1; i<=n; ++i){
if (top && stack[top] == a[i])
top--;
else if (a[i] < next)
break;
else {
while (next < a[i])
stack[++top] = next++;
++next;
}
}
puts(i<=n ? "No":"Yes");
}
}
#include <stdio.h>
#include <algorithm>
using namespace std;
int n;
struct _task{
double start_time;
double run_time;
bool operator < (const _task &other) const{
return start_time < other.start_time;
}
} task[110];
int main(){
int i;
double tot_time = 0,current_time = 0;
scanf("%d", &n);
for (i=0; i<n; ++i)
scanf("%lf %lf", &task[i].start_time, &task[i].run_time);
sort(task, task+n); // 按开始时间排序
for (i=0; i<n; ++i){
if (current_time < task[i].start_time)
current_time = task[i].start_time;
current_time += task[i].run_time;
tot_time += current_time - task[i].start_time;
}
printf("%.4lf\n", tot_time/n);
}
#include <stdio.h>
int n, m;
struct _line {
int data[12];
void operator -= (const _line &other) {
int i;
for (i=0; i<m; ++i)
data[i] -= other.data[i];
}
void operator *= (const int value) {
int i;
for (i=0; i<m; ++i)
data[i] *= value;
}
} line[12];
int gcd(int a,int b){
return b ? gcd(b, a%b) : a;
}
int main(){
int i, j, k, s, ans;
for (scanf("%d %d", &n, &m); n; scanf("%d %d", &n, &m)){
for (i=0; i<n; ++i)
for (j=0; j<m; ++j)
scanf("%d", line[i].data + j);
ans = 0;
for (i=0; i<m; ++i){
for (j=0; j<n; ++j)
if (line[j].data[i])
break;
if (j>=n)
continue;
ans++;
if (line[j].data[i] < 0)
line[j] *= -1;
for (k=0; k<n; ++k)
if (k!=j && line[k].data[i]) {
_line temp = line[j];
if (line[k].data[i] < 0)
line[k] *= -1;
s = gcd(line[k].data[i], temp.data[i]);
temp *= line[k].data[i] / s;
line[k] *= line[j].data[i] / s;
line[k] -= temp;
}
line[j] = line[--n]; // 消除第j行
}
printf("%d\n", ans);
}
}
#include <stdio.h>
#include <string.h>
char str[300], key[20];
int main(){
char ch, *p;
while (scanf("%s", str)!=EOF) {
for (scanf("%s", key); strcmp(key, "END"); scanf("%s", key)){
if (strcmp(key, "NULL") == 0){
puts("0 NULL");
continue;
}
if ( (p = strstr(str, key)) ){
ch = *p;
*p = '\0';
printf("%d %s\n", strlen(str), str[0]?str:"NULL");
*p = ch;
} else {
printf("%d %s\n", strlen(str), str);
}
}
}
}
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
#define MAXN 10010
#define MAXM 104
int a[MAXN], ans[MAXN], list[MAXN] , degree[MAXN];
vector <int> next[MAXN]; // 记录关系,next[i][j] 表示 j作业需要先完成i作业
int n, m;
int main(){
int cases;
int i, j, k, max;
scanf("%d", &cases);
while (cases--) {
scanf("%d", &n);
memset(ans, -1, sizeof ans);
m = 0;
for (i=1; i<=n; ++i)
next[i].clear();
for (i=1; i<=n; ++i){
scanf("%d %d", a+i, degree+i);
if (degree[i] == 0){ // 入度为0的节点加入初始队列。
list[m++] = i;
ans[i] = 0;
}
for (j=0; j<degree[i]; ++j){
scanf("%d", &k);
next[k].push_back(i); // 节点K影响节点I
}
}
max = 0;
for (i=0; i<m; ++i){ // 开始处理队列
k = list[i];
ans[k] += a[k];
if (ans[k] > max) // 更新最大值
max = ans[k];
for (j=next[k].size()-1; j>=0; --j){ // 枚举该节点所能影响到的所有节点
if (ans[k] > ans[next[k][j]]) // 更新该作业可开始的最早时间
ans[next[k][j]] = ans[k];
if (--degree[next[k][j]] == 0){ // 若该节点入度为0,则加入队列。
list[m++] = next[k][j];
}
}
}
if (m != n) // 若队列中不足N个作业,说明作业没处理完
puts("No Solution.");
else
printf("%d\n", max);
}
}
#include <stdio.h>
#include <queue>
#include <vector>
using namespace std;
priority_queue <int,vector <int>, greater<int> > heap;
int main(){
int i, k, n, ans = 0;
scanf("%d", &n);
for (i=0; i<n; ++i){
scanf("%d", &k);
heap.push(k);
}
for (i=1; i<n; ++i){
k = heap.top();
heap.pop();
k += heap.top();
heap.pop();
ans += k;
heap.push(k);
}
printf("%d\n", ans);
}
#include <stdio.h>
#include <string.h>
int SafeStrcpy(char *dst, const char *src, int size, char *key)
{
int len = strlen(src);
char *location = strstr(src, key);
if (location != NULL)
len = location - src;
strncpy(dst, src, len); /* 这句可不要 */
return len;
}
void test(void)
{
enum {BUFFER_SIZE = 256, KEY_SIZE = 16};
char input[BUFFER_SIZE] = "";
char output[BUFFER_SIZE] = "";
char key[KEY_SIZE] = "";
int len = 0; /* 用于记录所拷贝字符串的长度 */
while (scanf("%s%*c", input) != EOF)
{
while (1)
{
scanf("%s%*c", key);
if (strcmp(key, "END") == 0)
break;
if (strcmp(key, "NULL") == 0)
key[0] = 0;
len = SafeStrcpy(output, input, BUFFER_SIZE, key);
if (len == 0)
printf("0 NULL\n");
else
printf("%d %.*s\n", len, len, output);
}
}
}
int main()
{
test();
return 0;
}